HttpClient 传入代理IP和端口则走代理,不传则直接请求的两个方法

 HttpClient 传入代理IP和端口则走代理,不传则直接请求的两个方法

 

方法使用的相关包

import com.ifunpay.util.common.StringUtil;
import net.sf.json.JSONObject;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpClientParams;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
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.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

代码实现如下

public static String httpGetRequest(String url ,String proxyIp,int port,Map paramsMap,String encoding) throws  Exception{
        //创建httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //设置参数
        String params = null;
        if (paramsMap !=null){
            List list = new ArrayList();
            Iterator iterator = paramsMap.entrySet().iterator();
            while(iterator.hasNext()){
                Map.Entry elem = (Map.Entry) iterator.next();
                list.add(new BasicNameValuePair(elem.getKey(),String.valueOf(elem.getValue())));
            }
            //转化参数
            params = EntityUtils.toString(new UrlEncodedFormEntity(list, encoding));
        }
        //创建请求方法的实例, 并指定请求url
        HttpGet httpGet = new HttpGet(url+"?"+params);
        RequestConfig config ;
        if (StringUtil.isNotEmpty(proxyIp) && port>0){
            //代理对象
            HttpHost proxy = new HttpHost(proxyIp, port, "http");
            //配置对象
            config = RequestConfig.custom().setProxy(proxy).build();
            //使用配置
            httpGet.setConfig(config);
        }
        CloseableHttpResponse response=httpClient.execute(httpGet);
        HttpEntity entity=response.getEntity();
        String content=EntityUtils.toString(entity, "utf-8");
        log.info("httpGetRequestResultString:"+content);
        httpClient.close();
        return  content;
    }

    public static String httpPostRequest(String url ,String proxyIp,int port,Map paramsMap, String encoding) throws  Exception{
        //创建httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();

        //创建请求方法的实例, 并指定请求url
        HttpPost httpPost = new HttpPost(url);
        //设置参数
        if (paramsMap !=null){
            List list = new ArrayList();
            Iterator iterator = paramsMap.entrySet().iterator();
            while(iterator.hasNext()){
                Map.Entry elem = (Map.Entry) iterator.next();
                list.add(new BasicNameValuePair(elem.getKey(),String.valueOf(elem.getValue())));
            }
            if(list.size() > 0){
                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,encoding);
                httpPost.setEntity(entity);
            }
        }
        RequestConfig config ;
        if (StringUtil.isNotEmpty(proxyIp) && port>0){
            //代理对象
            HttpHost proxy = new HttpHost(proxyIp, port, "http");
            //配置对象
            config = RequestConfig.custom().setProxy(proxy).build();
            //使用配置
            httpPost.setConfig(config);
        }
        CloseableHttpResponse response=httpClient.execute(httpPost);
        HttpEntity entity=response.getEntity();
        String content=EntityUtils.toString(entity, "utf-8");
        log.info("httpPostRequestResultString:"+content);
        httpClient.close();
        return  content;

    }

 

你可能感兴趣的:(nginx)