下面代码使用HTTPClient包进行操作Get和Post请求
package com.rapido.weixin.utils; import java.io.IOException; import java.net.SocketTimeoutException; import java.net.URI; import java.security.KeyManagementException; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import javax.net.ssl.SSLContext; import org.apache.http.HttpEntity; import org.apache.http.HttpHeaders; import org.apache.http.HttpResponse; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.conn.ConnectTimeoutException; import org.apache.http.conn.ConnectionPoolTimeoutException; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.conn.ssl.SSLContextBuilder; import org.apache.http.conn.ssl.TrustStrategy; 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.apache.log4j.Logger; /** * HTTPClient 请求代理类 * @author X-rapido * @description 发送Get Post请求,本项目 HTTPClient4.3版本 * * 也可以使用新浪FetchURL是SAE为开发者提供的分布式网页抓取服务,用来同步的抓取http页面,FetchURL针对国内的网络的做了优化 * 内部有调度系统,尽可能保证用户快速的抓取到目标页面,更多内容详见:http://baike.haosou.com/doc/7565440-7839533.html */ public class HttpClientProxyUtil { private static Logger logger = Logger.getLogger(HttpClientProxyUtil.class); /** * 使用HTTPClient进行POST请求 * @param api_url 请求路径 * @param param 请求格式有name1=value1&name2=value2、json、xml、map或其他形式,具体要看接收方的取值 * @return */ public static String sendGet(String api_url, String param) { logger.info("请求URL:" + api_url +",参数:" + param); CloseableHttpClient httpClient = null; try { httpClient = HttpClients.createDefault(); // 生成一个httpclient对象 HttpGet httpGet = new HttpGet(api_url + "?" + param); // 连接超时时间,10秒, 传输超时时间,30秒,不设置超时的话,一旦服务器没有响应,等待时间N久(>24小时)。 RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(10000).setConnectTimeout(30000).build(); httpGet.setConfig(requestConfig); // 设置请求器的配置 // 配置请求的HEADER部分 httpGet.addHeader(HttpHeaders.ACCEPT, "application/xml"); HttpResponse response = httpClient.execute(httpGet); //执行请求Http HttpEntity entity = response.getEntity(); // logger.info("执行请求:" + httpPost.getRequestLine()); // logger.info("响应的所有HEADER内容:" + Arrays.toString(response.getAllHeaders())); // 判断响应实体是否为空 if (entity != null) { String content = EntityUtils.toString(entity, "UTF-8"); logger.info("响应内容:" + content); return content; } } catch (ConnectionPoolTimeoutException e) { e.printStackTrace(); logger.error("http get throw ConnectionPoolTimeoutException(等待超时)"); } catch (ConnectTimeoutException e) { e.printStackTrace(); logger.error("http get throw ConnectTimeoutException(联接超时)"); } catch (SocketTimeoutException e) { e.printStackTrace(); logger.error("http get throw SocketTimeoutException(通讯超时)"); } catch (Exception e) { e.printStackTrace(); logger.error("http get throw Exception"); } finally { if (httpClient != null) { try { httpClient.close(); } catch (IOException e) { e.printStackTrace(); } } } return ""; } /** * 使用HTTPClient进行POST请求 * @param api_url 请求路径 * @param param 请求格式有name1=value1&name2=value2、json、xml、map或其他形式,具体要看接收方的取值 * @return */ public static String sendPost(String api_url, String param) { logger.info("请求URL:" + api_url +",参数:" + param); CloseableHttpClient httpClient = null; try { httpClient = HttpClients.createDefault(); // 生成一个httpclient对象 HttpPost httpPost = new HttpPost(api_url); // 得指明使用UTF-8编码,否则到API服务器XML的中文不能被成功识别 StringEntity postEntity = new StringEntity(param, "UTF-8"); httpPost.addHeader("Content-Type", "text/xml"); httpPost.setEntity(postEntity); // 连接超时时间,10秒, 传输超时时间,30秒,4.3版本不设置超时的话,一旦服务器没有响应,等待时间N久(>24小时)。 RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(10000).setConnectTimeout(30000).build(); httpPost.setConfig(requestConfig);// 设置请求器的配置 HttpResponse response = httpClient.execute(httpPost); //执行请求Http HttpEntity entity = response.getEntity(); // logger.info("执行请求:" + httpPost.getRequestLine()); // logger.info("响应的所有HEADER内容:" + Arrays.toString(response.getAllHeaders())); // 判断响应实体是否为空 if (entity != null) { String content = EntityUtils.toString(entity, "UTF-8"); logger.info("响应内容:" + content); return content; } } catch (ConnectionPoolTimeoutException e) { e.printStackTrace(); logger.error("http get throw ConnectionPoolTimeoutException(等待超时)"); } catch (ConnectTimeoutException e) { e.printStackTrace(); logger.error("http get throw ConnectTimeoutException(联接超时)"); } catch (SocketTimeoutException e) { e.printStackTrace(); logger.error("http get throw SocketTimeoutException(通讯超时)"); } catch (Exception e) { e.printStackTrace(); logger.error("http get throw Exception"); } finally { if (httpClient != null) { try { httpClient.close(); } catch (IOException e) { e.printStackTrace(); } } } return ""; } public static CloseableHttpClient createSSLClientDefault() { // ManagedHttpClientConnectionFactory try { SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() { // 信任所有 public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException { return true; } }).build(); SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext); return HttpClients.custom().setSSLSocketFactory(sslsf).build(); } catch (KeyManagementException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (KeyStoreException e) { e.printStackTrace(); } return HttpClients.createDefault(); } /** * 请求Https请求 * @param url * @return */ public static String requestHttps(String url){ logger.info("请求URL:" + url); CloseableHttpClient httpClient = createSSLClientDefault(); HttpGet httpGet = new HttpGet(); try { httpGet.setURI(new URI(url)); RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(10000).setConnectTimeout(30000).build(); httpGet.setConfig(requestConfig); // 设置请求器的配置 // 配置请求的HEADER部分 httpGet.addHeader(HttpHeaders.ACCEPT, "application/xml"); HttpResponse response = httpClient.execute(httpGet); //执行请求Http HttpEntity entity = response.getEntity(); // logger.info("执行请求:" + httpPost.getRequestLine()); // logger.info("响应的所有HEADER内容:" + Arrays.toString(response.getAllHeaders())); // 判断响应实体是否为空 if (entity != null) { String content = EntityUtils.toString(entity, "UTF-8"); logger.info("响应内容:" + content); return content; } } catch (Exception e) { e.printStackTrace(); } return ""; } }
未经允许请勿转载:程序喵 » 【Java】HttpClientProxyUtil HTTPClient请求工具类源码