繁体   English   中英

HttpClient Get返回200但Post返回404

[英]HttpClient Get returns 200 but Post returns 404

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM