http系列 - 通过代理实现http请求

有时候受网络影响,http请求需要通过代理才能访问出去;精选如下案例供参考

案例1:httpGet

public static String httpGet(String url) throws Exception {
        try {
            HttpMethod gt = new GetMethod(url);
            gt.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, Constant.UTF8);
            gt.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 30000);
            gt.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
            HttpClient httpClient = new HttpClient();
            httpClient.getHostConfiguration().setProxy("代理IP", 8080);
            httpClient.getParams().setAuthenticationPreemptive(true);
            httpClient.getState().setProxyCredentials(AuthScope.ANY, new UsernamePasswordCredentials("开机用户", "开机密码"));
            int statusCode = httpClient.executeMethod(gt);
            logger.info("statusCode: " + statusCode);
            String returnStr = gt.getResponseBodyAsString();
            logger.info("接口URL: " + url);
            logger.info("接口返回: " + returnStr);
            return returnStr;
        } catch (Exception e) {
            logger.error("异常啦! ", e);
            System.out.println("-------------------http get异常了----------------");
        }
        return null;
    }

案例2:httpPost

public static String httpPost(String url, String json) {
        try {
            PostMethod httpPost = new PostMethod(url);
            httpPost.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, Constant.UTF8);
            httpPost.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 500000);
            httpPost.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
            HttpClient httpClient = new HttpClient();
            httpClient.getHostConfiguration().setProxy("代理IP", 8080);
            httpClient.getParams().setAuthenticationPreemptive(true);
            httpClient.getState().setProxyCredentials(AuthScope.ANY, new UsernamePasswordCredentials("开机用户名", "开机密码"));
            System.out.println("请求参数: " + json);
            RequestEntity se = new StringRequestEntity(json, "application/json", Constant.UTF8);
            httpPost.setRequestEntity(se);
            int statusCode = httpClient.executeMethod(httpPost);
            System.out.println("返回码: " + statusCode);
            String returnStr = httpPost.getResponseBodyAsString();
            System.out.println("返回值: " + returnStr);
            return returnStr;
        } catch (Exception e) {
            System.out.println("-------------------------------------------------------");
        }
        return "";
    }

案例2:httpsPost

    public static String httpsPost(String url, String json) {
        CloseableHttpResponse response = null;
        CloseableHttpClient httpClient = SSLHttpClient.getSSLHttpClient();
        try {
            HttpPost httpPost = new HttpPost(url);
            httpPost.setHeader("Content-Type", "application/json");
            httpPost.setHeader("Connection", "keep-alive");
            RequestConfig defaultRequestConfig = RequestConfig.custom().setConnectTimeout(120000).setConnectionRequestTimeout(120000).setSocketTimeout(120000).build();
            RequestConfig requestConfig = RequestConfig.copy(defaultRequestConfig).setProxy(new HttpHost("代理IP", 8080)).build();
            httpPost.setConfig(requestConfig);
            StringEntity se = new StringEntity(json);
            se.setContentType("text/json");
            se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
            httpPost.setEntity(se);
            response = httpClient.execute(httpPost);
            HttpEntity entity = response.getEntity(); // 获取返回实体
            String returnStr = EntityUtils.toString(entity, Constant.UTF8);
            logger.info(">>>>post url : " + url);
            logger.info(">>>>post 请求参数 : " + json);
            logger.info(">>>>post 接口返回值 : " + returnStr);
            return returnStr;
        } catch (Exception e) {
            logger.error("异常啦! ", e);
            logger.info("本地接口异常url :" + url);
            return null;
        } finally {
            if (response != null) {
                try {
                    EntityUtils.consume(response.getEntity());
                } catch (Exception e) {
                    logger.error("异常啦! ", e);
                }
            }
        }
    }

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值