I want to consume the .net JSON string web services in java. I am getting bad request error always but through SOAP UI i am getting the response. Any one suggest me how to consume the .net rest services in java.
"{"\DocumentId\":"\29292\","\Note\":"\jaasBook\"}"
String uri = "http://example.com/service.asmx/GetInfo";
URL url = new URL(uri);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("charset", "utf-8");
connection.setRequestProperty("Accept", "application/json");
connection.setChunkedStreamingMode(0);
connection.connect();
byte[] parameters = {"\DocumentId\":"\29292\","\Note\":"\jaasBook\"}".getBytes("UTF-8");
DataOutputStream os = new DataOutputStream(connection.getOutputStream());
os.write(parameters);
os.close();
InputStream response;
if(connection.getResponseCode() == 200){response = connection.getInputStream();}
else{response = connection.getErrorStream();}
InputStreamReader isr = new InputStreamReader(response);
StringBuilder sb = new StringBuilder();
BufferedReader br = new BufferedReader(isr);
String read = br.readLine();
while(read != null){
sb.append(read);
read = br.readLine();
}
At first glance it looks like you are improperly escaping the JSON string:
Note that the backslash comes before each double-quote you need to escape.