为什么HTTP POST返回代码400(错误的请求)? HTTP POST方法 [英] Why HTTP POST returns code 400 (bad request)? HTTP POST Method
本文介绍了为什么HTTP POST返回代码400(错误的请求)? HTTP POST方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
URL serverAddress = new URL(uri);
HttpURLConnection连接=(HttpURLConnection)serverAddress.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestProperty(Content-Type,contentType);
connection.setRequestMethod(POST);
int status = connection.getResponseCode(); //返回400
例如,这个HTTP GET返回代码200:(works)
/ **创建连接** /
URL serverAddress = new URL(uri);
HttpURLConnection连接=(HttpURLConnection)serverAddress.openConnection();
connection.setRequestProperty(Content-Type,contentType);
connection.setRequestMethod(GET);
connection.setDoOutput(false);
int status = connection.getResponseCode(); //返回200
解决方案
在写入流并关闭流( writer.write()和 os.close())之前调用(connection.getResponseCode()) 这就是为什么服务器返回错误的请求代码(400)。
这是我现在可以使用的代码:
/ **创建连接** /
URL serverAddress =新的URL(uri);
HttpURLConnection连接=(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(); //在执行HTTP POST时数据流准备就绪之前,无法调用
PrinterClass.show(status); (!状态= 200)//状态
如果
掷(新RESTfulWebServiceException( 无效的HTTP响应状态
+ 代码 +状态+ 从Web服务的服务器。));
private String getQuery()throws UnsupportedEncodingException
{
JSONObject jobj = new JSONObject();
jobj.put(customerNumber,新的JSONString(003));
jobj.put(mappingCode,new JSONString(jac_003));
jobj.put(name,新的JSONString(johnny));
jobj.put(status,新的JSONString(ACTIVE));
PrinterClass.show(jobj.toString());
return jobj.toString();
}
Why server returns response code 400(bad request)? (doesn't work)
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)
/** 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
解决方案
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:
/** 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();
}
这篇关于为什么HTTP POST返回代码400(错误的请求)? HTTP POST方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文