1

I am using the HttpURLConnection class to connect to external web service from eclipse, then I am getting a error message "Connection Refused"

public class TestConnection {


    static {
        //for localhost testing only
        javax.net.ssl.HttpsURLConnection.setDefaultHostnameVerifier(
            new javax.net.ssl.HostnameVerifier(){

                    public boolean verify(String hostname,
                                      javax.net.ssl.SSLSession sslSession) {
                        if (hostname.equals("localhost")) {
                            return true;
                        }
                        return false;
                    }
                });
    }

    public static void main(String[] args)
    {




    HttpURLConnection urlConnection;
    try {
        //https get method with required parameters
    //  XMLHttpRequest s1 = new XMLHttpRequest ();
        urlConnection = (HttpURLConnection) ((new URL("https://google.com").openConnection()));
        urlConnection.setRequestMethod("GET");
        urlConnection.setRequestProperty("User-Agent", "");
        int HTTPS_RESPONSE_CODE = urlConnection.getResponseCode();

        BufferedReader in = new BufferedReader(
                new InputStreamReader(urlConnection.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();
        String HTTPS_RESULT=response.toString(); 
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    }

}

java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.connect(Unknown Source)
    at sun.security.ssl.BaseSSLSocketImpl.connect(Unknown Source)
    at sun.net.NetworkClient.doConnect(Unknown Source)
    at sun.net.www.http.HttpClient.openServer(Unknown Source)
    at sun.net.www.http.HttpClient.openServer(Unknown Source)
    at sun.net.www.protocol.https.HttpsClient.<init>(Unknown Source)
    at sun.net.www.protocol.https.HttpsClient.New(Unknown Source)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    at java.net.HttpURLConnection.getResponseCode(Unknown Source)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(Unknown Source)
    at TestConnection.main(TestConnection.java:19)

But If i try to connect to the same site from a browser I am able to get a response from the service. Can you please let me know if there is a work around?

2 Answers2

0

By default, the HttpURLConnection class will not allow localhost as the hostname. You need to define a custom hostname verifier which will allow localhost. You can place this code into a static block at the top of the class where you intend to use HttpURLConnection:

public final class YourClassName {
    static {
        //for localhost testing only
        javax.net.ssl.HttpsURLConnection.setDefaultHostnameVerifier(
            new javax.net.ssl.HostnameVerifier(){

                    public boolean verify(String hostname,
                                      javax.net.ssl.SSLSession sslSession) {
                        if (hostname.equals("localhost")) {
                            return true;
                        }
                        return false;
                    }
                });
    }

    // use HttpURLConnection here ...
}
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

Please find the working code,

PROXY_SERVER set to a valid proxy server and port for http I am using is 8080

proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(PROXY_SERVER, PROXY_PORT));

And when you establish the connection pass the proxy as an argument.

urlConnection = (HttpURLConnection) ((new URL(URI).openConnection(proxy)));