【问题标题】:Cannot connect to https with HttpURLConnection无法使用 HttpURLConnection 连接到 https
【发布时间】:2017-12-01 11:14:23
【问题描述】:

我尝试对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

【问题讨论】:

  • 你应该投到HttpsURLConnection而不是HttpURLConnection
  • 我尝试声明为HttpsURLConnect 并进行转换,但同样的问题。
  • 他通过检查他的 URL 并设置 con.setDoOutput(true); 解决了他的问题。我已经做了这个

标签: java android httpurlconnection httpsurlconnection


【解决方案1】:

一旦我遇到了同样的问题,当我使用 HttpsURLConnection 而不是 HttpURLConnection 时解决了这个问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多