问题描述
有什么区别,如果我尝试连接到一个https开头使用的HttpURLConnection
和 HttpsURLConnection
?
我能够连接为https同时使用的HttpURLConnection
和 HttpsURLConnection
让我很困惑。我想我只能建立连接到一个https开头,如果我用 HttpsURLConnection?
下面是我的一些问题:
- 如果我能开到一个https开头使用HttpURLConnection类的连接,是我的连接仍处于https开头,或者它确实成为HTTP?
- 如何有关证书的确证?因为我没有任何
SSLSocketFactory的
和的HostnameVerifier
我的的HttpURLConnection
在连接到一个https开头,是什么SSLSocketFactory的
和的HostnameVerifier
我使用的是? - 这是否意味着我相信一切都不管,如果证书受信任或不?
- 使用
- HTTPS连接。
- 默认行为
- 不知道,但阅读以下内容。
我只是写了一些code作为测试(见下文一路)。是什么最终发生的是 URL.openConnection()的电话给你回的 libcore.net.http.HttpsURLConnectionImpl 的类。
这是HttpsURLConnection的实现,但HttpsURLConnection从HttpURLConnection的继承。所以,你看到多态性工作。
展望更远的进入调试器看起来类正使用默认的工厂方法。
的HostnameVerifier = javax.net.ssl.DefaultHostnameVerifier
的SSLSocketFactory = org.apache.harmony.xnet.provider.jsse.OpenSSLSocketFactoryImpl
在此基础上,它看起来像默认的行为正在被使用。我不知道,如果你相信的一切,但你肯定是建立HTTPS连接装置。
文档有点暗示的连接会自动信任的CA是众所周知的,但不是自签名。 (见下文)我没有测试这一点,但他们有例如code对如何实现这一目标这里。
的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
andHostnameVerifier
on myHttpURLConnection
while connecting to an "https", whatSSLSocketFactory
andHostnameVerifier
am I using? - Does it mean that I am trusting everything regardless if the certificate is trusted or not?
- HTTPS connection being used.
- Default Behaviors
- 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.
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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!