java 添加了代理的http请求




import net.sf.json.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
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.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.testng.Assert;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Map;

public class HttpUtils {

    public static String doGet(String url,String authori,String proxyIp,int proxyPort,String proxyScheme) {
        CloseableHttpClient httpClient = null;
        CloseableHttpResponse response = null;
        String result = "";
        try {
            // 通过址默认配置创建一个httpClient实例
            httpClient = HttpClients.createDefault();
            // 创建httpGet远程连接实例
            HttpGet httpGet = new HttpGet(url);
            // 代理信息
            HttpHost proxy = new HttpHost(proxyIp, proxyPort, proxyScheme);

            httpGet.setHeader("Authorization", "Bearer "+ authori);
            // 设置配置请求参数
            RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000)// 连接主机服务超时时间
                    .setProxy(proxy)
                    .setConnectionRequestTimeout(35000)// 请求超时时间
                    .setSocketTimeout(60000)// 数据读取超时时间
                    .build();
            // 为httpGet实例设置配置
            httpGet.setConfig(requestConfig);
            // 执行get请求得到返回对象
            response = httpClient.execute(httpGet);
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode == HttpStatus.SC_OK) {
                // 从响应对象中获取响应内容
                HttpEntity entity = response.getEntity();
                result = EntityUtils.toString(entity);
            }
            else{
                Assert.assertTrue(false,"接口调用失败,返回code=" + statusCode);
            }
            // 通过返回对象获取返回数据


        } catch (ClientProtocolException e) {
            Assert.assertTrue(false,e.getMessage());

            e.printStackTrace();
        } catch (IOException e) {
            Assert.assertTrue(false,e.getMessage());

            e.printStackTrace();
        } finally {
            // 关闭资源
            if (null != response) {
                try {
                    response.close();
                } catch (IOException e) {
                    Assert.assertTrue(false,e.getMessage());

                    e.printStackTrace();
                }
            }
            if (null != httpClient) {
                try {
                    httpClient.close();
                } catch (IOException e) {
                    Assert.assertTrue(false,e.getMessage());

                    e.printStackTrace();
                }
            }
        }
        return result;
    }

    public static String doPost(String url, Map<String, Object> paramMap,String proxyIp,int proxyPort,String proxyScheme) {
        CloseableHttpClient httpClient = null;
        CloseableHttpResponse httpResponse = null;
        String result = "";
        // 创建httpClient实例
        httpClient = HttpClients.createDefault();
        // 创建httpPost远程连接实例
        HttpPost httpPost = new HttpPost(url);
        // 配置请求参数实例
       StringEntity entityRequest = new StringEntity(JSONObject.fromObject(paramMap).toString(), Charset.forName("UTF-8"));
//代理信息
        HttpHost proxy = new HttpHost(proxyIp, proxyPort, proxyScheme);
        RequestConfig requestConfig = RequestConfig.custom()
                .setProxy(proxy)//
                .setConnectTimeout(35000)// 设置连接主机服务超时时间
                .setConnectionRequestTimeout(35000)// 设置连接请求超时时间
                .setSocketTimeout(60000)// 设置读取数据连接超时时间
                .build();
        // 为httpPost实例设置配置
        httpPost.setConfig(requestConfig);
        // 设置请求头
        httpPost.addHeader("Content-type", "application/json; charset=utf-8");
        entityRequest.setContentType("application/json");

            // 为httpPost设置封装好的请求参数
         httpPost.setEntity(entityRequest);

        try {
            // httpClient对象执行post请求,并返回响应参数对象
            httpResponse = httpClient.execute(httpPost);
            int statusCode = httpResponse.getStatusLine().getStatusCode();
            if (statusCode == HttpStatus.SC_OK) {
                // 从响应对象中获取响应内容
                HttpEntity entity = httpResponse.getEntity();

                result = EntityUtils.toString(entity);
            }
            else{
                Assert.assertTrue(false,"接口调用失败,返回code=" + statusCode);
            }
        } catch (ClientProtocolException e) {
            Assert.assertTrue(false,e.getMessage());
            e.printStackTrace();
        } catch (IOException e) {
            Assert.assertTrue(false,e.getMessage());

            e.printStackTrace();
        } finally {
            // 关闭资源
            if (null != httpResponse) {
                try {
                    httpResponse.close();
                } catch (IOException e) {
                    Assert.assertTrue(false,e.getMessage());

                    e.printStackTrace();
                }
            }
            if (null != httpClient) {
                try {
                    httpClient.close();
                } catch (IOException e) {
                    Assert.assertTrue(false,e.getMessage());

                    e.printStackTrace();
                }
            }
        }
        return result;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值