一.https忽略证书

  1. /**
  2. * 用于进行Https请求的HttpClient
  3. *
  4. * @author joey
  5. *
  6. */
  7. public class SSLClient {
  8. public static CloseableHttpClient createSSLClientDefault(){
  9. try {
  10. SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
  11. //信任所有
  12. public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
  13. return true;
  14. }
  15. }).build();
  16. SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
  17. return HttpClients.custom().setSSLSocketFactory(sslsf).build();
  18. } catch (KeyManagementException e) {
  19. e.printStackTrace();
  20. } catch (NoSuchAlgorithmException e) {
  21. e.printStackTrace();
  22. } catch (KeyStoreException e) {
  23. e.printStackTrace();
  24. }
  25. return HttpClients.createDefault();
  26. }
  27.  
  28. }

二.post与get请求

  1. /**
  2. * 利用HttpClient的工具类
  3. *
  4. * @author Joey
  5. *
  6. */
  7. public class HttpClientUtil {
  8.  
  9. private static String charSet = "UTF-8";
  10. private static CloseableHttpClient httpClient = null;
  11. private static CloseableHttpResponse response = null;
  12.  
  13. /**
  14. * https的post请求
  15. * @param url
  16. * @param jsonstr
  17. * @param charset
  18. * @return
  19. */
  20. public static String doHttpsPost(String url, String jsonStr) {
  21. try {
  22. httpClient = SSLClient.createSSLClientDefault();
  23. HttpPost httpPost = new HttpPost(url);
  24. httpPost.setHeader("Content-Type", "application/json");
  25.  
  26. StringEntity se = new StringEntity(jsonStr);
  27. se.setContentType("text/json");
  28. se.setContentEncoding(new BasicHeader("Content-Type", "application/json"));
  29. httpPost.setEntity(se);
  30.  
  31. response = httpClient.execute(httpPost);
  32. if (response != null) {
  33. HttpEntity resEntity = response.getEntity();
  34. if (resEntity != null) {
  35. return EntityUtils.toString(resEntity, charSet);
  36. }
  37. }
  38. } catch (Exception ex) {
  39. ex.printStackTrace();
  40. }finally {
  41. if(httpClient != null){
  42. try {
  43. httpClient.close();
  44. } catch (IOException e) {
  45. e.printStackTrace();
  46. }
  47. }
  48. if(response != null){
  49. try {
  50. response.close();
  51. } catch (IOException e) {
  52. e.printStackTrace();
  53. }
  54. }
  55. }
  56. return null;
  57. }
  58.  
  59. /**
  60. * http的post请求(用于key-value格式的参数)
  61. * @param url
  62. * @param param
  63. * @return
  64. */
  65. public static String doHttpPost(String url,Map<String,String> param){
  66. try {
  67. //请求发起客户端
  68. httpClient = HttpClients.createDefault();
  69. //参数集合
  70. List<NameValuePair> postParams = new ArrayList<NameValuePair>();
  71. //遍历参数并添加到集合
  72. for(Map.Entry<String, String> entry:param.entrySet()){
  73. postParams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
  74. }
  75.  
  76. //通过post方式访问
  77. HttpPost post = new HttpPost(url);
  78. HttpEntity paramEntity = new UrlEncodedFormEntity(postParams,charSet);
  79. post.setEntity(paramEntity);
  80. response = httpClient.execute(post);
  81. StatusLine status = response.getStatusLine();
  82. int state = status.getStatusCode();
  83. if (state == HttpStatus.SC_OK) {
  84. HttpEntity valueEntity = response.getEntity();
  85. String content = EntityUtils.toString(valueEntity);
  86. //jsonObject = JSONObject.fromObject(content);
  87. return content;
  88. }
  89. } catch (UnsupportedEncodingException e) {
  90. e.printStackTrace();
  91. } catch (ClientProtocolException e) {
  92. e.printStackTrace();
  93. } catch (IOException e) {
  94. e.printStackTrace();
  95. }finally{
  96. if(httpClient != null){
  97. try {
  98. httpClient.close();
  99. } catch (IOException e) {
  100. e.printStackTrace();
  101. }
  102. }
  103. if(response != null){
  104. try {
  105. response.close();
  106. } catch (IOException e) {
  107. e.printStackTrace();
  108. }
  109. }
  110. }
  111. return null;
  112. }
  113.  
  114. /**
  115. * http的post请求(用于请求json格式的参数)
  116. * @param url
  117. * @param params
  118. * @return
  119. */
  120. public static String doHttpPost(String url, String jsonStr) {
  121. try {
  122. httpClient = HttpClients.createDefault();
  123.  
  124. // 创建httpPost
  125. HttpPost httpPost = new HttpPost(url);
  126. httpPost.setHeader("Accept", "application/json");
  127.  
  128. StringEntity entity = new StringEntity(jsonStr, charSet);
  129. entity.setContentType("text/json");
  130. entity.setContentEncoding(new BasicHeader("Content-Type", "application/json"));
  131. httpPost.setEntity(entity);
  132. //发送post请求
  133. response = httpClient.execute(httpPost);
  134. if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
  135. HttpEntity responseEntity = response.getEntity();
  136. String jsonString = EntityUtils.toString(responseEntity);
  137. return jsonString;
  138. }
  139. }catch(Exception e) {
  140. e.printStackTrace();
  141. }finally {
  142. if(httpClient != null){
  143. try {
  144. httpClient.close();
  145. } catch (IOException e) {
  146. e.printStackTrace();
  147. }
  148. }
  149. if(response != null){
  150. try {
  151. response.close();
  152. } catch (IOException e) {
  153. e.printStackTrace();
  154. }
  155. }
  156. }
  157. return null;
  158. }
  159.  
  160. /**
  161. * http的Get请求
  162. * @param url
  163. * @param param
  164. * @return
  165. */
  166. public static String doHttpGet(String url,Map<String,String> param) {
  167. CloseableHttpClient httpclient = null;
  168. CloseableHttpResponse response = null;
  169.  
  170. try {
  171. httpclient = HttpClients.createDefault();
  172. if(param != null && !param.isEmpty()) {
  173. //参数集合
  174. List<NameValuePair> getParams = new ArrayList<NameValuePair>();
  175. for(Map.Entry<String, String> entry:param.entrySet()){
  176. getParams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
  177. }
  178. url +="?"+EntityUtils.toString(new UrlEncodedFormEntity(getParams), "UTF-8");
  179. }
  180. //发送gey请求
  181. HttpGet httpGet = new HttpGet(url);
  182. response = httpclient.execute(httpGet);
  183. if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
  184. return EntityUtils.toString(response.getEntity());
  185. }
  186. }catch(Exception e) {
  187. e.printStackTrace();
  188. }finally{
  189. if(httpclient != null){
  190. try {
  191. httpclient.close();
  192. } catch (IOException e) {
  193. e.printStackTrace();
  194. }
  195. }
  196. if(response != null){
  197. try {
  198. response.close();
  199. } catch (IOException e) {
  200. e.printStackTrace();
  201. }
  202. }
  203. }
  204. return null;
  205. }
  206.  
  207. }

