我正在使用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
确保服务器的相同URL同时接受POST
和GET
,这可能是因为服务器端点将POST
和GET
视为不同的路由并仅接受GET
,将404
返回到POST
请求。所以这不是你的http客户端的问题,而是服务器的问题。