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

我会带着你远行 2022-05-29 04:17 297阅读 0赞

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

案例1:httpGet

  1. public static String httpGet(String url) throws Exception {
  2. try {
  3. HttpMethod gt = new GetMethod(url);
  4. gt.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, Constant.UTF8);
  5. gt.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 30000);
  6. gt.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
  7. HttpClient httpClient = new HttpClient();
  8. httpClient.getHostConfiguration().setProxy("代理IP", 8080);
  9. httpClient.getParams().setAuthenticationPreemptive(true);
  10. httpClient.getState().setProxyCredentials(AuthScope.ANY, new UsernamePasswordCredentials("开机用户", "开机密码"));
  11. int statusCode = httpClient.executeMethod(gt);
  12. logger.info("statusCode: " + statusCode);
  13. String returnStr = gt.getResponseBodyAsString();
  14. logger.info("接口URL: " + url);
  15. logger.info("接口返回: " + returnStr);
  16. return returnStr;
  17. } catch (Exception e) {
  18. logger.error("异常啦! ", e);
  19. System.out.println("-------------------http get异常了----------------");
  20. }
  21. return null;
  22. }

案例2:httpPost

  1. public static String httpPost(String url, String json) {
  2. try {
  3. PostMethod httpPost = new PostMethod(url);
  4. httpPost.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, Constant.UTF8);
  5. httpPost.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 500000);
  6. httpPost.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
  7. HttpClient httpClient = new HttpClient();
  8. httpClient.getHostConfiguration().setProxy("代理IP", 8080);
  9. httpClient.getParams().setAuthenticationPreemptive(true);
  10. httpClient.getState().setProxyCredentials(AuthScope.ANY, new UsernamePasswordCredentials("开机用户名", "开机密码"));
  11. System.out.println("请求参数: " + json);
  12. RequestEntity se = new StringRequestEntity(json, "application/json", Constant.UTF8);
  13. httpPost.setRequestEntity(se);
  14. int statusCode = httpClient.executeMethod(httpPost);
  15. System.out.println("返回码: " + statusCode);
  16. String returnStr = httpPost.getResponseBodyAsString();
  17. System.out.println("返回值: " + returnStr);
  18. return returnStr;
  19. } catch (Exception e) {
  20. System.out.println("-------------------------------------------------------");
  21. }
  22. return "";
  23. }

案例2:httpsPost

  1. public static String httpsPost(String url, String json) {
  2. CloseableHttpResponse response = null;
  3. CloseableHttpClient httpClient = SSLHttpClient.getSSLHttpClient();
  4. try {
  5. HttpPost httpPost = new HttpPost(url);
  6. httpPost.setHeader("Content-Type", "application/json");
  7. httpPost.setHeader("Connection", "keep-alive");
  8. RequestConfig defaultRequestConfig = RequestConfig.custom().setConnectTimeout(120000).setConnectionRequestTimeout(120000).setSocketTimeout(120000).build();
  9. RequestConfig requestConfig = RequestConfig.copy(defaultRequestConfig).setProxy(new HttpHost("代理IP", 8080)).build();
  10. httpPost.setConfig(requestConfig);
  11. StringEntity se = new StringEntity(json);
  12. se.setContentType("text/json");
  13. se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
  14. httpPost.setEntity(se);
  15. response = httpClient.execute(httpPost);
  16. HttpEntity entity = response.getEntity(); // 获取返回实体
  17. String returnStr = EntityUtils.toString(entity, Constant.UTF8);
  18. logger.info(">>>>post url : " + url);
  19. logger.info(">>>>post 请求参数 : " + json);
  20. logger.info(">>>>post 接口返回值 : " + returnStr);
  21. return returnStr;
  22. } catch (Exception e) {
  23. logger.error("异常啦! ", e);
  24. logger.info("本地接口异常url :" + url);
  25. return null;
  26. } finally {
  27. if (response != null) {
  28. try {
  29. EntityUtils.consume(response.getEntity());
  30. } catch (Exception e) {
  31. logger.error("异常啦! ", e);
  32. }
  33. }
  34. }
  35. }

发表评论

表情:
评论列表 (有 0 条评论,297人围观)

还没有评论,来说两句吧...

相关阅读