tags:

views:

231

answers:

4

Hi I am using following code to retrieve response code from https based urls, but when i run a prog it just hangs cont.

code:

import java.net.;
import javax.net.ssl.;
import java.io.*;

class Https2
{
    public static void main(String args[]) throws Exception
    {
        URL u = new URL("https://myurl");
        HttpsURLConnection hc = (HttpsURLConnection)u.openConnection();
        hc.setConnectTimeout(3000);
        hc.setReadTimeout(5000);
        System.out.println("Response Code: " + hc.getResponseCode());
        hc.disconnect();
    }
}

How can make successfull connection to Https urls ?

any help or ideas will be well appreciated.

Thanks

A: 

I tried the attached sample with https://mail.google.com/mail/, and it worked flawlessly on linux and Mac OS X.

notnoop
But same URL and with same code is not working here (winxp sp2).what could be possible reason for that ? is there any problem in source code or any other problem ? have any idea?
Matrix
A: 

https://mail.google.com/mail/ works on my Windows XP box. Maybe your windows firewall settings does not contain the java.exe as exception? Or the site you want to connect to uses HTTP Basic Authentication over the HTTPS connection. Or the site needs you to identify yourself with a certificate as suggested by others?

Edit: Try your code without the timeout parameters. HTTPS connection and handshake is usually slower than a regular HTTP call. Your connection might time out due this before it can read the requested data.

I thing hc.setConnectionTimeout() might not even work because when you get to that point you already have a working connection according to the javadoc.

kd304
+1  A: 

Don't know if it helps but I've never had much joy with the JDK HTTP classes and have typically wound up using the Jakarta Common HTTP Client API (http://hc.apache.org/httpclient-3.x/).

Nick Holt
+1 for HTTPClient
kd304
A: 

The authentication of the server is required by default. You need to set the javax.net.ssl.trustStore system property as the name of a keystore containing the server's certificate.

BTW, it's a good idea to catch and print the exceptions to help diagnostics.

Maurice Perry