无法使用Java中的HTTP客户端将JSON发布到服务器 [英] Can't post JSON to server with HTTP Client in Java

查看:437
本文介绍了无法使用Java中的HTTP客户端将JSON发布到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

使用Java中的JSON HTTP POST

HTTP POST using JSON in Java

将json发布到java服务器

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

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

他们都没有解决我的问题。我正在尝试使用使用Java中的JSON进行HTTP POST的解决方案(16票),但它无法正常工作。

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.

这是我的代码:

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();
        }
    }

控制台在开始时打印开始,但我永远不会从结束获得任何输出,也不会从异常中获得任何堆栈跟踪。当调试调试器停在 httpClient.execute(request)行并且永远不会继续。

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.

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

I think my problem might be with the StringEntity but I'm not sure.

编辑:

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

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"}}


推荐答案

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

In your working example, you use nc, which means you are writing directly to TCP, not using HTTP, correct?

因此,使用HTTPClient进行HTTP调用会发送意外数据。
错误表明HTTP包装器存在问题。

Therefore, your HTTP invocation using HTTPClient is sending unexpected data. The errors indicate that the HTTP wrapper is what's at issue.

POST中出现意外字符P,Content-Type中出现C等等。服务器正在读取每一行,期望原始JSON字符串,但正在获取HTTP。

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.

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

Solution: You probably want to use a raw Java socket instead.

这篇关于无法使用Java中的HTTP客户端将JSON发布到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