• java https请求,https请求如何调用


    https协议(Secure Hypertext Transfer Protocol) :

    安全超文本传输协议, HTTPS以保密为目标研发, 简单讲HTTPS协议是由SSL+HTTP协议构建的可进行加密传输、 身份认证的网络协议, 其安全基础是SSL协议, 因此加密的详细内容请看SSL。 全称Hypertext Transfer Protocol overSecure Socket Layer。句法类同http:体系。 用于安全的HTTP数据传输。 https:URL表明它使用了HTTP, 但HTTPS存在不同于HTTP的默认端口及一个加密/身份验证层(在HTTP与TCP之间)。

    HTTPS和HTTP的区别:

    一、 https协议需要到ca申请证书, 一般免费证书很少, 需要交费。

    二、 http是超文本传输协议, 信息是明文传输, https 则是具有安全性的ssl加密传输协议。

    三、 http和https使用的是完全不同的连接方式, 用的端口也不一样, 前者是80,后者是443。

    四、 http的连接很简单, 是无状态的; HTTPS协议是由SSL+HTTP协议构建的可进行加密传输、 身份认证的网络协议, 比http协议安全。

    问题描述:

    当在网页中访问http请求的时候网页会自动将http请求转换为https请求 并提示如下:

     并且采用http那个链接采用postman请求接口时不通,那么可将原接口修改为https的请求 测试成功。

    那么代码中如何调用呢?

    直接上代码 工具类如下:

    1. import java.io.IOException;
    2. import java.nio.charset.Charset;
    3. import java.security.KeyManagementException;
    4. import java.security.KeyStoreException;
    5. import java.security.NoSuchAlgorithmException;
    6. import java.security.cert.CertificateException;
    7. import java.security.cert.X509Certificate;
    8. import java.util.ArrayList;
    9. import java.util.HashMap;
    10. import java.util.List;
    11. import java.util.Map;
    12. import javax.net.ssl.HostnameVerifier;
    13. import javax.net.ssl.SSLContext;
    14. import net.sf.json.JSONObject;
    15. import org.apache.http.HttpEntity;
    16. import org.apache.http.NameValuePair;
    17. import org.apache.http.client.entity.UrlEncodedFormEntity;
    18. import org.apache.http.client.methods.CloseableHttpResponse;
    19. import org.apache.http.client.methods.HttpPost;
    20. import org.apache.http.conn.ssl.NoopHostnameVerifier;
    21. import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
    22. import org.apache.http.conn.ssl.TrustStrategy;
    23. import org.apache.http.entity.StringEntity;
    24. import org.apache.http.impl.client.CloseableHttpClient;
    25. import org.apache.http.impl.client.HttpClients;
    26. import org.apache.http.message.BasicNameValuePair;
    27. import org.apache.http.ssl.SSLContextBuilder;
    28. import org.apache.http.util.EntityUtils;
    29. import org.slf4j.Logger;
    30. import org.slf4j.LoggerFactory;
    31. public class HttpsUtils {
    32. private static Logger logger = LoggerFactory.getLogger(HttpsUtils.class);
    33. static CloseableHttpClient httpClient;
    34. static CloseableHttpResponse httpResponse;
    35. public static CloseableHttpClient createSSLClientDefault() {
    36. try {
    37. SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
    38. // 信任所有
    39. public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
    40. return true;
    41. }
    42. }).build();
    43. HostnameVerifier hostnameVerifier = NoopHostnameVerifier.INSTANCE;
    44. SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, hostnameVerifier);
    45. return HttpClients.custom().setSSLSocketFactory(sslsf).build();
    46. } catch (KeyManagementException e) {
    47. e.printStackTrace();
    48. } catch (NoSuchAlgorithmException e) {
    49. e.printStackTrace();
    50. } catch (KeyStoreException e) {
    51. e.printStackTrace();
    52. }
    53. return HttpClients.createDefault();
    54. }
    55. /**
    56. * 发送https请求
    57. *
    58. * @throws Exception
    59. */
    60. public static String sendByHttp(JSONObject jsonObject, String url) {
    61. try {
    62. HttpPost httpPost = new HttpPost(url);
    63. httpPost.addHeader("Content-type", "application/json; charset=utf-8");
    64. httpPost.setHeader("Accept", "application/json");
    65. httpPost.setEntity(new StringEntity(jsonObject.toString(), Charset.forName("UTF-8")));
    66. httpClient = HttpsUtils.createSSLClientDefault();
    67. httpResponse = httpClient.execute(httpPost);
    68. HttpEntity httpEntity = httpResponse.getEntity();
    69. if (httpEntity != null) {
    70. String jsObject = EntityUtils.toString(httpEntity, "UTF-8");
    71. return jsObject;
    72. } else {
    73. return null;
    74. }
    75. } catch (Exception e) {
    76. e.printStackTrace();
    77. return null;
    78. } finally {
    79. try {
    80. httpResponse.close();
    81. httpClient.close();
    82. logger.info("请求流关闭完成");
    83. } catch (IOException e) {
    84. logger.info("请求流关闭出错");
    85. e.printStackTrace();
    86. }
    87. }
    88. }
    89. /**
    90. * 发送https get请求
    91. *
    92. * @throws Exception
    93. */
    94. public static String sendByHttpGet(Map params, String url) {
    95. try {
    96. URIBuilder uriBuilder = new URIBuilder(url);
    97. if(MapUtils.isNotEmpty(params)) {
    98. for (Map.Entry entry : params.entrySet()) {
    99. uriBuilder.setParameter(entry.getKey(), entry.getValue().toString());
    100. }
    101. }
    102. HttpGet httpGet = new HttpGet(uriBuilder.build());
    103. httpGet.addHeader("Content-type", "application/json; charset=utf-8");
    104. httpGet.setHeader("Accept", "application/json");
    105. httpClient = HttpsUtils.createSSLClientDefault();
    106. httpResponse = httpClient.execute(httpGet);
    107. HttpEntity httpEntity = httpResponse.getEntity();
    108. if (httpEntity != null) {
    109. String jsObject = EntityUtils.toString(httpEntity, "UTF-8");
    110. return jsObject;
    111. } else {
    112. return null;
    113. }
    114. } catch (Exception e) {
    115. e.printStackTrace();
    116. return null;
    117. } finally {
    118. try {
    119. httpResponse.close();
    120. httpClient.close();
    121. logger.info("请求流关闭完成");
    122. } catch (IOException e) {
    123. logger.info("请求流关闭出错");
    124. e.printStackTrace();
    125. }
    126. }
    127. }
    128. public static void main(String[] args) throws Exception {
    129. JSONObject jsonObject = new JSONObject();
    130. jsonObject.put("idCard","***");
    131. jsonObject.put("peopleName","***");
    132. System.out.println(HttpsUtils.sendByHttp(jsonObject, "https://60.214.234.91/sam/*/*/*"));;
    133. }
    134. }

    经测试 调用成功!

    之前一直采用RestTemplate调用没有成功 ! 有用这个调用成功的可在评论区指点一下,学习学习 感谢各路大神!

  • 相关阅读:
    SP605官方开发板不能扫到链的问题
    ubuntu-常用配置-记录
    路由进阶:双点双向路由重发布实验配置
    【JavaEE进阶序列 | 从小白到工程师】LinkedList集合特有的方法详解与使用
    【java】异常
    【uniapp】小程序开发4:微信小程序支付、h5跳转小程序
    基于单片机的智能家居安保系统(论文+源码)
    第七天:gec6818开发板QT和Ubuntu中QT安装连接sqlite3数据库驱动环境保姆教程
    Shell脚本函数简介及运用
    【算法练习Day48】回文子串&&最长回文子序列
  • 原文地址:https://blog.csdn.net/IT_51888_liang/article/details/126699407