Tuesday, January 17, 2012

java.net.ConnectException: Connection timed out: connect

 Exception in thread "main" java.net.ConnectException: Connection timed out: connect
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
    at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
    at java.net.Socket.connect(Socket.java:529)
    at java.net.Socket.connect(Socket.java:478)
    at sun.net.NetworkClient.doConnect(NetworkClient.java:163)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:394)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:529)
    at sun.net.www.http.HttpClient.(HttpClient.java:233)
    at sun.net.www.http.HttpClient.New(HttpClient.java:306)
    at sun.net.www.http.HttpClient.New(HttpClient.java:323)
    at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:860)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:801)
    at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:726)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1049)



If you get above error, there could be one of the following reasons.
  • The domain/IP or port is incorrect
  • The domain/IP or port (i.e service) is down
  • The domain/IP is taking longer than your default timeout to respond
  • You have a firewall that is blocking requests or responses on whatever port you are using
  • You have a firewall that is blocking requests to that particular host
  • Your internet connection is down

I most of the cases there is a firewalls issue. Firewalls and port or IP blocking may be in place by your ISP.

To access URL via Firewalls, you may require to set http.proxyHost and http.proxyPort. Try using below syntax.

System.setProperty("http.proxyHost", "host name");
System.setProperty("http.proxyPort", "port number");

Some time you may also get below error.

Exception in thread "main" java.io.IOException: Server returned HTTP response code: 403 for URL: http://www.google.com
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1313)

Here you may require to set User-Agent using below syntax.

connection.addRequestProperty("User-Agent", "Mozilla/4.0");

Please find the complete code below.


/* SimpleURLConnection.java */

import java.net.*;
import java.io.*;

public class SimpleURLConnection
{
    public static void main(String[] args) throws Exception
    {
        System.setProperty("http.proxyHost", "host name");
        System.setProperty("http.proxyPort", "port number");

        URL url= new URL("http://www.google.com");//provide proper url

        URLConnection connection = url.openConnection();
        connection.addRequestProperty("User-Agent", "Mozilla/4.0");

        BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));

        String line = "";

        while((line=in.readLine()) != null)
        {
            System.out.println(line);
        }

        //closing resource
        in.close();
        connection=null;
    }
}