1. 程式人生 > >工具類一(HttpUtils篇)

工具類一(HttpUtils篇)

把專案里老大寫的好用的工具類分享一下。覺得好用的拿去。

import java.util.ArrayList;

import java.util.List;

import java.util.Map;

import org.apache.http.HttpEntity;

import org.apache.http.NameValuePair;

import org.apache.http.client.config.RequestConfig;

import org.apache.http.client.entity.UrlEncodedFormEntity;

import

org.apache.http.client.methods.CloseableHttpResponse;

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.HttpClientBuilder;

import org.apache.http.message.BasicNameValuePair;

import org.apache.http.util.EntityUtils;

public class HttpUtilsTest {

//生成httpClient

private static final CloseableHttpClient httpClient;

publicstaticfinal StringCHAESET = "utf-8";

static {

RequestConfig config = RequestConfig.custom()

.setConnectTimeout(6000) //請求超時時間

.setSocketTimeout(6000) //響應超時時間

.build();

httpClient = HttpClientBuilder.create()

.setDefaultRequestConfig(config)

.build();

}

public static String httpGet(String url) throws Exception {

return httpGet(url,null);

}

public static String httpGet(String url, Map<String, String>params) throws Exception {

return httpGet(url,params, CHAESET);

}

/**

* @param url

* @param params

* @param charset

* @return

* @throws Exception

*/

public static String httpGet(String url, Map<String, String>params, String charset)throws Exception {

//構造url

if(params !=null && !params.isEmpty()) {

List<NameValuePair> pairs = new ArrayList<NameValuePair>();

for(Map.Entry<String, String>entry : params.entrySet()) {

if(entry.getValue() !=null) {

pairs.add(new BasicNameValuePair(entry.getKey(),entry.getValue()));

}

}

String queryString = EntityUtils.toString(new UrlEncodedFormEntity(pairs),charset); 

if(url.indexOf("?") != -1) {

url +="&" + queryString;

}else {

url +="?" + queryString;

}

}

//httpGet獲取請求,返回資料

HttpGet httpGet =new HttpGet(url);

CloseableHttpResponse response = httpClient.execute(httpGet);

try{

int statusCode = response.getStatusLine().getStatusCode();

if(statusCode != 200) {

httpGet.abort();

thrownew RuntimeException("httpClient: error status code:" +statusCode);

}

HttpEntity entity =response.getEntity();

String result =null;

if(entity !=null) {

result = EntityUtils.toString(entity,charset);

}

EntityUtils.consume(entity);

returnresult;

}finally {

response.close();

}

}

public static String httpPost(String url, HttpEntityrequestEntity) throws Exception {

return httpPost(url,null, requestEntity);

}

public static String httpPost(String url, Map<String, String>params, HttpEntity requsetEntity) throws Exception {

//構建url

if(params !=null && !params.isEmpty()) {

List<NameValuePair> pairs = new ArrayList<NameValuePair>();

for(Map.Entry<String, String>entry : params.entrySet()) {

if(entry.getValue() !=null) {

pairs.add(new BasicNameValuePair(entry.getKey(),entry.getValue()));

}

}

String queryString = EntityUtils.toString(new UrlEncodedFormEntity(pairs),CHAESET);

if(url.indexOf("?") > 0) {

url +="&" + queryString;

}else {

url +="?" + queryString;

}

}

//httpPost請求獲取資料

HttpPost httpPost =new HttpPost(url);

httpPost.setEntity(requsetEntity);

CloseableHttpResponse response = httpClient.execute(httpPost);

try{

int statusCode = response.getStatusLine().getStatusCode();

if(statusCode != 200) {

httpPost.abort();

thrownew RuntimeException("httpClient error status code:" +statusCode);

}

HttpEntity entity =response.getEntity();

String result =null;

if(entity !=null) {

result = EntityUtils.toString(entity,CHAESET);

}

EntityUtils.consume(entity);

returnresult;

}finally {

response.close();

}

}

}