简体   繁体   中英

Error 404 with Java HttpClient

I am using Java HttpClient package to make http requests. But I am getting 404. When I try the same request with curl, it works fine. Here's the curl request -

curl -i -X POST http://api/endpoint -H "Content-Type: application/json" -d 'content'

Here's the java code that I am using to implement the above curl request -

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);

I see error 404 NOT FOUND when I run this java code. What could the problem be?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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