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);
                }
            }
        }
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章