【问题标题】:HttpURLConnection not allowing me to post when I have https:// in the URL当我在 URL 中有 https:// 时,HttpURLConnection 不允许我发帖
【发布时间】:2016-05-24 05:58:43
【问题描述】:

如何让 POST 请求使用“https:”网址?

在以下示例中,当我将 URL 设置为“https”而不是“http”时,HttpURLConnection 会自动使我的请求成为“GET”?!而当它是一个常规的 http 请求时,它是一个 POST...

    URL url = new URL("*https:*//www.google.com");
   // URL url = new URL("*http:*//www.google.com");

    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
    try {
        urlConnection.setDoOutput(true);
        urlConnection.setChunkedStreamingMode(0);

        OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream());
        //writeStream(out);

        InputStream in = new BufferedInputStream(urlConnection.getInputStream());
        // readStream(in);
    }finally {
            urlConnection.disconnect();
        }

enter image description here

【问题讨论】:

  • 您尝试过除 www.google.com 以外的其他网址吗? Google 有一些限制性规则
  • 是的,我尝试了许多不同的网址,包括我为客户构建的网址。出于安全原因,我只是不能在示例中使用那个。使用 org.apache.http.client.methods.HttpPost 时一切正常。一旦我尝试迁移到使用 HttpURLConnection,我就无法再发布了。
  • 这看起来像是一个安全问题。对于https,您需要使用服务器支持的一些认证/身份验证,然后只有它才能将数据发布到服务器。我可能错了。
  • 您是否尝试通过说urlConnection.setRequestMethod("POST"); 来更明确地了解 POST?
  • 是的,我已经按部就班了。

标签: android ssl post https httpurlconnection


【解决方案1】:

使用HttpsURLConnection 而不是HttpURLConnection

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-18
    • 2021-01-02
    • 1970-01-01
    • 1970-01-01
    • 2019-04-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多