简体   繁体   English

使用HttpURLConnection的JAVA中的POST获得HTTP响应代码400

[英]POST in JAVA using HttpURLConnection getting a HTTP response code 400

I'm pretty new to making HTTP connections and working with API's in Java, so I'm not sure where the problem lies. 我在建立HTTP连接和使用Java中的API方面还很陌生,所以我不确定问题出在哪里。 When I send out a POST connection request in order to send a JSON formatted String of text to the other side, I get an error back along with a 400 response code. 当我发送POST连接请求以将JSON格式的文本字符串发送到另一端时,我收到一条错误以及400响应代码。 When I look up that code, it seems my connection isn't properly formatted. 当我查找该代码时,似乎我的连接格式不正确。 Code is below, along with the error message. 下面的代码以及错误消息。 Please help! 请帮忙! Thanks! 谢谢!

public void sendToAPI(String urlPass, String param) throws IOException {

    URL url = new URL(urlPass);
    HttpURLConnection connectionOut = (HttpURLConnection) url.openConnection();
    connectionOut.setRequestMethod("POST");
    connectionOut.setConnectTimeout(5000);
    connectionOut.setReadTimeout(5000);
    connectionOut.setRequestProperty("Content-Type", "application/json");
    connectionOut.setRequestProperty("Content-Length", Integer.toString(param.length()));
    connectionOut.setDoOutput(true);
    connectionOut.setDoInput(true);
    connectionOut.connect();

    DataOutputStream stream = new DataOutputStream(connectionOut.getOutputStream());
    stream.writeUTF(param);
    stream.flush();
    stream.close();

    int responsecode = connectionOut.getResponseCode();
    if(responsecode != 200) {
        System.out.println("Response Code is " + responsecode);             
    }

    BufferedReader in = new BufferedReader(
            new InputStreamReader(connectionOut.getInputStream()));
    String output;
    StringBuffer response = new StringBuffer();

    while ((output = in.readLine()) != null) {
        response.append(output);
    }
    in.close();

    //printing result from response
    System.out.println(response.toString());

}

Response Code is 400 Exception in thread "main" java.io.IOException : Server returned HTTP response code: 400 for URL:XXX 响应代码为 “线程”“ main”中的400异常java.io.IOException :服务器返回的HTTP响应代码:400:URL:XXX

So after playing around with the DataOutputStream , I replaced the below code: 因此,在DataOutputStream ,我替换了以下代码:

DataOutputStream stream = new DataOutputStream(connectionOut.getOutputStream());
stream.writeUTF(param);

With another example I found online: 我在网上找到了另一个例子:

OutputStream os = connectionOut.getOutputStream();
os.write(param.getBytes());
os.flush();
os.close();

I'm not sure yet why, but this suddenly got the proper response code I was looking for, so the format it was sent in matched what they requested. 我不确定为什么,但是这突然得到了我想要的正确的响应代码,因此发送的格式与他们的要求相符。 Thanks for all responses. 感谢您的所有回复。

u can try this code: 您可以尝试以下代码:

InputStream inputStream;
if (responseCode == 200) {
    inputStream = con.getInputStream();
} else {
    inputStream = con.getErrorStream();
}

BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder builder = new StringBuilder();
String lines;
while ((lines = reader.readLine()) != null) {
    builder.append(lines);
    builder.append(System.getProperty("line.separator"));
}

String retStr = builder.toString().trim();
reader.close();

System.out.println("retStr: " + retStr);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在REST API中使用HttpURLConnection到Java中的POST时,响应代码为400 - Response code is 400 while using HttpURLConnection to POST in java for REST API HttpURLConnection:获取响应代码400 - HttpURLConnection: Getting Response code 400 Java - 从 HttpURLConnection 读取 POST 抛出响应代码 400 - Java - Reading POST from HttpURLConnection throwing response code 400 服务器返回的HTTP响应代码:400表示URL HttpURLConnection.getInputStream表示JSON POST数据字符串 - Server returned HTTP response code: 400 for URL HttpURLConnection.getInputStream for JSON POST data string HttpURLConnection错误-java.io.IOException:服务器返回的HTTP响应代码:URL的400 - HttpURLConnection Error - java.io.IOException: Server returned HTTP response code: 400 for URL 服务器返回 HTTP 响应代码:400 用于在 java httpurlconnection 中生成 oauth 令牌 - Server returned HTTP response code: 400 for oauth token generation in java httpurlconnection HttpURLConnection 在 POST 上失败,HTTP 400 - HttpURLConnection failing on POST with HTTP 400 服务器使用 httpurlconnection 发出 http 头请求时响应代码 400 - Server responding with response code 400 when making http head request using httpurlconnection 服务器返回 HTTP 响应代码:400:模拟 HttpURLConnection 时 - Server returned HTTP response code: 400 : When mocking HttpURLConnection 服务器返回URL的HTTP响应代码:400:HttpURLConnection错误 - Server returned HTTP response code: 400 for URL : HttpURLConnection Error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM