HttpClient用法--这一篇全了解(内含例子)

HttpClient相比传统JDK自带的URLConnection,增加了易用性和灵活性,它不仅使客户端发送Http请求变得容易,而且也方便开发人员测试接口(基于Http协议的),提高了开发的效率,也方便提高代码的健壮性。因此熟练掌握HttpClient是很重要的必修内容,掌握HttpClient后,相信对于Http协议的了解会更加深入。

org.apache.commons.httpclient.HttpClient与org.apache.http.client.HttpClient的区别

Commons的HttpClient项目现在是生命的尽头,不再被开发,  已被Apache HttpComponents项目HttpClient和HttpCore  模组取代,提供更好的性能和更大的灵活性。  

一、简介

HttpClient是Apache Jakarta Common下的子项目,用来提供高效的、最新的、功能丰富的支持HTTP协议的客户端编程工具包,并且它支持HTTP协议最新的版本和建议。HttpClient已经应用在很多的项目中,比如Apache Jakarta上很著名的另外两个开源项目Cactus和HTMLUnit都使用了HttpClient。

下载地址: http://hc.apache.org/downloads.cgi

二、特性

1. 基于标准、纯净的java语言。实现了Http1.0和Http1.1

2. 以可扩展的面向对象的结构实现了Http全部的方法(GET, POST, PUT, DELETE, HEAD, OPTIONS, and TRACE)。

3. 支持HTTPS协议。

4. 通过Http代理建立透明的连接。

5. 利用CONNECT方法通过Http代理建立隧道的https连接。

6. Basic, Digest, NTLMv1, NTLMv2, NTLM2 Session, SNPNEGO/Kerberos认证方案。

7. 插件式的自定义认证方案。

8. 便携可靠的套接字工厂使它更容易的使用第三方解决方案。

9. 连接管理器支持多线程应用。支持设置最大连接数,同时支持设置每个主机的最大连接数,发现并关闭过期的连接。

10. 自动处理Set-Cookie中的Cookie。

11. 插件式的自定义Cookie策略。

12. Request的输出流可以避免流中内容直接缓冲到socket服务器。

13. Response的输入流可以有效的从socket服务器直接读取相应内容。

14. 在http1.0和http1.1中利用KeepAlive保持持久连接。

15. 直接获取服务器发送的response code和 headers。

16. 设置连接超时的能力。

17. 实验性的支持http1.1 response caching。

18. 源代码基于Apache License 可免费获取。

三、使用方法

使用HttpClient发送请求、接收响应很简单,一般需要如下几步即可。

1. 创建HttpClient对象。

2. 创建请求方法的实例,并指定请求URL。如果需要发送GET请求,创建HttpGet对象;如果需要发送POST请求,创建HttpPost对象。

3. 如果需要发送请求参数,可调用HttpGet、HttpPost共同的setParams(HttpParams params)方法来添加请求参数;对于HttpPost对象而言,也可调用setEntity(HttpEntity entity)方法来设置请求参数。

4. 调用HttpClient对象的execute(HttpUriRequest request)发送请求,该方法返回一个HttpResponse。

5. 调用HttpResponse的getAllHeaders()、getHeaders(String name)等方法可获取服务器的响应头;调用HttpResponse的getEntity()方法可获取HttpEntity对象,该对象包装了服务器的响应内容。程序可通过该对象获取服务器的响应内容。

6. 释放连接。无论执行方法是否成功,都必须释放连接

相关jar包

  1. commons-cli-1.2.jar
  2. commons-codec-1.9.jar
  3. commons-logging-1.2.jar
  4. fluent-hc-4.5.1.jar
  5. httpclient-4.5.1.jar
  6. httpclient-cache-4.5.1.jar
  7. httpclient-win-4.5.1.jar
  8. httpcore-4.4.3.jar
  9. httpcore-ab-4.4.3.jar
  10. httpcore-nio-4.4.3.jar
  11. httpmime-4.5.1.jar
  12. jna-4.1.0.jar
  13. jna-platform-4.1.0.jar

最简单post请求, 源自 http://my.oschina.net/xinxingegeya/blog/282683

  1. package a;
  2.  
  3. import java.io.FileInputStream;
  4. import java.io.IOException;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. import java.util.Properties;
  8.  
  9. import org.apache.http.HttpEntity;
  10. import org.apache.http.HttpResponse;
  11. import org.apache.http.NameValuePair;
  12. import org.apache.http.client.HttpClient;
  13. import org.apache.http.client.config.RequestConfig;
  14. import org.apache.http.client.entity.UrlEncodedFormEntity;
  15. import org.apache.http.client.methods.HttpPost;
  16. import org.apache.http.impl.client.DefaultHttpClient;
  17. import org.apache.http.message.BasicNameValuePair;
  18. import org.apache.http.util.EntityUtils;
  19.  
  20. public class First {
  21. public static void main(String[] args) throws Exception{
  22. List<NameValuePair> formparams = new ArrayList<NameValuePair>();
  23. formparams.add(new BasicNameValuePair("account", ""));
  24. formparams.add(new BasicNameValuePair("password", ""));
  25. HttpEntity reqEntity = new UrlEncodedFormEntity(formparams, "utf-8");
  26.  
  27. RequestConfig requestConfig = RequestConfig.custom()
  28. .setConnectTimeout(5000)//一、连接超时:connectionTimeout-->指的是连接一个url的连接等待时间
  29. .setSocketTimeout(5000)// 二、读取数据超时:SocketTimeout-->指的是连接上一个url,获取response的返回等待时间
  30. .setConnectionRequestTimeout(5000)
  31. .build();
  32.  
  33. HttpClient client = new DefaultHttpClient();
  34. HttpPost post = new HttpPost("http://cnivi.com.cn/login");
  35. post.setEntity(reqEntity);
  36. post.setConfig(requestConfig);
  37. HttpResponse response = client.execute(post);
  38.  
  39. if (response.getStatusLine().getStatusCode() == 200) {
  40. HttpEntity resEntity = response.getEntity();
  41. String message = EntityUtils.toString(resEntity, "utf-8");
  42. System.out.println(message);
  43. } else {
  44. System.out.println("请求失败");
  45. }
  46. }
  47.  
  48. }

四、实例