封装HttpClient进行http请求与https请求的更多相关文章

  1. httpclient绕过证书验证进行HTTPS请求

    http请求是我们常用的一种web应用的应用层协议,但是由于它的不安全性,现在正在逐渐向https协议过渡.https协议是在http的基础上进行了隧道加密,加密方式有SSL和TLS两种.当serve ...

  2. httpclient 3.1跳过https请求SSL的验证

    一.因为在使用https发送请求的时候会涉及,验证方式.但是这种方式在使用的时候很不方便.特别是在请求外部接口的时候,所以这我写了一个跳过验证的方式.(供参考) 二.加入包,这里用的是commons- ...

  3. 支持https请求以及https请求的抓包

    iOS9推出的时候,苹果希望大家使用https协议,来提高数据传输之间的安全性.下面我就从最简单的代码介绍,如何在工程中设置,来支持https的请求. 一.证书准备篇 1.证书转换 在服务器人员,给你 ...

  4. Java 使用代理发送Http请求 (将Http请求代理Https请求)

    package com.test.porxy; import java.io.BufferedReader; import java.io.IOException; import java.io.In ...

  5. HttpClient 之 发送Https请求

    HttpClient包是一个优秀的Http请求的开源jar. 本文Http工具类的封装基于HttpClient,封装后的工具类支持Https请求. 但是由于项目的需要快速的实现,以下代码还可能会有点过 ...

  6. [PHP自动化-进阶]003.CURL处理Https请求访问

    引言:继前文<模拟登录并采集数据>,<模拟登录带有验证码的网站>,大家对CURL基本上已经有了认识,这一讲简单的说一下请求Https. 在很多的站点,如TalkingData, ...

  7. SPRING IN ACTION 第4版笔记-第九章Securing web applications-011-把敏感信息请求转为https(requiresChannel())

    1.把包含敏感信息的请求转为https请求,则较为安全,但如何只把有需要安全的请求转为https,而不是不加分辩就把所有请求都转为https呢?可以用requiresChannel() @Overri ...

  8. AFNetWorking3.0使用 自签名证书的https请求

    前几日,项目组出于安全角度的考虑,要求项目中的请求使用https请求,因为是企业内部使用的app,因此使用了自签名的证书,而自签名的证书是不受信任的,所以我们就需要自己来做证书的验证,包括服务器验证客 ...

  9. Volley框架支持HTTPS请求。

    第一次写帖子,嘿嘿. 最近了解到google2013IO大会出了个网络框架,正好项目也需要用到,就看了下. 最后发现接口都是HTTPS的,但是Volley默认是不支持HTTPS,网上找了好久,都没有对 ...

