使用HttpURLConnection类和HttpsURLConnection连接到HTTPS? [英] Using HttpURLConnection and HttpsURLConnection to connect to an https?

查看:725
本文介绍了使用HttpURLConnection类和HttpsURLConnection连接到HTTPS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么区别,如果我尝试连接到一个https开头使用的HttpURLConnection HttpsURLConnection

我能够连接为https同时使用的HttpURLConnection HttpsURLConnection 让我很困惑。我想我只能建立连接到一个https开头,如果我用 HttpsURLConnection?

下面是我的一些问题:

  • 如果我能开到一个https开头使用HttpURLConnection类的连接,是我的连接仍处于https开头,或者它确实成为HTTP?
  • 如何有关证书的确证?因为我没有任何 SSLSocketFactory的的HostnameVerifier 我的的HttpURLConnection 在连接到一个https开头,是什么 SSLSocketFactory的的HostnameVerifier 我使用的是?
  • 这是否意味着我相信一切都不管,如果证书受信任或不?
解决方案
    使用
  1. HTTPS连接。
  2. 默认行为
  3. 不知道,但阅读以下内容。

我只是写了一些code作为测试(见下文一路)。是什么最终发生的是 URL.openConnection()的电话给你回的 libcore.net.http.HttpsURLConnectionImpl 的类。

这是HttpsURLConnection的实现,但HttpsURLConnection从HttpURLConnection的继承。所以,你看到多态性工作。

展望更远的进入调试器看起来类正使用默认的工厂方法。

的HostnameVerifier = <一个href="http://developer.android.com/reference/javax/net/ssl/HttpsURLConnection.html#getDefaultHostnameVerifier%28%29">javax.net.ssl.DefaultHostnameVerifier

的SSLSocketFactory = <一个href="http://developer.android.com/reference/javax/net/ssl/HttpsURLConnection.html#getDefaultSSLSocketFactory%28%29">org.apache.harmony.xnet.provider.jsse.OpenSSLSocketFactoryImpl

在此基础上,它看起来像默认的行为正在被使用。我不知道,如果你相信的一切,但你肯定是建立HTTPS连接装置。

文档有点暗示的连接会自动信任的CA是众所周知的,但不是自签名。 (见下文)我没有测试这一点,但他们有例如code对如何实现这一目标这里

  

如果应用程序需要信任的证书颁发机构(CA)   证书不属于该系统的一部分,它应该指定其   经由的SSLSocketFactory上设置自己X509TrustManager   HttpsURLConnection。

 的Runnable R =新的Runnable(){

    公共无效的run(){
        尝试 {
        URL测试=新的URL(https://www.google.com/);
        HttpURLConnection的连接=(HttpURLConnection类)test.openConnection();
        InputStream的是= connection.getInputStream();
        StringWriter的SW =新的StringWriter();
        。字符串值=新的java.util.Scanner中(是).useDelimiter(\\ A)下一个();
        Log.w(谷歌,值);
        }赶上(例外五){
            Log.e(测试,e.getMessage());
        }
    }

};

线程t =新主题(R);
t.start();
 

What are the differences if I try to connect to an "https" using HttpURLConnection and HttpsURLConnection?

I was able to connect to "https" using both HttpURLConnection and HttpsURLConnection so I am confused. I thought I can only establish a connection to an "https" if I used HttpsURLConnection?

Below are some of my questions:

  • If I was able to open a connection to an "https" using HttpURLConnection, is my connection still in "https" or does it become "http"?
  • How about the validating of certificates? Since I didn't any SSLSocketFactory and HostnameVerifier on my HttpURLConnection while connecting to an "https", what SSLSocketFactory and HostnameVerifier am I using?
  • Does it mean that I am trusting everything regardless if the certificate is trusted or not?

解决方案

  1. HTTPS connection being used.
  2. Default Behaviors
  3. Not sure but read below.

I just wrote some code as a test (see all the way below). What ends up happening is the URL.openConnection() call will give you back a libcore.net.http.HttpsURLConnectionImpl class.

This is an implementation of HttpsURLConnection but HttpsURLConnection inherits from HttpURLConnection. So you're seeing polymorphism at work.

Looking further into the debugger it appears that the class is using the default factory methods.

hostnameVerifier = javax.net.ssl.DefaultHostnameVerifier

sslSocketFactory = org.apache.harmony.xnet.provider.jsse.OpenSSLSocketFactoryImpl

Based on this it looks like the default behaviors are being used. I don't know if that means if you're trusting everything but you are definitely establishing an HTTPS connection.

The documentation kinda hints at the connection will auto-trust CAs that are well known but not self-signed. (see below) I did not test this but they have example code on how to accomplish that here.

If an application wants to trust Certificate Authority (CA) certificates that are not part of the system, it should specify its own X509TrustManager via a SSLSocketFactory set on the HttpsURLConnection.

Runnable r = new Runnable() {

    public void run() {
        try {
        URL test = new URL("https://www.google.com/");
        HttpURLConnection connection = (HttpURLConnection) test.openConnection();
        InputStream is = connection.getInputStream();
        StringWriter sw = new StringWriter();
        String value = new java.util.Scanner(is).useDelimiter("\\A").next();
        Log.w("GOOGLE", value);
        } catch(Exception e) {
            Log.e("Test", e.getMessage());
        }
    }

};

Thread t = new Thread(r);
t.start();

这篇关于使用HttpURLConnection类和HttpsURLConnection连接到HTTPS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