java - JSON with HttpURLConnection not working on PUT -
i stuck when working json in java application. quite newbie both json , httpurlconnection, doing basic wrong. have tried quite lot of things , i'm not able find error, can me.
i trying put call aganist server. code.
//desired json string: [{"type":"url", "parsed_data":"testdata"}] string infotojson = "[{\"type\":\"url\",\"parsed_data\":\"testdata\"}]"; string uri = "www.myuri.com" string user = "myuser"; string password = "mypass"; string userpass = user + ":" + password; string encoded = new sun.misc.base64encoder().encode(userpass.getbytes("utf-8")); url url = new url(uri); httpurlconnection connection = (httpurlconnection) url.openconnection(); connection.setrequestproperty ("authorization", "basic " + encoded); connection.addrequestproperty("accept", "application/json"); connection.addrequestproperty("content-type", "application/json"); connection.setdooutput(true); connection.setrequestmethod("put"); outputstream out = connection.getoutputstream(); out.write(infotojson.getbytes("utf-8")); system.out.println("error: " + connection.getresponsecode()); out.close();
the user/pass verified work, on "get" method.
i keep receiving "error 400" code, have not been able identify particular problem.
i have feeling problem related "infotojson" variable in way or another: codification of json string, char encoding or similar. however, have not been able find problem after million tests.
any hint on this?
edit: code has been corrected hgoebl suggestions, current version working one.
Comments
Post a Comment