繁体   English   中英

Java HttpClient出现错误404

[英]Error 404 with Java HttpClient

我正在使用Java HttpClient程序包发出http请求。 但是我得到404。当我尝试使用curl进行相同的请求时,它工作正常。 这是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未找到。 可能是什么问题?

删除post.setHeader(“ Accept”,“ application / json”);

暂无
暂无

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

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