gpt4 book ai didi

java - Java HttpClient 错误 404

转载 作者:行者123 更新时间:2023-12-05 06:42:08 25 4
gpt4 key购买 nike

我正在使用 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 NOT FOUND。可能是什么问题?

最佳答案

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

关于java - Java HttpClient 错误 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38339114/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com