httpclient 4.0.1 实例
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.conn.params.ConnPerRouteBean;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
public class HttpClientUtils implements Serializable {
/**
*
*/
private static final long serialVersionUID = -7658119265770743448L;
private static HttpClient httpClient;
private static Log log = LogFactory.getLog(HttpClientUtils.class);
static {
ThreadSafeClientConnManager tscc = null;
SchemeRegistry schemeregistry = new SchemeRegistry();
PlainSocketFactory plainsocketfactory = PlainSocketFactory
.getSocketFactory();
Scheme scheme = new Scheme("http", plainsocketfactory, 8080);
schemeregistry.register(scheme);
SSLSocketFactory sslsocketfactory = SSLSocketFactory.getSocketFactory();
Scheme scheme1 = new Scheme("https", sslsocketfactory, 443);
schemeregistry.register(scheme1);
BasicHttpParams basichttpparams = new BasicHttpParams();
ConnManagerParams.setMaxTotalConnections(basichttpparams, 500);
ConnManagerParams.setTimeout(basichttpparams, 12000L);
ConnManagerParams.setMaxConnectionsPerRoute(basichttpparams, new ConnPerRouteBean(400));
HttpConnectionParams.setSoTimeout(basichttpparams, 60000);
HttpConnectionParams.setSocketBufferSize(basichttpparams, 20480);
HttpConnectionParams.setConnectionTimeout(basichttpparams, 20000);
basichttpparams.setBooleanParameter(
"http.protocol.single-cookie-header", true);
tscc = new ThreadSafeClientConnManager(basichttpparams, schemeregistry);
ThreadSafeClientConnManager threadsafeclientconnmanager = tscc;
httpClient = new DefaultHttpClient(threadsafeclientconnmanager,
basichttpparams);
}
/**
* 以get方式提交表单
*
* @param url
* 服务器路径
* @return 响应结果
* @throws Exception
*/
public static String doGet(String url) throws Exception {
HttpGet get = new HttpGet(url);
InputStream in = null;
BufferedReader br = null;
StringBuffer sb = new StringBuffer();
try{
HttpResponse response = httpClient.execute(get);
/** 返回状态 **/
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
HttpEntity entity = response.getEntity();
if (entity != null) {
String tempLine;
in = entity.getContent();
br = new BufferedReader(new InputStreamReader(
in));
while ((tempLine = br.readLine()) != null) {
sb.append(tempLine);
}
entity.consumeContent();
}
}else{
if(get!=null&& (!(get.isAborted())))
get.abort();
return null;
}
}catch(Exception e){
if(get!=null&& (!(get.isAborted())))
get.abort();
e.printStackTrace();
return null;
}finally{//2013-11-12修改,增加流的关闭,in.close()它会触发一个连接的释放这个连接将重新被连接管理器收回
closeCon(in,br,null,get);
}
return sb.toString();
}
/**
* 以POST方式提交表单
*
* @param url
* 服务器路径
* @param param
* 参数键值对
* @return 响应结果
* @throws Exception
*/
public static String doPost(String url, Map<String, Object> param)
throws Exception {
log.info("do posting ------param参数个数:"+param.size());
HttpPost post = new HttpPost(url);
StringBuffer sb = new StringBuffer();
InputStream in = null;
try{
if (param != null && param.size() > 0) {
List<BasicNameValuePair> nameValuePairs = new ArrayList<BasicNameValuePair>(
param.size());
Set<String> keys = param.keySet();
for (Object o : keys) {
String key = (String) o;
nameValuePairs.add(new BasicNameValuePair(key, String
.valueOf(param.get(key))));
}
post.setEntity(new UrlEncodedFormEntity(nameValuePairs, "utf-8"));
}
HttpResponse response = httpClient.execute(post);
/** 返回状态 **/
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
HttpEntity entity = response.getEntity();
if (entity != null) {
in = entity.getContent();
BufferedReader br = new BufferedReader(
new InputStreamReader(in, "utf-8"));
String tempLine;
while ((tempLine = br.readLine()) != null) {
sb.append(tempLine);
}
entity.consumeContent();
}
}else{
if(post!=null&& (!(post.isAborted())))
post.abort();
}
}catch(Exception e){
e.printStackTrace();
if(post!=null&& (!(post.isAborted())))
post.abort();
}finally{
closeCon(in,null,post,null);//2013-11-12修改,增加流的关闭,in.close()它会触发一个连接的释放这个连接将重新被连接管理器收回
}
// String str=new String(sb.toString().getBytes(),"UTF-8");
return sb.toString();
}
/**
* 提交参数里有文件的数据
*
* @param url
* 服务器地址
* @param param
* 参数
* @return 服务器返回结果
* @throws Exception
*/
public static String uploadSubmit(String url, Map<String, String> param,
File file) throws Exception {
HttpPost post = new HttpPost(url);
InputStream in = null;
BufferedReader br = null;
StringBuffer sb = new StringBuffer();
try{
MultipartEntity entity = new MultipartEntity();
if (param != null && !param.isEmpty()) {
for (Map.Entry<String, String> entry : param.entrySet()) {
entity.addPart(entry.getKey(), new StringBody(entry.getValue()));
}
}
// 添加文件参数
if (file != null && file.exists()) {
entity.addPart("file", new FileBody(file));
}
post.setEntity(entity);
HttpResponse response = httpClient.execute(post);
int stateCode = response.getStatusLine().getStatusCode();
if (stateCode == HttpStatus.SC_OK) {
HttpEntity result = response.getEntity();
if (result != null) {
in = result.getContent();
br = new BufferedReader(
new InputStreamReader(in));
String tempLine;
while ((tempLine = br.readLine()) != null) {
sb.append(tempLine);
sb.append("\r\n");
}
result.consumeContent();
}
}else{
if (post!=null&& (!(post.isAborted())))
post.abort();
}
}catch(Exception e){
e.printStackTrace();
if (post!=null&& (!(post.isAborted())))
post.abort();
}finally{
closeCon(in,br,post,null);//2013-11-12修改,增加流的关闭,in.close()它会触发一个连接的释放这个连接将重新被连接管理器收回
}
return sb.toString();
}
/**
* exportFile 下载文件
*
* @param url
* 服务器路径
* @param param
* 参数键值对
* @param srcUrl
* 文件下载地址
* @param filePath
* 文件存放地址
* @return 响应结果
* @throws Exception
*/
public static synchronized void exportFile(String url, Map<String, Object> param,String fileName)
throws Exception {
FileOutputStream out =null;
HttpPost post = new HttpPost(url);
InputStream in = null;
try {
//文件名和文件流定义
File wdFile = new File(fileName);
System.out.println("filepath :"+fileName);
out = new FileOutputStream(wdFile);
if (param != null && param.size() > 0) {
List<BasicNameValuePair> nameValuePairs = new ArrayList<BasicNameValuePair>(
param.size());
Set<String> keys = param.keySet();
for (Object o : keys) {
String key = (String) o;
nameValuePairs.add(new BasicNameValuePair(key, String
.valueOf(param.get(key))));
}
post.setEntity(new UrlEncodedFormEntity(nameValuePairs, "utf-8"));
}
HttpResponse response = httpClient.execute(post);
/** 返回状态 **/
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
HttpEntity entity = response.getEntity();
if (entity != null) {
in = entity.getContent();
int l;
byte[] tmp = new byte[2048];
while ((l = in.read(tmp)) != -1) {
out.write(tmp, 0, l);
}
entity.consumeContent();
}
}else{
if(post!=null&& (!(post.isAborted())))
post.abort();
}
} catch (Exception e) {
e.printStackTrace();
if(post!=null&& (!(post.isAborted())))
post.abort();
if (out != null)
try {
out.close();
} catch (IOException ee) {
ee.printStackTrace();
}
}finally{
closeCon(in, null, post, null);//2013-11-12修改,增加流的关闭,in.close()它会触发一个连接的释放这个连接将重新被连接管理器收回
if(out!=null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static HttpClient getHttpClientInstance() {
return httpClient;
}
public static void closeCon(InputStream in,BufferedReader br,HttpPost post,HttpGet get){
if (post != null&& (!(post.isAborted()))){
try
{
post.abort();
}
catch (Exception e)
{
e.printStackTrace ();
}
}//post != null
if (get != null&& (!(get.isAborted()))){
try
{
get.abort();
}
catch (Exception e)
{
e.printStackTrace ();
}
}//get != null
if (in != null){
try
{
in.close ();
}
catch (IOException e)
{
e.printStackTrace ();
}
}//in != null
if (br != null){
try
{
br.close ();
}
catch (IOException e)
{
e.printStackTrace ();
}
}//br != null
}
}