gpt4 book ai didi

java - httpPost setEntity 始终为空

转载 作者:太空狗 更新时间:2023-10-29 14:17:30 27 4
gpt4 key购买 nike

我正在尝试向 node.js 服务器发帖,但出于某种原因,无论我尝试什么,正文对我来说总是空的。我现在正在对 requestb.in 进行测试,它在那里也总是空的。

这是我用于发布的代码:

public static String post(String url, String json) {
StringBuilder stringBuffer = new StringBuilder();
BufferedReader bufferedReader = null;
try {
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost("http://requestb.in/14a9s7m1");
StringEntity se = new StringEntity("{'string':'string'}", HTTP.UTF_8);
se.setContentType("application/json; charset=UTF-8");
httpPost.setEntity(se);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
httpPost.setHeader("hmac", Methods.getMethods().getHmac(json));

HttpResponse httpResponse = httpClient.execute(httpPost, localContext);
InputStream inputStream = httpResponse.getEntity().getContent();
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

String readLine = bufferedReader.readLine();
while (readLine != null) {
stringBuffer.append(readLine);
stringBuffer.append("\n");
readLine = bufferedReader.readLine();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return stringBuffer.toString();
}

这是请求b.in http://requestb.in/14a9s7m1?inspect原始正文应该包含 json 字符串,对吗?

有什么建议吗?

最佳答案

使用 HttpUrlConnection 时可能会犯很多错误。我承认我没有看到任何错误,但这并不意味着什么。

由于 Google 不建议使用 HttpClient 和 AndroidHttpClient(FROYO 或更早版本除外),但 we should use HttpUrlConnection相反,您的方法是正确的(从 Android 的角度来看)。

当使用名为 DavidWebb 的非常轻量级的 HttpUrlConnection 包装器时,代码如下所示(我省略了 hmac-generation):

public class TestWebbRequestBin {

@Test public void stackOverflow20543115() throws Exception {
Webb webb = Webb.create();
webb.setBaseUri("http://requestb.in");

JSONObject jsonObject = new JSONObject();
jsonObject.put("string", "string");
String json = jsonObject.toString(); // {"string":"string"}

Response<String> response = webb
.post("/1g7afwn1")
.header("Accept", "application/json")
.header("Content-type", "application/json")
.header("hmac", "some-hmac-just-a-test")
.body(json)
.asString();

assertEquals(200, response.getStatusCode());
assertTrue(response.isSuccess());

String body = response.getBody();
assertEquals("ok\n", body);
}

}

当我发布的 JSON 类似于您的示例时,requestb.in 会接受它:

    json = "{'string':'string'}";

但这不是有效的 JSON(这里在 node.js 中测试过):

> JSON.parse("{'string':'string'}")
SyntaxError: Unexpected token '
at Object.parse (native)
at repl:1:7
at REPLServer.self.eval (repl.js:110:21)
at Interface.<anonymous> (repl.js:239:12)

tl;dr

  • 注意发送有效的 JSON
  • 掌握HttpUrlConnection或使用简单的抽象库

对于讨厌的错误,您可以调试您的 node.js 代码(或 console.log(req))或使用类似 Wireshark 的工具.

关于java - httpPost setEntity 始终为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20543115/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com