[英]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.