【问题标题】:Android HTTPS Post IssueAndroid HTTPS 发布问题
【发布时间】:2013-10-02 16:16:52
【问题描述】:

我在通过 post 方法向 https 连接提交表单时遇到一些问题。

通常的错误是:

java.net.UnknownHostException

但我有时会收到错误,例如连接被对等方关闭

将表单提交到 http url 似乎可以无缝地工作,但是当使用 https 连接时,它似乎会带来很多问题。

服务器证书有效(由 Go Daddy 签名),我看不到任何问题,因为我可以让 iOS 设备正常提交给它。

我已经尝试了这些解决方案,但它们似乎没有太大的不同:

Secure HTTP Post in Android

How to HTTPS post in Android

Android HTTPS Post - Not working

Android SSL https post

Not trusted certificate using ksoap2-android

有没有人有有用的教程或者可以解释如何执行 https 帖子?

谢谢:)

【问题讨论】:

    标签: android post https


    【解决方案1】:
    URL url = new URL("https://www.xyz.com");
    HttpsURLConnection httpURLConnection  = (HttpsURLConnection) url.openConnection();
    httpURLConnection.setRequestProperty("Content-Type",
                    "text/plain");
    httpURLConnection.setRequestMethod("POST");
    httpURLConnection.setDoOutput(true);
    httpURLConnection.setAllowUserInteraction(false);
    httpURLConnection.setInstanceFollowRedirects(true);
    httpURLConnection.setHostnameVerifier(DO_NOT_VERIFY);
    httpURLConnection.connect();
    OutputStream outputStream = httpURLConnection.getOutputStream();
     outStream.write(datainbytes);
    
    
    
     final static HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() {
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
     };
    

    这对我来说完美无缺。

    【讨论】:

    • 嗨,谢谢。但是,这与我已经拥有的非常相似,并且从中得到了相同的错误。无论如何,您是否只是不检查证书?
    • 试试修改后的代码。看看这是否有帮助。虽然不是一个好习惯。
    • 这主要发生在 SSL 握手失败时。可能是证书的原因。您可以使用以下 url digicert.com/help 检查服务器上的证书是否有效
    猜你喜欢
    • 2011-08-02
    • 1970-01-01
    • 2011-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多