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);
}
}
}
}
还没有评论,来说两句吧...