Java 为什么 HTTP POST 返回代码 400(错误请求)?HTTP POST 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29262364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why HTTP POST returns code 400 (bad request)? HTTP POST Method
提问by Jacob
Why server returns response code 400(bad request)? (doesn't work)
为什么服务器返回响应代码 400(错误请求)?(不起作用)
URL serverAddress = new URL(uri);
HttpURLConnection connection = (HttpURLConnection) serverAddress.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestProperty("Content-Type", contentType);
connection.setRequestMethod("POST");
int status = connection.getResponseCode(); // returns 400
For example this HTTP GET returns code 200: (works)
例如,这个 HTTP GET 返回代码 200: (有效)
/** Creating Connection **/
URL serverAddress = new URL(uri);
HttpURLConnection connection = (HttpURLConnection) serverAddress.openConnection();
connection.setRequestProperty("Content-Type", contentType);
connection.setRequestMethod("GET");
connection.setDoOutput(false);
int status = connection.getResponseCode(); // returns 200
采纳答案by Jacob
I've tried to get response code ( connection.getResponseCode() )before I did actually wrote to a stream and close the stream ( writer.write()and os.close() )thats why server returned Bad Request code(400). This is my code which works now:
在我实际写入流并关闭流(writer.write()和os.close() )之前,我已经尝试获取响应代码( connection.getResponseCode() ), 这就是服务器返回错误请求代码(400)的原因. 这是我现在工作的代码:
/** Creating Connection **/
URL serverAddress = new URL(uri);
HttpURLConnection connection = (HttpURLConnection) serverAddress.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestProperty("Content-Type", contentType);
connection.setRequestMethod("POST");
/** POSTing **/
OutputStream os = connection.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(getQuery());
writer.flush();
writer.close();
os.close();
connection.connect();
int status = connection.getResponseCode();//this cannot be invoked before data stream is ready when performing HTTP POST
PrinterClass.show(status); //status
if (status != 200)
throw (new RESTfulWebServiceException("Invalid HTTP response status "
+ "code " + status + " from web service server."));
private String getQuery() throws UnsupportedEncodingException
{
JSONObject jobj = new JSONObject();
jobj.put("customerNumber", new JSONString("003"));
jobj.put("mappingCode", new JSONString("jac_003"));
jobj.put("name", new JSONString("johnny"));
jobj.put("status", new JSONString("ACTIVE"));
PrinterClass.show(jobj.toString());
return jobj.toString();
}
回答by gtklocker
Error 400 means Bad Request. You don't show us which URI you're trying to request, however it's quite possible that the URI accepts only GET requests and doesn't have a way to respond to a POST request, hence it throws a 400 error. For example, requesting a page that shows a listing of photos with GET makes sense, but making a POST request to said page makes no sense at all.
错误 400表示错误请求。您没有向我们显示您尝试请求的 URI,但是 URI 很可能只接受 GET 请求并且无法响应 POST 请求,因此它会引发 400 错误。例如,使用 GET 请求显示照片列表的页面是有意义的,但对所述页面发出 POST 请求则毫无意义。
Another possibility is that you may be providing an incorrect content type. Usually, when making POST requests you want to use application/x-www-form-urlencoded
, which is the content type that's used when you submit any web form on the internet. Furthermore, you may not be providing the form data that the server needs. Imagine a scenario where you try to post a message on Facebook but you've provided no message. The server will rightfully dismiss your empty request.
另一种可能性是您提供的内容类型可能不正确。通常,在发出 POST 请求时,您想使用application/x-www-form-urlencoded
,这是您在 Internet 上提交任何 Web 表单时使用的内容类型。此外,您可能没有提供服务器所需的表单数据。想象一个场景,您尝试在 Facebook 上发布一条消息,但您没有提供任何消息。服务器将理所当然地驳回您的空请求。
If you provide us with the request URI and content type you are using we can help you further in debugging this.
如果您向我们提供您正在使用的请求 URI 和内容类型,我们可以帮助您进一步调试。
回答by thst
look into the documentation for the partner call. The get operation shows all partners, the post operation requires a body to be set. you dont send the body in your code, thus you send a bad request.
查看合作伙伴电话的文档。get 操作显示所有伙伴,post 操作需要设置一个主体。您没有在代码中发送正文,因此您发送了错误的请求。
见这里:http: //nwb.sys.stage-cf-billing.swisslab.io/com.swisscom.nwb.cf.api/doc/swagger/index.html#!/Partner/GETPartnervs
回答by M. Usman Khan
In my case, there was a spacein my URL string. I replaced it with %20 and it worked.
就我而言,我的 URL 字符串中有一个空格。我用 %20 替换它并且它起作用了。
requestUrl = requestUrl.replace(" ", "%20");