主文件

  1. package com.test;
  2.  
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.IOException;
  6. import java.io.UnsupportedEncodingException;
  7. import java.security.KeyManagementException;
  8. import java.security.KeyStore;
  9. import java.security.KeyStoreException;
  10. import java.security.NoSuchAlgorithmException;
  11. import java.security.cert.CertificateException;
  12. import java.util.ArrayList;
  13. import java.util.List;
  14. import javax.net.ssl.SSLContext;
  15. import org.apache.http.HttpEntity;
  16. import org.apache.http.NameValuePair;
  17. import org.apache.http.ParseException;
  18. import org.apache.http.client.ClientProtocolException;
  19. import org.apache.http.client.entity.UrlEncodedFormEntity;
  20. import org.apache.http.client.methods.CloseableHttpResponse;
  21. import org.apache.http.client.methods.HttpGet;
  22. import org.apache.http.client.methods.HttpPost;
  23. import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
  24. import org.apache.http.conn.ssl.SSLContexts;
  25. import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
  26. import org.apache.http.entity.ContentType;
  27. import org.apache.http.entity.mime.MultipartEntityBuilder;
  28. import org.apache.http.entity.mime.content.FileBody;
  29. import org.apache.http.entity.mime.content.StringBody;
  30. import org.apache.http.impl.client.CloseableHttpClient;
  31. import org.apache.http.impl.client.HttpClients;
  32. import org.apache.http.message.BasicNameValuePair;
  33. import org.apache.http.util.EntityUtils;
  34. import org.apache.http.client.config.RequestConfig;
  35. import org.junit.Test;
  36. public class HttpClientTest {
  37.   //方法见下........
  38. }

HttpClientUtils工具类

  1. package com.bobo.code.web.controller.technology.httpcomponents;
  2.  
  3.  
  4. import org.apache.http.HttpEntity;
  5. import org.apache.http.HttpHost;
  6. import org.apache.http.HttpResponse;
  7. import org.apache.http.NameValuePair;
  8. import org.apache.http.client.HttpClient;
  9. import org.apache.http.client.config.RequestConfig;
  10. import org.apache.http.client.methods.HttpUriRequest;
  11. import org.apache.http.client.methods.RequestBuilder;
  12. import org.apache.http.conn.routing.HttpRoute;
  13. import org.apache.http.impl.client.CloseableHttpClient;
  14. import org.apache.http.impl.client.HttpClientBuilder;
  15. import org.apache.http.impl.client.HttpClients;
  16. import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
  17. import org.apache.http.message.BasicNameValuePair;
  18. import org.apache.http.util.EntityUtils;
  19.  
  20. import java.io.IOException;
  21. import java.util.*;
  22.  
  23. public class HttpClientUtils {
  24.  
  25. private static PoolingHttpClientConnectionManager connectionManager = null;
  26. private static HttpClientBuilder httpBuilder = null;
  27. private static RequestConfig requestConfig = null;
  28.  
  29. private static int MAXCONNECTION = 10;
  30.  
  31. private static int DEFAULTMAXCONNECTION = 5;
  32.  
  33. private static String IP = "cnivi.com.cn";
  34. private static int PORT = 80;
  35.  
  36. static {
  37. //设置http的状态参数
  38. requestConfig = RequestConfig.custom()
  39. .setSocketTimeout(5000)
  40. .setConnectTimeout(5000)
  41. .setConnectionRequestTimeout(5000)
  42. .build();
  43.  
  44. HttpHost target = new HttpHost(IP, PORT);
  45. connectionManager = new PoolingHttpClientConnectionManager();
  46. connectionManager.setMaxTotal(MAXCONNECTION);//客户端总并行链接最大数
  47. connectionManager.setDefaultMaxPerRoute(DEFAULTMAXCONNECTION);//每个主机的最大并行链接数
  48. connectionManager.setMaxPerRoute(new HttpRoute(target), 20);
  49. httpBuilder = HttpClients.custom();
  50. httpBuilder.setConnectionManager(connectionManager);
  51. }
  52.  
  53. public static CloseableHttpClient getConnection() {
  54. CloseableHttpClient httpClient = httpBuilder.build();
  55. return httpClient;
  56. }
  57.  
  58.  
  59. public static HttpUriRequest getRequestMethod(Map<String, String> map, String url, String method) {
  60. List<NameValuePair> params = new ArrayList<NameValuePair>();
  61. Set<Map.Entry<String, String>> entrySet = map.entrySet();
  62. for (Map.Entry<String, String> e : entrySet) {
  63. String name = e.getKey();
  64. String value = e.getValue();
  65. NameValuePair pair = new BasicNameValuePair(name, value);
  66. params.add(pair);
  67. }
  68. HttpUriRequest reqMethod = null;
  69. if ("post".equals(method)) {
  70. reqMethod = RequestBuilder.post().setUri(url)
  71. .addParameters(params.toArray(new BasicNameValuePair[params.size()]))
  72. .setConfig(requestConfig).build();
  73. } else if ("get".equals(method)) {
  74. reqMethod = RequestBuilder.get().setUri(url)
  75. .addParameters(params.toArray(new BasicNameValuePair[params.size()]))
  76. .setConfig(requestConfig).build();
  77. }
  78. return reqMethod;
  79. }
  80.  
  81. public static void main(String args[]) throws IOException {
  82. Map<String, String> map = new HashMap<String, String>();
  83. map.put("account", "");
  84. map.put("password", "");
  85.  
  86. HttpClient client = getConnection();
  87. HttpUriRequest post = getRequestMethod(map, "http://cnivi.com.cn/login", "post");
  88. HttpResponse response = client.execute(post);
  89.  
  90. if (response.getStatusLine().getStatusCode() == 200) {
  91. HttpEntity entity = response.getEntity();
  92. String message = EntityUtils.toString(entity, "utf-8");
  93. System.out.println(message);
  94. } else {
  95. System.out.println("请求失败");
  96. }
  97. }
  98. }

get方式

  1. /**
  2. * 发送 get请求
  3. */
  4. public void get() {
  5. CloseableHttpClient httpclient = HttpClients.createDefault();
  6. try {
  7. // 创建httpget.
  8. HttpGet httpget = new HttpGet("http://www.baidu.com/");
  9. System.out.println("executing request " + httpget.getURI());
  10. // 执行get请求.
  11. CloseableHttpResponse response = httpclient.execute(httpget);
  12. try {
  13. // 获取响应实体
  14. HttpEntity entity = response.getEntity();
  15. System.out.println("--------------------------------------");
  16. // 打印响应状态
  17. System.out.println(response.getStatusLine());
  18. if (entity != null) {
  19. // 打印响应内容长度
  20. System.out.println("Response content length: " + entity.getContentLength());
  21. // 打印响应内容
  22. System.out.println("Response content: " + EntityUtils.toString(entity));
  23. }
  24. System.out.println("------------------------------------");
  25. } finally {
  26. response.close();
  27. }
  28. } catch (ClientProtocolException e) {
  29. e.printStackTrace();
  30. } catch (ParseException e) {
  31. e.printStackTrace();
  32. } catch (IOException e) {
  33. e.printStackTrace();
  34. } finally {
  35. // 关闭连接,释放资源
  36. try {
  37. httpclient.close();
  38. } catch (IOException e) {
  39. e.printStackTrace();
  40. }
  41. }
  42. }

