HttpClient Get返回200但Post返回404

问题描述 投票:0回答:1

我正在使用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
java apache-httpclient-4.x
1个回答
0
投票

确保服务器的相同URL同时接受POSTGET,这可能是因为服务器端点将POSTGET视为不同的路由并仅接受GET,将404返回到POST请求。所以这不是你的http客户端的问题,而是服务器的问题。

© www.soinside.com 2019 - 2024. All rights reserved.