随机推荐

  1. 面试题(JVM加载机制)

    JVM加载class文件的原理机制? JVM中类的装载是classLoader 和它的子类来实现的,Java classLoader是个重要的java运行时的系统组件.它在运行时查找和装入类文件的类. ...

  2. ios基础-分辨率适配

    (一)分辨率定义 分辨率,是指单位长度内包括的像素点的数量,它的单位通常为像素/英寸(ppi).描写叙述分辨率的单位有:(dpi点每英寸).lpi(线每英寸)和ppi(像素每英寸). (二)ios分辨 ...

  3. ACM-SG函数之Fibonacci again and again——hdu1848

    Fibonacci again and again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav ...

  4. Drupal 自己定义主题实体 Theming Custom Entities

    在自己定义主题中输出结果时,有三个部分或很多其它特殊的函数.如 hook_menu,Page Callback.MODULE_theme 钩子 1.hook_menu 为了使用自己定义的实体.像创建. ...

  5. java生成6位随机数的5种方法

    转自:https://blog.csdn.net/u012491783/article/details/76862526/

  6. windows安装gnvm安装教程,node多版本解决方案

    本文是实现windows下node多版本管理 Win10专业版 一.安装前准备 安装前请卸载node相关的所有东西!!! 二.gnvm下载 gnvm搜索 http://ksria.com/gnvm/ ...

  7. WPF向系统发送消息 并传递结构体

    场景 :需要开发一个通讯组件 流程为:界面-开启接收服务-通过发送组件发送信息到 其他客户端和服务端 接受服务接收其他客户端发送的消息 需要传递给对应组件或者界面 因此会出现类库重复引用问题.因为采用 ...

  8. vue中采用axios发送请求及拦截器

    这几天在使用vue中axios发送get请求的时候很顺手,但是在发送post请求的时候老是在成功的回调函数里边返回参数不存在,当时就纳闷了,经过查阅资料,终于得到了解决方案,在此做一总结: 首先我们在 ...

  9. 深入了解React组件重新渲染的条件和生命周期

    React组件rerender的真正条件 当前组件的State中的属性改变时且当前组件的shouldcomponentupdate返回true,那么当前组件会rerender 组件的props中的任一 ...

  10. 「JavaSE 重新出发」05.03.02 在运行时使用反射分析对象

    在编写程序时,如果知道想要查看的域名和类型,查看指定的域是一个很容易的事,而利用反射机制可以查看在编译时还不清楚的对象域. java Employee tank = new Employee(&quo ...