post方式 

  1. /**
  2. * 发送 post请求访问本地应用并根据传递参数不同返回不同结果
  3. */
  4. public void post() {
  5. // 创建默认的httpClient实例.
  6. CloseableHttpClient httpclient = HttpClients.createDefault();
  7. // 创建httppost
  8. HttpPost httppost = new HttpPost("http://localhost:8080/myDemo/Ajax/serivceJ.action");
  9. // 创建参数队列
  10. List<NameValuePair> formparams = new ArrayList<NameValuePair>();
  11. formparams.add(new BasicNameValuePair("type", "house"));
  12. UrlEncodedFormEntity uefEntity;
  13. try {
  14. uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
  15. httppost.setEntity(uefEntity);
  16. System.out.println("executing request " + httppost.getURI());
  17. CloseableHttpResponse response = httpclient.execute(httppost);
  18. try {
  19. HttpEntity entity = response.getEntity();
  20. if (entity != null) {
  21. System.out.println("--------------------------------------");
  22. System.out.println("Response content: " + EntityUtils.toString(entity, "UTF-8"));
  23. System.out.println("--------------------------------------");
  24. }
  25. } finally {
  26. response.close();
  27. }
  28. } catch (ClientProtocolException e) {
  29. e.printStackTrace();
  30. } catch (UnsupportedEncodingException e1) {
  31. e1.printStackTrace();
  32. } catch (IOException e) {
  33. e.printStackTrace();
  34. } finally {
  35. // 关闭连接,释放资源
  36. try {
  37. httpclient.close();
  38. } catch (IOException e) {
  39. e.printStackTrace();
  40. }
  41. }
  42. }

post方式乱码补充 

如果有乱码,可以偿试使用 StringEntity 来替换HttpEntity:

  1. StringEntity content =new StringEntity(soapRequestData.toString(), Charset.forName("UTF-8"));// 第二个参数,设置后才会对,内容进行编码
  2. content.setContentType("application/soap+xml; charset=UTF-8");
  3. content.setContentEncoding("UTF-8");
  4. httppost.setEntity(content);

