无法使用Java中的HTTP客户端将JSON发布到服务器

spa*_*pan 5 java post json

我正在尝试将JSON对象发送到自定义端口上的服务器。但是,我从未从服务器返回任何响应。我认为错误可能出在字符串实体中,但我无法弄清楚它是什么。

我看过这些其他问题:

在Java中使用JSON进行HTTP POST

将json发布到Java服务器

如何使用Java使用正确的实体而不使用任何库来构建http发布请求?

如何将Android中的数据以JSON格式发布到服务器?

他们都没有解决我的问题。我正在尝试使用“在Java中使用JSON的HTTP POST”解决方案(16票),但是它不起作用。

这是我的代码:

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();
        }
    }
Run Code Online (Sandbox Code Playgroud)

控制台在开始时会显示“开始”,但是我从“结束”中没有任何输出,也从异常中没有任何堆栈跟踪。调试时,调试器将在“ httpClient.execute(request)”行停止,并且永远不会继续。

当我从终端运行此命令时:

echo '{"id":1, "method":"object.deleteAll", "params":["subscriber"]} ' | nc x.x.x.x 3994
Run Code Online (Sandbox Code Playgroud)

一切正常执行,服务器收到我的请求。

我认为我的问题可能出在,StringEntity但我不确定。

编辑:

使用Wireshark,我能够捕获此发送到服务器的数据包:

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"]}
Run Code Online (Sandbox Code Playgroud)

并且该数据包从服务器返回:

{"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"}}
Run Code Online (Sandbox Code Playgroud)

cmo*_*key 4

在您的工作示例中,您使用 nc,这意味着您直接写入 TCP,而不是使用 HTTP,对吗?

因此,您使用 HTTPClient 的 HTTP 调用正在发送意外数据。这些错误表明 HTTP 包装器是有问题的。

“POST”中的意外字符“P”、“Content-Type”中的“C”等。服务器正在读取每一行,期望得到原始 JSON 字符串,但得到的是 HTTP。

解决方案:您可能想使用原始Java 套接字