无法使用HttpURLConnection连接到https

时间:2017-12-01 11:14:24

标签: java android httpurlconnection httpsurlconnection

我尝试对http://www.android.com/进行GET / POST,就像在documentation

中给出的示例一样

InputStream我收到一条消息302 Moved并告诉我继续https://www.android.com/

当我将URL修改为https时,我得到:

  

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));

更新

我认为这是SSL证书的问题,但事实并非如此。我添加了一个盲目信任所有SSL证书的功能。来源herehere

1 个答案:

答案 0 :(得分:0)

当我使用HttpsURLConnection代替HttpURLConnection时遇到了同样的问题。