Java HttpClient出现错误404
我正在使用JavaHttpClient包进行http请求。但是我得到了404。当我使用curl尝试相同的请求时,效果很好。这是卷曲要求-
curl -i -X POST http://api/endpoint -H "Content-Type: application/json" -d 'content'
下面是我用来实现上述curl请求的java代码-
HttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost("http://api/endpoint");
post.setHeader("Content-type", "application/json");
post.setEntity(new StringEntity(content));
HttpResponse response = client.execute(post);
logger.info("Response Code : "
+ response.getStatusLine().getStatusCode());
BufferedReader rd = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
logger.info("Response details "+result);
运行此java代码时,我发现错误404未找到。有什么问题吗
# 1 楼答案
移除帖子。setHeader(“接受”、“应用程序/json”)