具体SOAP协议代码如下:

  1. package com.isoftstone.core.service.impl;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.FileReader;
  7. import java.io.IOException;
  8. import java.io.InputStreamReader;
  9. import java.nio.charset.Charset;
  10. import java.util.Scanner;
  11.  
  12. import org.apache.http.HttpEntity;
  13. import org.apache.http.HttpResponse;
  14. import org.apache.http.client.ClientProtocolException;
  15. import org.apache.http.client.HttpClient;
  16. import org.apache.http.client.entity.EntityBuilder;
  17. import org.apache.http.client.methods.HttpPost;
  18. import org.apache.http.entity.ContentType;
  19. import org.apache.http.entity.StringEntity;
  20. import org.apache.http.impl.client.HttpClients;
  21. import org.apache.http.util.EntityUtils;
  22. import org.apache.log4j.Logger;
  23. import org.jdom.Document;
  24. import org.jdom.Element;
  25.  
  26. import com.isoftstone.core.common.constant.RequestConstants;
  27. import com.isoftstone.core.common.tools.XmlTool;
  28. import com.isoftstone.core.service.intf.ServiceOfStringPara;
  29. /**
  30. *
  31. *
  32. */
  33. public class DeloittePricingSingleCarImpl implements ServiceOfStringPara {
  34. private String serviceUrl = "http://10.30.0.35:7001/ZSInsUW/Auto/PricingService";
  35.  
  36. private static Logger log = Logger.getLogger(DeloittePricingSingleCarImpl.class.getName());
  37.  
  38. public String invoke(String sRequest) {
  39.  
  40. StringBuffer soapRequestData = new StringBuffer();
  41. soapRequestData.append("<soapenv:Envelope");
  42. soapRequestData.append(" xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" ");
  43. soapRequestData.append(" xmlns:prov=\"http://provider.webservice.zsins.dtt.com/\">");
  44. soapRequestData.append(" <soapenv:Header/> ");
  45. soapRequestData.append("<soapenv:Body>");
  46. soapRequestData.append("<prov:executePrvPricing>");
  47. soapRequestData.append("<arg0>");
  48. soapRequestData.append("<![CDATA[" + sRequest + "]]>");
  49. soapRequestData.append("</arg0>");
  50. soapRequestData.append("</prov:executePrvPricing>");
  51. soapRequestData.append(" </soapenv:Body>");
  52. soapRequestData.append("</soapenv:Envelope>");
  53.  
  54. HttpClient httpclient = HttpClients.createDefault();
  55. HttpPost httppost = new HttpPost(serviceUrl);
  56.  
  57. StringEntity content =new StringEntity(soapRequestData.toString(), Charset.forName("UTF-8"));// 第二个参数,设置后才会对,内容进行编码
  58. content.setContentType("application/soap+xml; charset=UTF-8");
  59. content.setContentEncoding("UTF-8");
  60. httppost.setEntity(content);
  61.  
  62. //用下面的服务器端以UTF-8接收到的报文会乱码,原因未知
  63. // HttpEntity reqEntity = EntityBuilder.create().setContentType(
  64. // ContentType.TEXT_PLAIN) // .TEXT_PLAIN
  65. // .setText(soapRequestData.toString()).build();
  66. // httppost.setEntity(reqEntity);
  67. // httppost.addHeader("Content-Type",
  68. // "application/soap+xml; charset=utf-8");
  69. HttpResponse response = null;
  70. Document doc = null;
  71. String returnXml = null;
  72. String sentity = null;
  73. try {
  74. response = httpclient.execute(httppost);
  75. HttpEntity resEntity = response.getEntity();
  76. if (resEntity != null) {
  77. sentity = EntityUtils.toString(resEntity, "UTF-8");
  78. doc = XmlTool.getDocument(sentity, RequestConstants.ENCODE);
  79. System.out.println(doc.toString());
  80. Element eRoot = doc.getRootElement();
  81. Element body = eRoot.getChild("Body", eRoot.getNamespace());
  82. Element resp = (Element) body.getChildren().get(0);
  83. Element returnele = resp.getChild("return");
  84. returnXml = returnele.getText().toString();
  85. }
  86. } catch (ClientProtocolException e) {
  87. e.printStackTrace();
  88. } catch (IOException e) {
  89. e.printStackTrace();
  90. } catch (Exception e) {
  91. e.printStackTrace();
  92. } finally {
  93. log.info("发送给系统的请求报文:\n" + soapRequestData.toString());
  94. log.info("系统返回的响应报文:\n" + sentity);
  95. log.info("返回给核心的的报文:\n" + returnXml);
  96. }
  97. return returnXml;
  98. }
  99.  
  100.  
  101. public String getServiceUrl() {
  102. return serviceUrl;
  103. }
  104.  
  105.  
  106. public void setServiceUrl(String serviceUrl) {
  107. this.serviceUrl = serviceUrl;
  108. }
  109.  
  110.  
  111. public static void main(String[] args) throws Exception{
  112. File file = new File("D:/test.txt");
  113. System.out.println(file.exists());
  114.  
  115. String temp2 = null;
  116. StringBuilder sb2 = new StringBuilder();
  117. InputStreamReader isr = new InputStreamReader(new FileInputStream(file),"GBK");
  118. BufferedReader br = new BufferedReader(isr);
  119. temp2 = br.readLine();
  120.  
  121. while( temp2 != null ){
  122. sb2.append(temp2);
  123. temp2 = br.readLine();
  124. }
  125. String sss = sb2.toString();
  126. // System.out.println(sss.toString());
  127. new DeloittePricingSingleCarImpl().invoke(sss);
  128. }
  129. }
  130.  

 

post提交表单

  1. /**
  2. * post方式提交表单(模拟用户登录请求)
  3. */
  4. public void postForm() {
  5. // 创建默认的httpClient实例.
  6. CloseableHttpClient httpclient = HttpClients.createDefault();
  7. // 创建httppost
  8. HttpPost httppost = new HttpPost("http://localhost:8080/myDemo/Ajax/serivceJ.action");
  9. // 创建参数队列
  10. List<NameValuePair> formparams = new ArrayList<NameValuePair>();
  11. formparams.add(new BasicNameValuePair("username", "admin"));
  12. formparams.add(new BasicNameValuePair("password", "123456"));
  13. UrlEncodedFormEntity uefEntity;
  14. try {
  15. uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
  16. httppost.setEntity(uefEntity);
  17. System.out.println("executing request " + httppost.getURI());
  18. CloseableHttpResponse response = httpclient.execute(httppost);
  19. try {
  20. HttpEntity entity = response.getEntity();
  21. if (entity != null) {
  22. System.out.println("--------------------------------------");
  23. System.out.println("Response content: " + EntityUtils.toString(entity, "UTF-8"));
  24. System.out.println("--------------------------------------");
  25. }
  26. } finally {
  27. response.close();
  28. }
  29. } catch (ClientProtocolException e) {
  30. e.printStackTrace();
  31. } catch (UnsupportedEncodingException e1) {
  32. e1.printStackTrace();
  33. } catch (IOException e) {
  34. e.printStackTrace();
  35. } finally {
  36. // 关闭连接,释放资源
  37. try {
  38. httpclient.close();
  39. } catch (IOException e) {
  40. e.printStackTrace();
  41. }
  42. }
  43. }

文件上传

  1. /**
  2. * 上传文件
  3. */
  4. public void upload() {
  5. CloseableHttpClient httpclient = HttpClients.createDefault();
  6. try {
  7. HttpPost httppost = new HttpPost("http://localhost:8080/myDemo/Ajax/serivceFile.action");
  8.  
  9. FileBody bin = new FileBody(new File("F:\\image\\sendpix0.jpg"));
  10. StringBody comment = new StringBody("A binary file of some kind", ContentType.TEXT_PLAIN);
  11.  
  12. HttpEntity reqEntity = MultipartEntityBuilder.create().addPart("bin", bin).addPart("comment", comment).build();
  13.  
  14. httppost.setEntity(reqEntity);
  15.  
  16. System.out.println("executing request " + httppost.getRequestLine());
  17. CloseableHttpResponse response = httpclient.execute(httppost);
  18. try {
  19. System.out.println("----------------------------------------");
  20. System.out.println(response.getStatusLine());
  21. HttpEntity resEntity = response.getEntity();
  22. if (resEntity != null) {
  23. System.out.println("Response content length: " + resEntity.getContentLength());
  24. }
  25. EntityUtils.consume(resEntity);
  26. } finally {
  27. response.close();
  28. }
  29. } catch (ClientProtocolException e) {
  30. e.printStackTrace();
  31. } catch (IOException e) {
  32. e.printStackTrace();
  33. } finally {
  34. try {
  35. httpclient.close();
  36. } catch (IOException e) {
  37. e.printStackTrace();
  38. }
  39. }
  40. }

ssl连接

  1. /**
  2. * HttpClient连接SSL
  3. */
  4. public void ssl() {
  5. CloseableHttpClient httpclient = null;
  6. try {
  7. KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
  8. FileInputStream instream = new FileInputStream(new File("d:\\tomcat.keystore"));
  9. try {
  10. // 加载keyStore d:\\tomcat.keystore
  11. trustStore.load(instream, "123456".toCharArray());
  12. } catch (CertificateException e) {
  13. e.printStackTrace();
  14. } finally {
  15. try {
  16. instream.close();
  17. } catch (Exception ignore) {
  18. }
  19. }
  20. // 相信自己的CA和所有自签名的证书
  21. SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(trustStore, new TrustSelfSignedStrategy()).build();
  22. // 只允许使用TLSv1协议
  23. SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" }, null,
  24. SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
  25. httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
  26. // 创建http请求(get方式)
  27. HttpGet httpget = new HttpGet("https://localhost:8443/myDemo/Ajax/serivceJ.action");
  28. System.out.println("executing request" + httpget.getRequestLine());
  29. CloseableHttpResponse response = httpclient.execute(httpget);
  30. try {
  31. HttpEntity entity = response.getEntity();
  32. System.out.println("----------------------------------------");
  33. System.out.println(response.getStatusLine());
  34. if (entity != null) {
  35. System.out.println("Response content length: " + entity.getContentLength());
  36. System.out.println(EntityUtils.toString(entity));
  37. EntityUtils.consume(entity);
  38. }
  39. } finally {
  40. response.close();
  41. }
  42. } catch (ParseException e) {
  43. e.printStackTrace();
  44. } catch (IOException e) {
  45. e.printStackTrace();
  46. } catch (KeyManagementException e) {
  47. e.printStackTrace();
  48. } catch (NoSuchAlgorithmException e) {
  49. e.printStackTrace();
  50. } catch (KeyStoreException e) {
  51. e.printStackTrace();
  52. } finally {
  53. if (httpclient != null) {
  54. try {
  55. httpclient.close();
  56. } catch (IOException e) {
  57. e.printStackTrace();
  58. }
  59. }
  60. }
  61. }

关于RequestConfig的配置:  

源自:  

http://segmentfault.com/a/1190000000587944

http://blog.****.net/walkerjong/article/details/51710945

  1. public void requestConfig(){
  2. // 新建一个RequestConfig:
  3. RequestConfig defaultRequestConfig = RequestConfig.custom()
  4. //一、连接目标服务器超时时间:ConnectionTimeout-->指的是连接一个url的连接等待时间
  5. .setConnectTimeout(5000)
  6. //二、读取目标服务器数据超时时间:SocketTimeout-->指的是连接上一个url,获取response的返回等待时间
  7. .setSocketTimeout(5000)
  8. //三、从连接池获取连接的超时时间:ConnectionRequestTimeout
  9. .setConnectionRequestTimeout(5000)
  10. .build();
  11.  
  12. // 这个超时可以设置为客户端级别,作为所有请求的默认值:
  13. CloseableHttpClient httpclient = HttpClients.custom()
  14. .setDefaultRequestConfig(defaultRequestConfig)
  15. .build();
  16. // httpclient.execute(httppost);的时候可以让httppost直接享受到httpclient中的默认配置.
  17.  
  18. // Request不会继承客户端级别的请求配置,所以在自定义Request的时候,需要将客户端的默认配置拷贝过去:
  19. HttpGet httpget = new HttpGet("http://www.apache.org/");
  20. RequestConfig requestConfig = RequestConfig.copy(defaultRequestConfig)
  21. .setProxy(new HttpHost("myotherproxy", 8080))
  22. .build();
  23. httpget.setConfig(requestConfig);
  24. // httpget可以单独地使用新copy的requestConfig请求配置,不会对别的request请求产生影响
  25. }

httpGet或httpPost 的abort()和releaseConnection()差异

  1. //httpPost.abort();//中断请求,接下来可以开始另一段请求,所以个人理应,用这个应该可以在session中虚拟登录
  2. //httpPost.releaseConnection();//释放请求.如果释放了相当于要清空session

可知模拟登录可以如下:  源自 http://bbs.****.net/topics/390195343

  1. package com.bms.core;
  2.  
  3. import java.io.IOException;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6.  
  7. import org.apache.http.Consts;
  8. import org.apache.http.HttpEntity;
  9. import org.apache.http.HttpResponse;
  10. import org.apache.http.NameValuePair;
  11. import org.apache.http.client.ClientProtocolException;
  12. import org.apache.http.client.entity.UrlEncodedFormEntity;
  13. import org.apache.http.client.methods.HttpGet;
  14. import org.apache.http.client.methods.HttpPost;
  15. import org.apache.http.impl.client.DefaultHttpClient;
  16. import org.apache.http.message.BasicNameValuePair;
  17. import org.apache.http.util.EntityUtils;
  18.  
  19. import com.bms.util.CommonUtil;
  20.  
  21. public class Test2 {
  22.  
  23. /**
  24. * @param args
  25. * @throws IOException
  26. * @throws ClientProtocolException
  27. */
  28. public static void main(String[] args) throws ClientProtocolException, IOException {
  29. DefaultHttpClient httpclient = new DefaultHttpClient();
  30.  
  31. HttpGet httpGet = new HttpGet("http://www.baidu.com");
  32. String body = "";
  33. HttpResponse response;
  34. HttpEntity entity;
  35. response = httpclient.execute(httpGet);
  36. entity = response.getEntity();
  37. body = EntityUtils.toString(entity);//这个就是页面源码了
  38. httpGet.abort();//中断请求,接下来可以开始另一段请求
  39. System.out.println(body);
  40. //httpGet.releaseConnection();//释放请求.如果释放了相当于要清空session
  41. //以下是post方法
  42. HttpPost httpPost = new HttpPost("http://www.baidu.com");//一定要改成可以提交的地址,这里用百度代替
  43. List <NameValuePair> nvps = new ArrayList <NameValuePair>();
  44. nvps.add(new BasicNameValuePair("name", "1"));//名值对
  45. nvps.add(new BasicNameValuePair("account", "xxxx"));
  46. httpPost.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));
  47. response = httpclient.execute(httpPost);
  48. entity = response.getEntity();
  49. body = EntityUtils.toString(entity);
  50. System.out.println("Login form get: " + response.getStatusLine());//这个可以打印状态
  51. httpPost.abort();
  52. System.out.println(body);
  53. httpPost.releaseConnection();
  54. }
  55.  
  56. }

