6

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"}}
Community
  • 1
  • 1
span
  • 5,405
  • 9
  • 57
  • 115
  • Your `Content-Type` should be `application/json`, not `application/x-www-form-urlencoded`. – Perception Feb 08 '13 at 14:34
  • Thanks, tried changing, still don't work :( – span Feb 08 '13 at 14:35
  • Looks like the issue might be on your server side, if indeed those traces are coming from there. You should try your request in a REST console, and compare the headers set by the tool with the ones you are using in your code. – Perception Feb 08 '13 at 14:51
  • Hmm, thanks. Will try to investigate further! – span Feb 08 '13 at 14:57
  • How is the server implemented? Is it an HTTP server (i.e. PHP, Java Servlet,...)? Seems that it's something else. – remigio Feb 08 '13 at 15:29
  • Yeah, I'm looking at the server implementation now. It seems cmonkey might have hit the spot although the server admin has gone home for the weekend and the nc command has now also stopped working. So I'll have to wait until monday for a fix :/. Thanks for ideas though! – span Feb 08 '13 at 15:33

1 Answers1

4

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.

cmonkey
  • 4,256
  • 1
  • 26
  • 46
  • Interesting! I will look into this! – span Feb 08 '13 at 15:06
  • It seems you are absolutely correct. Thank you for limiting the scope of my problem. I've posted a new question here if you are interested ;) http://stackoverflow.com/questions/14788002/sending-a-json-object-over-tcp – span Feb 09 '13 at 12:09