gpt4 book ai didi

Java Put-Request 返回 400 错误请求

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

我尝试对我编写的rest-api执行put-request,但它总是返回400-Bad Request。

java.io.IOException: Server returned HTTP response code: 400 for URL: http://localhost:8000/api/v0/contacts/2 at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)

我可以使用 firefox-rest 客户端插件以及另一个 Angular 客户端来调用该 url,所以我想我在 java 代码中配置了一些错误。

服务器总是返回一些东西,所以我不知道为什么当我尝试获取输入流时它会崩溃。

protected String put(String path, String parameters)
throws IOException {

HttpURLConnection connection = getConnection(path, "PUT", parameters);

OutputStreamWriter outputWriter = new OutputStreamWriter(
connection.getOutputStream());

outputWriter.write(parameters);
outputWriter.flush();
outputWriter.close();

InputStream is = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String response = reader.readLine();
while(response != null){
response += reader.readLine();
}
reader.close();

return response;
}


private HttpURLConnection getConnection(String path, String method,String params) throws MalformedURLException, IOException,
ProtocolException {
URL url = new URL(this.api + path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
connection.setRequestMethod(method);
connection.setRequestProperty("Content-Length",
String.valueOf(params.length()));
return connection;
}

抱歉英语不好,这不是我的母语。

最佳答案

我在cmd提示符下执行了ipconfig,它显示了192.168.1.3。我在 Android 应用程序中的 localhost 位置更改了它,现在我可以访问本地 Web 服务器了。

关于Java Put-Request 返回 400 错误请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29895259/

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