源自  http://blog.****.net/wangpeng047/article/details/19624529#reply

其它相关资料: 非CloseableHttpClient  HTTPClient模块的HttpGet和HttpPost

HttpClient 4.3教程

httpclient异常情况分析

我项目中用到的HttpClientUtil (2016/12/17)

  1. package com.isoftstone.pcis.isc.util;
  2.  
  3. import java.io.IOException;
  4. import java.io.InterruptedIOException;
  5. import java.net.UnknownHostException;
  6.  
  7. import javax.net.ssl.SSLException;
  8. import javax.net.ssl.SSLHandshakeException;
  9.  
  10. import org.apache.http.HttpEntityEnclosingRequest;
  11. import org.apache.http.HttpRequest;
  12. import org.apache.http.NoHttpResponseException;
  13. import org.apache.http.client.HttpRequestRetryHandler;
  14. import org.apache.http.client.protocol.HttpClientContext;
  15. import org.apache.http.config.Registry;
  16. import org.apache.http.config.RegistryBuilder;
  17. import org.apache.http.conn.ConnectTimeoutException;
  18. import org.apache.http.conn.socket.ConnectionSocketFactory;
  19. import org.apache.http.conn.socket.LayeredConnectionSocketFactory;
  20. import org.apache.http.conn.socket.PlainConnectionSocketFactory;
  21. import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
  22. import org.apache.http.impl.client.CloseableHttpClient;
  23. import org.apache.http.impl.client.HttpClients;
  24. import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
  25. import org.apache.http.protocol.HttpContext;
  26. import com.isoftstone.pcis.isc.util.ProperUtil;
  27.  
  28. public class HttpClientUtil {
  29.  
  30. private static CloseableHttpClient httpclient = null;
  31.  
  32. static final int maxTotal=Integer.valueOf(ProperUtil.get("maxTotal")).intValue();//总最大连接数
  33. static final int defaultMaxPerRoute=Integer.valueOf(ProperUtil.get("corePoolSize")).intValue();//每条线路最大连接数 = 本系统核心线程数 , 这样永远不会超过最大连接
  34.  
  35. public static CloseableHttpClient getHttpClient() {
  36.  
  37. if (null == httpclient) {
  38. synchronized (HttpClientUtil.class) {
  39. if (null == httpclient) {
  40. httpclient = getNewHttpClient();
  41. }
  42. }
  43. }
  44.  
  45. return httpclient;
  46. }
  47.  
  48. private static CloseableHttpClient getNewHttpClient() {
  49.  
  50.  
  51. // 设置连接池
  52. ConnectionSocketFactory plainsf = PlainConnectionSocketFactory.getSocketFactory();
  53. LayeredConnectionSocketFactory sslsf = SSLConnectionSocketFactory.getSocketFactory();
  54. Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory> create().register("http", plainsf).register("https", sslsf).build();
  55. PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registry);
  56. // 配置最大连接数
  57. cm.setMaxTotal(maxTotal);
  58. // 配置每条线路的最大连接数
  59. cm.setDefaultMaxPerRoute(defaultMaxPerRoute);
  60.  
  61. // 请求重试处理
  62. HttpRequestRetryHandler httpRequestRetryHandler = new HttpRequestRetryHandler() {
  63. @Override
  64. public boolean retryRequest(IOException exception,
  65. int executionCount, HttpContext context) {
  66. if (executionCount >= 2) {// 如果已经重试了2次,就放弃
  67. return false;
  68. }
  69. if (exception instanceof NoHttpResponseException) {// 如果服务器丢掉了连接,那么就重试
  70. return true;
  71. }
  72. if (exception instanceof SSLHandshakeException) {// 不要重试SSL握手异常
  73. return false;
  74. }
  75. if (exception instanceof InterruptedIOException) {// 超时
  76. return false;
  77. }
  78. if (exception instanceof UnknownHostException) {// 目标服务器不可达
  79. return false;
  80. }
  81. if (exception instanceof ConnectTimeoutException) {// 连接被拒绝
  82. return false;
  83. }
  84. if (exception instanceof SSLException) {// SSL握手异常
  85. return false;
  86. }
  87.  
  88. HttpClientContext clientContext = HttpClientContext.adapt(context);
  89. HttpRequest request = clientContext.getRequest();
  90.  
  91. if (!(request instanceof HttpEntityEnclosingRequest)) {
  92. return true;
  93. }
  94. return false;
  95. }
  96.  
  97. };
  98.  
  99. CloseableHttpClient newHttpclient=null;
  100.  
  101. newHttpclient = HttpClients.custom()
  102. .setConnectionManager(cm)
  103. // .setDefaultRequestConfig(requestConfig)
  104. .setRetryHandler(httpRequestRetryHandler)
  105. .build();
  106.  
  107. return newHttpclient;
  108. }
  109. }

