0

I am having some issues submitting a form via post method to a https connection.

The usual error is:

java.net.UnknownHostException

But I can sometimes get error such as connection closed by peer

Submitting a form to a http url seems to work seamlessly but when using a https connection it seems to bring up so many issues.

The server certificate is valid (signed by Go Daddy) and I cannot see any issues as I can get iOS devices to submit to it fine.

I've tried these solutions but they do not seem to have made much difference:

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

Does anyone have a useful tutorial or could possibly explain how to perform a https post?

Thanks :)

Community
  • 1
  • 1
TheT4llOne
  • 43
  • 5

1 Answers1

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

This works flawlessly for me.

S.A.Norton Stanley
  • 1,833
  • 3
  • 23
  • 37
  • Hi, Thanks for this. however this is very similar to what I have already and I get the same error from this. Do you no of anyway just to simply not check the certificate maybe? – TheT4llOne Sep 26 '13 at 16:04
  • Try the modified code. See if this helps. Not a good practice though. – S.A.Norton Stanley Sep 26 '13 at 17:00
  • This mainly occurs when the SSL Handshake fails. It could be because of the certificate. You could check if the certificate on the server is valid using the following url http://www.digicert.com/help/ – S.A.Norton Stanley Sep 27 '13 at 10:00