connection.setRequestProperty("Content-type", "application/x-www-form-urlenpred");
postData。
application/x-www-form-urlenpred仅当内容类型实际经过url编码时,才需要内容类型。例如,您对POST数据进行如下编码:
String data = "param1=" + URLEnprer.enpre(param1, "UTF-8")+ "¶m2=" + URLEnprer.enpre(param2, "UTF-8");
这样,另一端将能够根据指定的格式解析数据而不会破坏数据。
和,
connection.setRequestProperty("Content-Length", Integer.toString(postData.length));
这对于确保可靠的数据传输是优选的。如果您忽略此设置,并且连接以某种方式断开,则另一端将永远无法确定内容是否已完全流式传输。
就是说,
HttpUrlConnection如果您知道将请求方法“自动”设置为以下事实,则无需强制转换为
POST:
connection.setDoOutput(true);
或者您更适合:
connection.setDoOutput("POST".equals(method));
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)