HTTP Client工具类:




import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
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.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class HTTPSample {

private static CloseableHttpClient httpClient = null;

public static CloseableHttpClient httpClient(){
if(httpClient!=null) return HTTPSample.httpClient;
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
// Increase max total connection to 200
cm.setMaxTotal(200);
// Increase default max connection per route to 20
cm.setDefaultMaxPerRoute(20);
HTTPSample.httpClient = HttpClients.custom().setConnectionManager(cm).build();
return httpClient;
}

public static String httpGet(String url, Map<String, String> requestParams) {

HttpGet httpGet = null;
String result = "";
try {
// ��������
StringBuilder builder = new StringBuilder(url);
builder.append("?");
for (Map.Entry<String, String> entry : requestParams.entrySet()) {
builder.append((String) entry.getKey());
builder.append("=");
builder.append((String) entry.getValue());
builder.append("&");
}

String tmpUrl = builder.toString();
tmpUrl = tmpUrl.substring(0, tmpUrl.length() - 1);

httpGet = new HttpGet(tmpUrl);

// System.out.println("executing request " + httpGet.getURI());
// System.out.println("-------------------------------------");

HttpResponse response = httpClient().execute(httpGet);

// reponse header
// System.out.println(response.getStatusLine().getStatusCode());

Header[] headers = response.getAllHeaders();
// for (Header header : headers) {
// System.out.println(header.getName() + ": " + header.getValue());
// }

// System.out.println();

// ��ҳ����
HttpEntity httpEntity = response.getEntity();
// System.out.println(EntityUtils.toString(httpEntity));
result = EntityUtils.toString(httpEntity);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (httpGet != null) {
httpGet.abort();
}
}
return result;
}

public static String httpPost(String url, Map<String, String> requestParams, String urlEncode) {

HttpPost httpPost = null;
String result = "";
try {
// ��������
List<NameValuePair> params = new ArrayList<NameValuePair>();
for (Map.Entry<String, String> entry : requestParams.entrySet()) {
params.add(new BasicNameValuePair((String) entry.getKey(),
(String) entry.getValue()));
}

httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params, urlEncode));

// System.out.println("executing request " + httpPost.getURI());
// System.out.println("-------------------------------------");

// reponse header
HttpResponse response = httpClient().execute(httpPost);
// System.out.println(response.getStatusLine().getStatusCode());

Header[] headers = response.getAllHeaders();
// for (Header header : headers) {
// System.out.println(header.getName() + ": " + header.getValue());
// }

// System.out.println();

// ��ҳ����
HttpEntity httpEntity = response.getEntity();
result = EntityUtils.toString(httpEntity);

} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (httpPost != null) {
httpPost.abort();
}
}
return result;
}
}
posted on 2016-07-01 16:17  九重霄  阅读(268)  评论(0编辑  收藏  举报