gpt4 book ai didi

java - 服务器返回 java.io.IOException : Server returned HTTP response code: 400 for URL

转载 作者:太空宇宙 更新时间:2023-11-04 09:08:31 28 4
gpt4 key购买 nike

我正在尝试调用rest API,其记录如下

curl -X POST "https://url/api/v1/className/doSomething" -H "accept: application/json" -H "Authorization: Bearer token" -H "Content-Type: application/json" -d

这是我的代码发送http post。它适用于我之前使用过的所有 http api,但不适用于此。

        String result = "";

RequestProcess process = LogManager.getInstance().newProcess();
process.setProcessName(url);
process.setRequestContent(requestContent);

ObjectMapper mapper = new ObjectMapper().registerModule(new Jdk8Module()).registerModule(new JavaTimeModule());


String rawData = mapper.writeValueAsString(requestContent);

String charset = "UTF-8";
HttpURLConnection connection = (HttpURLConnection)new URL(url).openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Accept-Charset", charset);
connection.setRequestProperty("Content-Type", "application/json;charset=" + charset);
connection.setRequestProperty("User-Agent", USER_AGENT);
connection.setRequestMethod("POST");

try (OutputStream output = connection.getOutputStream()) {
output.write(rawData.getBytes(charset));
}

InputStream inputStream = connection.getInputStream();
result = StreamUtil.toString(inputStream);

process.setResponseContent(result);
LogManager.getInstance().endProcess(process);

return result;

我收到错误

java.io.IOException:服务器返回 HTTP 响应代码:400 对于 URL:url

我尝试从 postman 那里做同样的事情,它有效。有什么想法吗?

最佳答案

您发送什么数据?至少你应该发送 -d {} 来获取空数据或删除 -d

关于java - 服务器返回 java.io.IOException : Server returned HTTP response code: 400 for URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59933306/

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