Java Put-Request returns 400 Bad Request



我尝试针对我编写的 rest-api 执行放置请求,但它总是返回 400-Bad Request。

java.io.IOException:服务器返回 HTTP 响应代码:400 对于 URL:http://localhost:8000/api/v0/contacts/2 at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)

我可以使用 firefox-rest 客户端插件以及另一个角度客户端调用 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 应用程序中代替本地主机更改了它,现在我可以访问本地网络服务器。

最新更新