HttpURLConnection代码
……
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod(
"POST");
conn.setDoOutput(
true);
conn.setDoInput(
true);
conn.setUseCaches(
false);
conn.setRequestProperty(
"Content-Type",
"application/x-www-form-urlencoded;charset=UTF-8");
conn.setRequestProperty(
"Connection",
"Keep-Alive");
conn.setRequestProperty(
"Charset",
"UTF-8");
conn.setConnectTimeout(
30000);
conn.setReadTimeout(
30000);
conn.connect();
DataOutputStream dos =
new DataOutputStream(
this.conn.getOutputStream());
if (!HttpParam.isEmpty()) {
for (Object o : HttpParam.entrySet()) {
Map.Entry entry = (Map.Entry) o;
String param = URLEncoder.encode((String) entry.getKey(),
"UTF-8") +
"="+ URLEncoder.encode((String) entry.getValue(),
"UTF-8");
dos.writeBytes(param+
"&");
}
}
dos.flush();
dos.close();
……
记要
1.请求应放置于子线程中执行 2.请求有开始语句就应该有结束语句 3.post 请求如果不加分割符号 只会识别一个参数
转载请注明原文地址: https://ju.6miu.com/read-237.html