HttpClient Get返回200,但Post返回404

时间:2019-03-27 01:44:23

标签: java apache-httpclient-4.x

我正在使用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

1 个答案:

答案 0 :(得分:0)

确保服务器的相同URL接受POSTGET,这可能是因为服务器端点将POSTGET视为不同的路由,并且仅接受{ {1}},将GET返回到您的404请求。 因此,这不是您的http客户端的问题,而是服务器的问题。