我自己整理的HttpClientTool

  1. package com.isoftstone.core.util;
  2.  
  3. import java.io.IOException;
  4. import java.io.InterruptedIOException;
  5. import java.net.UnknownHostException;
  6.  
  7. import javax.net.ssl.SSLException;
  8. import javax.net.ssl.SSLHandshakeException;
  9.  
  10. import org.apache.http.HttpEntityEnclosingRequest;
  11. import org.apache.http.HttpHost;
  12. import org.apache.http.HttpRequest;
  13. import org.apache.http.NoHttpResponseException;
  14. import org.apache.http.client.HttpRequestRetryHandler;
  15. import org.apache.http.client.config.RequestConfig;
  16. import org.apache.http.client.protocol.HttpClientContext;
  17. import org.apache.http.config.Registry;
  18. import org.apache.http.config.RegistryBuilder;
  19. import org.apache.http.conn.ConnectTimeoutException;
  20. import org.apache.http.conn.routing.HttpRoute;
  21. import org.apache.http.conn.socket.ConnectionSocketFactory;
  22. import org.apache.http.conn.socket.LayeredConnectionSocketFactory;
  23. import org.apache.http.conn.socket.PlainConnectionSocketFactory;
  24. import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
  25. import org.apache.http.impl.client.CloseableHttpClient;
  26. import org.apache.http.impl.client.HttpClients;
  27. import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
  28. import org.apache.http.protocol.HttpContext;
  29.  
  30. /**
  31. * org.apache.http.impl.client.CloseableHttpClient链接池生成工具
  32. * @reference http://www.cnblogs.com/whatlonelytear/articles/4835538.html
  33. * @author King
  34. * @date 20170601
  35. */
  36. public class HttpClientTool {
  37.  
  38. // org.apache.http.impl.client.CloseableHttpClient
  39. private static CloseableHttpClient httpclient = null;
  40.  
  41. // 这里就直接默认固定了,因为以下三个参数在新建的method中仍然可以重新配置并被覆盖.
  42. static final int connectionRequestTimeout = 5000;// ms毫秒,从池中获取链接超时时间
  43. static final int connectTimeout = 5000;// ms毫秒,建立链接超时时间
  44. static final int socketTimeout = 30000;// ms毫秒,读取超时时间
  45.  
  46. // 总配置,主要涉及是以下两个参数,如果要作调整没有用到properties会比较后麻烦,但鉴于一经粘贴,随处可用的特点,就不再做依赖性配置化处理了.
  47. // 而且这个参数同一家公司基本不会变动.
  48. static final int maxTotal = 500;// 最大总并发,很重要的参数
  49. static final int maxPerRoute = 100;// 每路并发,很重要的参数
  50.  
  51. // 正常情况这里应该配成MAP或LIST
  52. // 细化配置参数,用来对每路参数做精细化处理,可以管控各ip的流量,比如默认配置请求baidu:80端口最大100个并发链接,
  53. static final String detailHostName = "http://www.baidu.com";// 每个细化配置之ip(不重要,在特殊场景很有用)
  54. static final int detailPort = 80;// 每个细化配置之port(不重要,在特殊场景很有用)
  55. static final int detailMaxPerRoute = 100;// 每个细化配置之最大并发数(不重要,在特殊场景很有用)
  56.  
  57. public static CloseableHttpClient getHttpClient() {
  58. if (null == httpclient) {
  59. synchronized (HttpClientTool.class) {
  60. if (null == httpclient) {
  61. httpclient = init();
  62. }
  63. }
  64. }
  65. return httpclient;
  66. }
  67.  
  68. /**
  69. * 链接池初始化 这里最重要的一点理解就是. 让CloseableHttpClient 一直活在池的世界里, 但是HttpPost却一直用完就消掉.
  70. * 这样可以让链接一直保持着.
  71. *
  72. * @return
  73. */
  74. private static CloseableHttpClient init() {
  75. CloseableHttpClient newHttpclient = null;
  76.  
  77. // 设置连接池
  78. ConnectionSocketFactory plainsf = PlainConnectionSocketFactory.getSocketFactory();
  79. LayeredConnectionSocketFactory sslsf = SSLConnectionSocketFactory.getSocketFactory();
  80. Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory> create().register("http", plainsf).register("https", sslsf).build();
  81. PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registry);
  82. // 将最大连接数增加
  83. cm.setMaxTotal(maxTotal);
  84. // 将每个路由基础的连接增加
  85. cm.setDefaultMaxPerRoute(maxPerRoute);
  86.  
  87. // 细化配置开始,其实这里用Map或List的for循环来配置每个链接,在特殊场景很有用.
  88. // 将每个路由基础的连接做特殊化配置,一般用不着
  89. HttpHost httpHost = new HttpHost(detailHostName, detailPort);
  90. // 将目标主机的最大连接数增加
  91. cm.setMaxPerRoute(new HttpRoute(httpHost), detailMaxPerRoute);
  92. // cm.setMaxPerRoute(new HttpRoute(httpHost2),
  93. // detailMaxPerRoute2);//可以有细化配置2
  94. // cm.setMaxPerRoute(new HttpRoute(httpHost3),
  95. // detailMaxPerRoute3);//可以有细化配置3
  96. // 细化配置结束
  97.  
  98. // 请求重试处理
  99. HttpRequestRetryHandler httpRequestRetryHandler = new HttpRequestRetryHandler() {
  100. @Override
  101. public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
  102. if (executionCount >= 2) {// 如果已经重试了2次,就放弃
  103. return false;
  104. }
  105. if (exception instanceof NoHttpResponseException) {// 如果服务器丢掉了连接,那么就重试
  106. return true;
  107. }
  108. if (exception instanceof SSLHandshakeException) {// 不要重试SSL握手异常
  109. return false;
  110. }
  111. if (exception instanceof InterruptedIOException) {// 超时
  112. return false;
  113. }
  114. if (exception instanceof UnknownHostException) {// 目标服务器不可达
  115. return false;
  116. }
  117. if (exception instanceof ConnectTimeoutException) {// 连接被拒绝
  118. return false;
  119. }
  120. if (exception instanceof SSLException) {// SSL握手异常
  121. return false;
  122. }
  123.  
  124. HttpClientContext clientContext = HttpClientContext.adapt(context);
  125. HttpRequest request = clientContext.getRequest();
  126. // 如果请求是幂等的,就再次尝试
  127. if (!(request instanceof HttpEntityEnclosingRequest)) {
  128. return true;
  129. }
  130. return false;
  131. }
  132. };
  133.  
  134. // 配置请求的超时设置
  135. RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(connectionRequestTimeout).setConnectTimeout(connectTimeout).setSocketTimeout(socketTimeout).build();
  136. newHttpclient = HttpClients.custom().setConnectionManager(cm).setDefaultRequestConfig(requestConfig).setRetryHandler(httpRequestRetryHandler).build();
  137. return newHttpclient;
  138. }
  139. }
  1. package com.alqsoft.utils;
  2.  
  3.  
  4. import java.io.BufferedReader;
  5. import java.io.IOException;
  6. import java.io.UnsupportedEncodingException;
  7. import java.net.URI;
  8. import java.net.URISyntaxException;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11.  
  12.  
  13. import org.apache.commons.logging.Log;
  14. import org.apache.commons.logging.LogFactory;
  15. import org.apache.http.HttpEntity;
  16. import org.apache.http.HttpResponse;
  17. import org.apache.http.NameValuePair;
  18. import org.apache.http.ParseException;
  19. import org.apache.http.client.ClientProtocolException;
  20. import org.apache.http.client.config.CookieSpecs;
  21. import org.apache.http.client.config.RequestConfig;
  22. import org.apache.http.client.entity.UrlEncodedFormEntity;
  23. import org.apache.http.client.methods.CloseableHttpResponse;
  24. import org.apache.http.client.methods.HttpGet;
  25. import org.apache.http.client.methods.HttpPost;
  26. import org.apache.http.client.utils.URIBuilder;
  27. import org.apache.http.config.Registry;
  28. import org.apache.http.config.RegistryBuilder;
  29. import org.apache.http.cookie.Cookie;
  30. import org.apache.http.cookie.CookieOrigin;
  31. import org.apache.http.cookie.CookieSpec;
  32. import org.apache.http.cookie.CookieSpecProvider;
  33. import org.apache.http.cookie.MalformedCookieException;
  34. import org.apache.http.impl.client.BasicCookieStore;
  35. import org.apache.http.impl.client.CloseableHttpClient;
  36. import org.apache.http.impl.client.HttpClients;
  37. import org.apache.http.impl.cookie.BestMatchSpecFactory;
  38. import org.apache.http.impl.cookie.BrowserCompatSpec;
  39. import org.apache.http.impl.cookie.BrowserCompatSpecFactory;
  40. import org.apache.http.message.BasicNameValuePair;
  41. import org.apache.http.protocol.HttpContext;
  42. import org.apache.http.util.EntityUtils;
  43.  
  44.  
  45. public class HttpClientObject {
  46. private static Log logger = LogFactory.getLog(HttpClientObject.class);
  47. private CloseableHttpClient httpClient = null;
  48. private HttpResponse response;
  49.  
  50. private HttpPost httpPost = null;
  51.  
  52. private HttpGet httpGet = null;
  53.  
  54. private String paramKey = "";
  55.  
  56. private String paramValue = "";
  57.  
  58. private String responseString;
  59.  
  60. public void setParamKey(String paramKey) {
  61. this.paramKey = paramKey;
  62. }
  63.  
  64. public void setParamValue(String paramValue) {
  65. this.paramValue = paramValue;
  66. }
  67.  
  68. public String getResponseString() {
  69. return responseString;
  70. }
  71.  
  72. public HttpClientObject() {
  73. this.getHttpClient();
  74. }
  75.  
  76. private List<NameValuePair> getRequestBody() {
  77. NameValuePair pair1 = new BasicNameValuePair(paramKey, paramValue);
  78. List<NameValuePair> pairList = new ArrayList<NameValuePair>();
  79. pairList.add(pair1);
  80. return pairList;
  81. }
  82.  
  83.  
  84. public void submit() {
  85. try {
  86. if (httpPost != null) {
  87. response = httpClient.execute(httpPost);
  88. httpPost = null;
  89. }
  90. if (httpGet != null) {
  91. response = httpClient.execute(httpGet);
  92. httpGet = null;
  93. }
  94. this.response();
  95. } catch (ClientProtocolException e) {
  96. e.printStackTrace();
  97. } catch (IOException e) {
  98. e.printStackTrace();
  99. }
  100. }
  101.  
  102.  
  103. private void response() {
  104. String result = "";
  105. BufferedReader in = null;
  106. try {
  107. HttpEntity httpEntity = response.getEntity();
  108. responseString = EntityUtils.toString(httpEntity);
  109. } catch (ClientProtocolException e) {
  110. e.printStackTrace();
  111. } catch (IOException e) {
  112. e.printStackTrace();
  113. } finally {
  114. try {
  115. if (in != null) {
  116. in.close();
  117. }
  118. } catch (Exception e2) {
  119. e2.printStackTrace();
  120. }
  121. }
  122.  
  123.  
  124. }
  125.  
  126.  
  127. public void setPost(String httpUrl) {
  128. try {
  129. HttpEntity requestHttpEntity = new UrlEncodedFormEntity(this.getRequestBody());
  130. httpPost = new HttpPost(httpUrl);
  131. httpPost.addHeader("Content-Type", "”application/json;charset=UTF-8");
  132. httpPost.setEntity(requestHttpEntity);
  133. } catch (UnsupportedEncodingException e) {
  134. e.printStackTrace();
  135. } catch (ParseException e) {
  136. e.printStackTrace();
  137. }
  138. }
  139.  
  140.  
  141. public void setGet(String httpUrl) {
  142. httpGet = new HttpGet(httpUrl);
  143. httpGet.addHeader("Content-Type", "text/html;charset=UTF-8");
  144. }
  145.  
  146.  
  147. private void getHttpClient() {
  148. BasicCookieStore cookieStore = new BasicCookieStore();
  149. CookieSpecProvider easySpecProvider = new CookieSpecProvider() {
  150. public CookieSpec create(HttpContext context) {
  151.  
  152.  
  153. return new BrowserCompatSpec() {
  154. @Override
  155. public void validate(Cookie cookie, CookieOrigin origin)
  156. throws MalformedCookieException {
  157. // Oh, I am easy
  158. }
  159. };
  160. }
  161.  
  162.  
  163. };
  164. Registry<CookieSpecProvider> r = RegistryBuilder.<CookieSpecProvider>create()
  165. .register(CookieSpecs.BEST_MATCH, new BestMatchSpecFactory())
  166. .register(CookieSpecs.BROWSER_COMPATIBILITY, new BrowserCompatSpecFactory())
  167. .register("easy", easySpecProvider).build();
  168.  
  169.  
  170. RequestConfig requestConfig = RequestConfig.custom().setCookieSpec("easy")
  171. .setSocketTimeout(20000).setConnectTimeout(20000).build();
  172.  
  173.  
  174. httpClient = HttpClients.custom().setDefaultCookieSpecRegistry(r)
  175. .setDefaultRequestConfig(requestConfig).setDefaultCookieStore(cookieStore).build();
  176. }
  177.  
  178.  
  179. /**
  180. * httpclient发送http get请求;
  181. *
  182. * @return
  183. * @throws URISyntaxException
  184. * @throws IOException
  185. * @throws ClientProtocolException
  186. */
  187. public static String sendGet(String uriPath, List<NameValuePair> ns)
  188. throws URISyntaxException, ClientProtocolException, IOException {
  189. CloseableHttpClient httpclient = HttpClients.createDefault();
  190. URIBuilder uri = new URIBuilder();
  191. uri.setPath(uriPath);
  192. uri.addParameters(ns);
  193. URI u = uri.build();
  194. HttpGet httpget = new HttpGet(u);
  195. CloseableHttpResponse response = httpclient.execute(httpget);
  196. return EntityUtils.toString(response.getEntity());
  197. }
  198.  
  199.  
  200. /**
  201. * httpclient发送http post请求;
  202. *
  203. * @return
  204. * @throws URISyntaxException
  205. * @throws IOException
  206. * @throws ClientProtocolException
  207. */
  208. public static String sendPost(String uriPath, List<NameValuePair> ns)
  209. throws URISyntaxException, ClientProtocolException, IOException {
  210. CloseableHttpClient httpclient = HttpClients.createDefault();
  211. URIBuilder uri = new URIBuilder();
  212. uri.setPath(uriPath);
  213. uri.addParameters(ns);
  214. URI u = uri.build();
  215. HttpPost httpPost = new HttpPost(u);
  216. CloseableHttpResponse response = httpclient.execute(httpPost);
  217. return EntityUtils.toString(response.getEntity());
  218. }
  219.  
  220. }
  221.  
  222.  
  223. 使用方法如下:
  224.  
  225. String signUrl= ”路径“;
  226.  
  227. try{
  228. List<NameValuePair> nameValuePairs=new ArrayList<NameValuePair>();
  229. nameValuePairs.add(new BasicNameValuePair("phone",phone));
  230. nameValuePairs.add(new BasicNameValuePair("xytId",String.valueOf(xytId)));
  231. nameValuePairs.add(new BasicNameValuePair("balance",balance));
  232.  
  233. String result=HttpClientObject.sendPost(signUrl,nameValuePairs);
  234. logger.info("result: = "+result);
  235. JSONObject jsonObject=JSON.parseObject(result);
  236.  
  237. Integer code=Integer.valueOf(jsonObject.getString("code"));
  238. if(code.equals(0)){
  239. code1=0L;
  240. }else if(code.equals(1)){
  241. code1=1L;
  242. }else if(code.equals(3)){
  243. code1=3L;
  244. }
  245. }catch(Exception e){
  246. e.printStackTrace();
  247. }
上一篇:HttpClient--使用HttpClient进行Get Post请求访问


下一篇:MVC,API 使用(强类型)