V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
578619132
V2EX  ›  问与答

java环境下能运行的代码到android不能正常运行。

  •  
  •   578619132 · 2013-01-06 16:02:34 +08:00 · 2825 次点击
    这是一个创建于 4280 天前的主题,其中的信息可能已经有所发展或是发生改变。
    关于获取https服务器的问题,java用的方法是:
    URL urL;

    try {
    urL = new URL(hsUrl);
    HttpsURLConnection con = (HttpsURLConnection) urL.openConnection();

    X509TrustManager xtm = new X509TrustManager() {
    @Override
    public X509Certificate[] getAcceptedIssuers() {
    // TODO Auto-generated method stub
    return null;
    }

    @Override
    public void checkServerTrusted(X509Certificate[] arg0,
    String arg1) throws CertificateException {
    // TODO Auto-generated method stub

    }

    @Override
    public void checkClientTrusted(X509Certificate[] arg0,
    String arg1) throws CertificateException {
    // TODO Auto-generated method stub

    }
    };

    TrustManager[] tm = { xtm };

    SSLContext ctx = SSLContext.getInstance("TLS");
    ctx.init(null, tm, null);

    con.setSSLSocketFactory(ctx.getSocketFactory());
    con.setHostnameVerifier(new HostnameVerifier() {
    @Override
    public boolean verify(String arg0, SSLSession arg1) {
    return true;
    }
    });

    InputStream ss = con.getInputStream();

    DataInputStream in = new DataInputStream(ss);
    String re;
    while ((re = in.readLine()) != null) {
    System.out.println(re);
    break;
    }
    in.close();
    找了很多天解决办法了, - -心都碎了。
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1893 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 21ms · UTC 16:23 · PVG 00:23 · LAX 09:23 · JFK 12:23
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.