【发布时间】:2019-03-27 12:42:35
【问题描述】:
我正在使用 apache http 客户端对同一端点进行 Get 和 Post 调用。 Get 返回 200 OK,但 Post 返回 404 Not Found。有任何想法吗?我的设置如下:
HttpClient client = HttpClients.createDefault();
String key = "foo"
HttpGet httpGet = new HttpGet("https://url");
HttpResponse response1 = client.execute(httpGet);
System.out.println(response1.getStatusLine());
HttpEntity entity1 = response1.getEntity();
EntityUtils.consume(entity1);
// Returns 200 OK
String bundle = "{\"foo\":\"bar\"}";
HttpPost httpPost = new HttpPost("https://url");
StringEntity requestEntity = new StringEntity(
bundle,
"application/json",
"UTF-8");
httpPost.setEntity(requestEntity);
HttpResponse response2 = client.execute(httpPost);
System.out.println(response2.getStatusLine());
HttpEntity entity2 = response2.getEntity();
EntityUtils.consume(entity2);
// Returns 404 Not Found
【问题讨论】:
-
你能用 Postman 之类的工具发帖吗?
-
是的,我可以使用 Postman 发帖。
-
您是否对 Postman 发送的有效负载与您的代码发送的有效负载进行了并排比较,以查看差异是什么以及是否可以发现任何明显的东西?
-
我认为它与请求正文无关,即使我将正文设置为“{}”,我也会得到 404。
-
整个 HTTP 请求 - 请求行、标头和正文。
标签: java apache-httpclient-4.x