Android HTTPS发布问题

时间:2013-09-26 15:37:41

标签: android post https

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

通常的错误是:

java.net.UnknownHostException

但我有时可能会收到错误,例如由同行关闭的连接

将表单提交到http网址似乎可以无缝地工作,但是当使用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帖子?

谢谢:)

1 个答案:

答案 0 :(得分:0)

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

这对我来说完美无瑕。