[置顶] android网络通讯之HttpClient4不指定参数名发送Post

时间:2024-11-26 09:36:20

在HttpClient4之前都是通过List<NameValuePair>键值对的形式来向服务器传递参数 ,在4.0版本中在加入了不指定参数名发送数据的形式,利用StringEntity来完成,相当于向一个流写入请求。

HttpClient4中采用ThreadSafeClientConnManager来保证线程的安全,优于2.0的MultiThreadedHttpConnectionManager类。另外Apache官方强烈推荐只使用一个HttpClient的实例,所以我们可以将以下demo方法写成单例模式。可以参考我的博文http://blog.****.net/caicongyang/article/details/9498437

demo将使用StringEntity来完成不指定参数名发送Post,已经采用ThreadSafeClientConnManager来保证线程的安全

   /** 发送报文到GPRS网关 */
public String sendToHttpClientGprs(String url,String content) {
BufferedReader reader = null;
StringBuffer stringBuffer = null;
HttpParams httpParams = new BasicHttpParams();
// 设置超时
ConnManagerParams.setTimeout(httpParams, 5*1000);
HttpConnectionParams.setConnectionTimeout(httpParams, 5*1000);
HttpConnectionParams.setSoTimeout(httpParams, 5*1000); // 多线程最大连接数
ConnManagerParams.setMaxConnectionsPerRoute(httpParams, new ConnPerRouteBean(5));
// 多线程总连接数
ConnManagerParams.setMaxTotalConnections(httpParams, 10);
// 设置异常处理机制
HttpProtocolParams.setUseExpectContinue(httpParams, true);
// 设置是否检查旧连接
HttpConnectionParams.setStaleCheckingEnabled(httpParams, false);
// 设置版本
HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);
// 设置编码
HttpProtocolParams.setContentCharset(httpParams, HTTP.UTF_8);
// 设置重定向
HttpClientParams.setRedirecting(httpParams, false); // 设置userAgent
String userAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2) Gecko/20100115 Firefox/3.6";
HttpProtocolParams.setUserAgent(httpParams, userAgent); //设置是否延迟发送
HttpConnectionParams.setTcpNoDelay(httpParams, true);
//设置缓存大小
HttpConnectionParams.setSocketBufferSize(httpParams, DEFAULT_SOCKET_BUFFER_SIZE); // 支持http与https
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); //ThreadSafeClientConnManager线程安全管理类
ClientConnectionManager manager = new ThreadSafeClientConnManager(httpParams, schemeRegistry);
HttpClient client = new DefaultHttpClient(manager, httpParams);
//post请求
HttpPost httppost = new HttpPost(path);
httppost.setHeader("connection", "Keep-Alive");
httppost.setHeader("Content-Type", "text/plain");
try {
//直接传入发送内容,不指定参数名
StringEntity myEntity = new StringEntity(content, "UTF-8");
httppost.setEntity(myEntity);
// 发送请求
HttpResponse response = client.execute(httppost);
Log.i("ResponseCode", response.getStatusLine().getStatusCode() + "");
// 请求成功
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
stringBuffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
stringBuffer.append(line);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
// 关闭流
if (null != reader) {
reader.close();
reader = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
if (null == stringBuffer) {
return "-1";
} else {
return stringBuffer.toString();
}
}