HttpClientUtil

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.net.ssl.HttpsURLConnection;

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.config.RequestConfig;
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.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;



public class HttpClientUtil {

	
	/**
	 * GET请求
	 * @param url	请求地址
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static String httpGet(String url) throws ClientProtocolException, IOException{
		return httpGet(url,30000,30000);
	}

	/**
	 * GET请求
	 * @param url	请求地址
	 * @param socketTimeout		读取超时
	 * @param connectTimeout	连接超时
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	private static String httpGet(String url,int socketTimeout,int connectTimeout) throws ClientProtocolException, IOException {
		String result="";
		CloseableHttpClient  client  =HttpClientBuilder.create().build();
		try {
			HttpGet httpGet = new HttpGet(url.trim());
			httpGet.setHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
			RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(socketTimeout).setConnectTimeout(connectTimeout).build();  
			httpGet.setConfig(requestConfig);
			HttpResponse response  = client.execute(httpGet);
			HttpEntity entity = response.getEntity(); 
			result = EntityUtils.toString(entity,"UTF-8");
			client.close();
		} finally{
			if(client!=null){
				client.close();
			}
			
		}
		return result;
	}
	
	/**
	 * POST请求
	 * @param url	请求地址
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static String httpPost(String url) throws ClientProtocolException, IOException {
		return httpPost(url,30000,30000);
	}

	
	/**
	 * POST请求
	 * @param url	请求地址
	 * @param socketTimeout		读取时间
	 * @param connectTimeout	超时时间
	 * @return
	 * @throws IOException
	 */
	public static String httpPost(String url,int socketTimeout,int connectTimeout) throws IOException  {
		String result="";
		CloseableHttpClient  client  =HttpClientBuilder.create().build();
		try {
			HttpPost httpPost = new HttpPost(url.trim());
			httpPost.setHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
			RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(socketTimeout).setConnectTimeout(connectTimeout).build(); 
			httpPost.setConfig(requestConfig);
			HttpResponse response  = client.execute(httpPost);
			HttpEntity entity = response.getEntity(); 
			result = EntityUtils.toString(entity,"UTF-8");
			client.close();
		} catch (ClientProtocolException e) {
			return result="-100";
		} catch (IOException e) {
			return result="-100";
		} finally{
			if(client!=null){
					client.close();
			}
			
		}
		return result;
	}
	
	
	public static String httpPost(String url,Map params) throws ClientProtocolException, IOException {
		if (params != null && !params.isEmpty()) {  
            List formParams = new ArrayList();  
            Set> entrySet = params.entrySet();  
            for (Entry entry : entrySet) {  
                formParams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));  
            }
            return httpPost(url,formParams,300000,300000);
        }
		return httpPost(url);
	}
	
	
	public static String httpPost(String url,List param) throws ClientProtocolException, IOException{
		return httpPost(url,param,30000,30000);
	}
	
	public static String httpPost(String url,List param,int socketTimeout,int connectTimeout) throws ClientProtocolException, IOException{
		String result="";
		CloseableHttpClient  client  =HttpClientBuilder.create().build();
		try{
			HttpPost httpost = new HttpPost(url.trim());
			httpost.setHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
			HttpEntity entity = null;
			if(param==null){
				entity = new StringEntity("");
			}else{
				entity = new UrlEncodedFormEntity(param,"UTF-8");
			}
			httpost.setEntity(entity);
			RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(socketTimeout).setConnectTimeout(connectTimeout).build();  
			httpost.setConfig(requestConfig);
			HttpResponse response  = client.execute(httpost);
			HttpEntity responseEntity = response.getEntity(); 
			result = EntityUtils.toString(responseEntity,"utf-8");
			client.close();
		} finally{
			if(client!=null){
				client.close();
			}
		}
		return result;
	}
	
	
	public static String httpXmlPost(String url,String xmlString) throws ClientProtocolException, IOException{
		return httpXmlPost(url,xmlString,30000,30000);
	}
	
	public static String httpXmlPost(String url,String xmlString,int socketTimeout,int connectTimeout) throws ClientProtocolException, IOException{
		String result="";
		CloseableHttpClient  client  = HttpClientBuilder.create().build();
		try{
			HttpPost httpost = new HttpPost(url.trim());
			httpost.setHeader("Content-Type","text/xml;charset=UTF-8");
			HttpEntity  entity  = new StringEntity(xmlString,"UTF-8");
			httpost.setEntity(entity);
			RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(socketTimeout).setConnectTimeout(connectTimeout).build();  
			httpost.setConfig(requestConfig);
			HttpResponse response  = client.execute(httpost);
			HttpEntity responseEntity = response.getEntity(); 
			result = EntityUtils.toString(responseEntity,"utf-8");
			client.close();
		} finally{
			if(client!=null){
				client.close();
			}
		}
		return result;
	}
	
	
	public static String httpJsonPost(String url,String jsonString) throws ClientProtocolException, IOException{
		return httpJsonPost(url,jsonString,30000,30000);
	}
	
	public static String httpJsonPost(String url,String jsonString,int socketTimeout,int connectTimeout) throws ClientProtocolException, IOException{
		String result="";
		CloseableHttpClient  client  =HttpClientBuilder.create().build();
		try{
			HttpPost httpost = new HttpPost(url.trim());
			httpost.setHeader("Content-Type","text/json;charset=UTF-8");
			HttpEntity  entity  = new StringEntity(jsonString,"UTF-8");
			httpost.setEntity(entity);
			RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(socketTimeout).setConnectTimeout(connectTimeout).build();  
			httpost.setConfig(requestConfig);
			HttpResponse response  = client.execute(httpost);
			HttpEntity responseEntity = response.getEntity(); 
			result = EntityUtils.toString(responseEntity,"utf-8");
			client.close();
		} finally{
			if(client!=null){
				client.close();
			}
		}
		return result;
	}
	
}

你可能感兴趣的:(HttpClientUtil)