简体   繁体   English

无法使用HttpURLConnection连接到https

[英]Cannot connect to https with HttpURLConnection

I try to make a GET/POST to http://www.android.com/ like in the example given in the documentation 我尝试像在文档中给出的示例那样对http://www.android.com/进行GET / POST

In the InputStream I get a message saying 302 Moved and tells me to go on https://www.android.com/ InputStream我收到一条消息,说302 Moved并告诉我继续https://www.android.com/

When I modify my URL to https, I get: 当我将URL修改为https时,我得到:

java.io.FileNotFoundException: https://www.android.com/ java.io.FileNotFoundException: https : //www.android.com/

HttpsURLConnection con;
String request = "https://www.android.com/";
URL url = new URL(request);
    con = (HttpsURLConnection) url.openConnection();
    con.setDoOutput(true);
    con.setInstanceFollowRedirects(false);
    con.setRequestMethod("GET");
    con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    con.setRequestProperty("charset", "utf-8");
    con.setRequestProperty("Content-Length", Integer.toString(postDataLength));

Update 更新

I thought it was an issue of SSL certificates, but it is not. 我以为这是SSL证书的问题,但不是。 I've added a function that will blindly trust all SSL certificates. 我添加了一个功能,该功能将盲目地信任所有SSL证书。 Sources here and here . 来源这里这里

一旦我遇到了使用HttpsURLConnection而不是HttpURLConnection时解决的相同问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM