title: Connection timed out error in intellij Idea
body: <p>I am trying to make a simple http request using HttpURLConnection. I am getting this error, connection timed out. I am using intellij IDEA, and set the proxy properly. Check connection in the settings, says the connection is success. What might have gone wrong? Here is my code.</p>
  
  <pre><code>import java.io.*;
  import java.net.*;
  
  /**
   * Created by admin on 22/8/15.
   */
  public class Hello {
      public static void main(String [] args)
      {
          try
          {
              URL url = new URL("http://www.google.com");
              URLConnection urlConnection = url.openConnection();
              HttpURLConnection connection = null;
              if(urlConnection instanceof HttpURLConnection)
              {
                  connection = (HttpURLConnection) urlConnection;
              }
              else
              {
                  System.out.println("Please enter an HTTP URL.");
                  return;
              }
              BufferedReader in = new BufferedReader(
                      new InputStreamReader(connection.getInputStream()));
              String urlString = "";
              String current;
              while((current = in.readLine()) != null)
              {
                  urlString += current;
              }
              System.out.println(urlString);
          }catch(IOException e)
          {
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>The error is the following</p>
  
  <pre><code>java.net.ConnectException: Connection timed out
      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.&lt;init&gt;(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)
      at Hello.main(Hello.java:24)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:597)
      at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
  
  Process finished with exit code 0
  </code></pre>
  
  <p>ps: Using system properties in the code does work. But it is not feasible for my purposes. Is there any work around for this. 
  I don't want to write code like the following, i.e., removing some lines from production. And the following works fine.</p>
  
  <pre><code>import java.io.*;
  import java.net.*;
  import java.util.Properties;
  
  /**
   * Created by admin on 22/8/15.
   */
  public class Hello {
  
     // This method should be removed in production
      static void setProxy(){
          Properties systemProperties = System.getProperties();
          systemProperties.setProperty("http.proxyHost","lotus");
          systemProperties.setProperty("http.proxyPort", "8080");
      }
      public static void main(String [] args)
      {
          try
          {
  
              setProxy();
              URL url = new URL("http://www.google.com");
              URLConnection urlConnection = url.openConnection();
              HttpURLConnection connection = null;
              if(urlConnection instanceof HttpURLConnection)
              {
                  connection = (HttpURLConnection) urlConnection;
              }
              else
              {
                  System.out.println("Please enter an HTTP URL.");
                  return;
              }
              BufferedReader in = new BufferedReader(
                      new InputStreamReader(connection.getInputStream()));
              String urlString = "";
              String current;
              while((current = in.readLine()) != null)
              {
                  urlString += current;
              }
              System.out.println(urlString);
          }catch(IOException e)
          {
              e.printStackTrace();
          }
      }
  }
  </code></pre>