I'm trying to send a JSON object to a server on a custom port. I never get any response back from the server however. I think the error might be in the string entity but I can't figure out what it is.
I've had a look at these other issues:
HTTP POST using JSON in Java
post json to java server
How to build a http post request with the correct entity with Java and not using any library?
How to POST data in Android to server in JSON format?
None of them have solved my problem. I'm trying the solution (16 votes) from "HTTP POST using JSON in Java" but it wont work.
Here's my code:
public void reset() {
DefaultHttpClient httpClient = new DefaultHttpClient();
try {
System.out.println("Start");
jsonURL = "http://x.x.x.x:3994";
HttpPost request = new HttpPost(jsonURL);
StringEntity params = new StringEntity("{\"id\":1,\"method\":\"object.deleteAll\",\"params\":[\"subscriber\"]}");
request.addHeader("Content-Type", "application/json");
request.setEntity(params);
HttpResponse response = httpClient.execute(request);
System.out.println("End");
} catch (Exception ex) {
ex.printStackTrace();
} finally {
httpClient.getConnectionManager().shutdown();
}
}
The console prints "Start" when it begins but I never get any output from "End" nor any stack trace from the exception. When debugging the debugger stops at the "httpClient.execute(request)
" line and never continues.
When I run this command from my terminal:
echo '{"id":1, "method":"object.deleteAll", "params":["subscriber"]} ' | nc x.x.x.x 3994
Everything is executed properly and my request is received by the server.
I think my problem might be with the StringEntity
but I'm not sure.
EDIT:
Using Wireshark I was able to capture this packet being sent to the server:
POST / HTTP/1.1 Content-Type: application/json Content-Length: 60
Host: x.x.x.x:3994 Connection: Keep-Alive User-Agent:
Apache-HttpClient/4.2.1 (java 1.5)
{"id":1,"method":"object.deleteAll","params":["subscriber"]}
and this packet coming back from the server:
{"id":null,"error":{"code":-32700,"message":"Unexpected character ('P' (code 80)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')"}}
{"id":null,"error":{"code":-32700,"message":"Unexpected character ('c' (code 99)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')"}}
{"id":null,"error":{"code":-32700,"message":"Unexpected character ('C' (code 67)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')"}}
{"id":null,"error":{"code":-32700,"message":"Unexpected character ('C' (code 67)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')"}}
{"id":null,"error":{"code":-32700,"message":"Unexpected character ('H' (code 72)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')"}}
{"id":null,"error":{"code":-32700,"message":"Unexpected character ('C' (code 67)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')"}}
{"id":null,"error":{"code":-32700,"message":"Unexpected character ('U' (code 85)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')"}}
{"id":null,"error":{"code":-32700,"message":"Line must contain a JSON object"}}
In your working example, you use nc, which means you are writing directly to TCP, not using HTTP, correct?
Therefore, your HTTP invocation using HTTPClient is sending unexpected data. The errors indicate that the HTTP wrapper is what's at issue.
Unexpected character "P" as in "POST", "C" as in "Content-Type", etc. The server is reading each line, expecting a raw JSON string, but is getting the HTTP instead.
Solution: You probably want to use a raw Java socket instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With