随笔 - 68  文章 - 0 评论 - 41 阅读 - 56万
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

重写X509TrustManager

复制代码
 1 private static TrustManager myX509TrustManager = new X509TrustManager() { 
 2 
 3     @Override 
 4     public X509Certificate[] getAcceptedIssuers() { 
 5         return null; 
 6     } 
 7 
 8     @Override 
 9     public void checkServerTrusted(X509Certificate[] chain, String authType) 
10     throws CertificateException { 
11     } 
12 
13     @Override 
14     public void checkClientTrusted(X509Certificate[] chain, String authType) 
15     throws CertificateException { 
16     } 
17 };
复制代码
复制代码
 1 static public String SendHttpsPOST(String url, List&lt;NameValuePair&gt; param, String data) 
 2 { 
 3         String result = null; 
 4         
 5 
 6         //使用此工具可以将键值对编码成"Key=Value&amp;Key2=Value2&amp;Key3=Value3&rdquo;形式的请求参数 
 7         String requestParam = URLEncodedUtils.format(param, "UTF-8"); 
 8         try { 
 9             //设置SSLContext 
10             SSLContext sslcontext = SSLContext.getInstance("TLS"); 
11             sslcontext.init(null, new TrustManager[]{myX509TrustManager}, null);
12 
13             //打开连接
14         //要发送的POST请求url?Key=Value&amp;Key2=Value2&amp;Key3=Value3的形式 
15            URL requestUrl = new URL(url + "?" + requestParam); 
16            HttpsURLConnection httpsConn = (HttpsURLConnection)requestUrl.openConnection();
17 
18             //设置套接工厂 
19             httpsConn.setSSLSocketFactory(sslcontext.getSocketFactory());
20 
21             //加入数据 
22             httpsConn.setRequestMethod("POST"); 
23             httpsConn.setDoOutput(true); 
24             DataOutputStream out = new DataOutputStream( 
25                     httpsConn.getOutputStream()); 
26             if (data != null) 
27                 out.writeBytes(data); 
28             
29             out.flush(); 
30             out.close();
31 
32             //获取输入流 
33             BufferedReader in = new BufferedReader(new InputStreamReader(httpsConn.getInputStream())); 
34             int code = httpsConn.getResponseCode(); 
35             if (HttpsURLConnection.HTTP_OK == code){ 
36                 String temp = in.readLine(); 
37                 /*连接成一个字符串*/ 
38                 while (temp != null) { 
39                     if (result != null) 
40                         result += temp; 
41                     else 
42                         result = temp; 
43                     temp = in.readLine(); 
44                 } 
45             } 
46         } catch (KeyManagementException e) { 
47             e.printStackTrace(); 
48         } catch (NoSuchAlgorithmException e) { 
49             e.printStackTrace(); 
50         } catch (MalformedURLException e) { 
51             e.printStackTrace(); 
52         } catch (ProtocolException e) { 
53             e.printStackTrace(); 
54         } catch (IOException e) { 
55             e.printStackTrace(); 
56         }
57 
58         return result; 
59 }
复制代码

 

posted on   曾经的你|  阅读(38320)  评论(2编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
点击右上角即可分享
微信分享提示