博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java用ssl和url共同类实现访问某https网站
阅读量:5807 次
发布时间:2019-06-18

本文共 2137 字,大约阅读时间需要 7 分钟。

hot3.png

 

 

package test;import java.io.*;import java.net.*;import java.security.cert.CertificateException;import java.security.cert.X509Certificate;import javax.net.ssl.*;public class URLSTest {private static class TrustAnyTrustManager implements X509TrustManager {   public void checkClientTrusted(X509Certificate[] chain, String authType)     throws CertificateException {   }   public void checkServerTrusted(X509Certificate[] chain, String authType)     throws CertificateException {   }   public X509Certificate[] getAcceptedIssuers() {    return new X509Certificate[] {};   }}private static class TrustAnyHostnameVerifier implements HostnameVerifier {   public boolean verify(String hostname, SSLSession session) {    return true;   }}public static void main(String[] args) throws Exception {   InputStream in = null;   OutputStream out = null;   byte[] buffer = new byte[4096];   String str_return = "";   try {    SSLContext sc = SSLContext.getInstance("SSL");    sc.init(null, new TrustManager[] { new TrustAnyTrustManager() },      new java.security.SecureRandom());    URL console = new URL(      "https://ebank.xxxxx.com/pweb/test.do?actionType=1");    HttpsURLConnection conn = (HttpsURLConnection) console      .openConnection();    conn.setSSLSocketFactory(sc.getSocketFactory());    conn.setHostnameVerifier(new TrustAnyHostnameVerifier());    conn.connect();    InputStream is = conn.getInputStream();    DataInputStream indata = new DataInputStream(is);    String ret = "";    while (ret != null) {     ret = indata.readLine();     if (ret != null && !ret.trim().equals("")) {      str_return = str_return        + new String(ret.getBytes("ISO-8859-1"), "GBK");     }    }    conn.disconnect();   } catch (ConnectException e) {    System.out.println("ConnectException");    System.out.println(e);    throw e;   } catch (IOException e) {    System.out.println("IOException");    System.out.println(e);    throw e;   } finally {    try {     in.close();    } catch (Exception e) {    }    try {     out.close();    } catch (Exception e) {    }   }   System.out.println(str_return);}}

 

转载于:https://my.oschina.net/lenglingx/blog/184174

你可能感兴趣的文章
菜鸟笔记(一) - Java常见的乱码问题
查看>>
我理想中的前端工作流
查看>>
记一次Git异常操作:将多个repository合并到同一repository的同一分支
查看>>
CodeIgniter 3.0 新手捣鼓源码(一) base_url()
查看>>
Chrome 广告屏蔽功能不影响浏览器性能
查看>>
vSphere 6将于2月2日全球同步发表
查看>>
Android状态栏实现沉浸式模式
查看>>
让你的APP实现即时聊天功能
查看>>
iOS 绝对路径和相对路径
查看>>
使用Openfiler搭建ISCSI网络存储
查看>>
应用新安全组 - 每天5分钟玩转 OpenStack(116)
查看>>
生产制造追溯系统
查看>>
4.3. 键盘设置
查看>>
iOS - UIViewController
查看>>
准备 overlay 网络实验环境 - 每天5分钟玩转 Docker 容器技术(49)
查看>>
论文笔记之:Large Scale Distributed Semi-Supervised Learning Using Streaming Approximation
查看>>
IntPtr 转 string
查看>>
一文搞懂各种 Docker 网络 - 每天5分钟玩转 Docker 容器技术(72)
查看>>
学生名单
查看>>
(转) 多模态机器翻译
查看>>