title: java.net.ConnectException: Connection timed out: connect when running .java web service client
body: <p>I created .java WS client to hit external SOAP request. my laptop is beyond proxy. i went through all the documentation and tried all the solutions of setting up proxy in window->Preferences->general->network connections. 
  None of them are working.I am getting required output if i use wireless (out of corp network)</p>
  
  <p>I am able to access the URL from browser even with proxy settings: 
  I tried setting up http, HTTPs and SOCKS proxy with userids(domain\username), password and tried also native option. None of them are working.</p>
  
  <p>solutions tried:
  1. setting up proxy in windows->preferences->general->networks
  2. added eclipse to firewall
  3. disabled anti virus
  4. added vmargs -Djava.net.preferIPv4Stack=true in eclipse.ini</p>
  
  <p>Any help is appreciated.</p>
  
  <p>Below is my .java file
  ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++</p>
  
  <pre><code>package narsiRestApiTest;
  
  import java.io.BufferedReader;
  import java.io.InputStreamReader;
  import java.io.StringReader;
  import java.net.HttpURLConnection;
  import java.net.URL;
  
  import javax.xml.parsers.DocumentBuilder;
  import javax.xml.parsers.DocumentBuilderFactory;
  import org.w3c.dom.Document;
  import org.w3c.dom.Element;
  import org.w3c.dom.Node;
  import org.w3c.dom.NodeList;
  import org.xml.sax.InputSource;
  
  public class HttpGetter {
  
      public static void main(String[] args) throws Exception {
          // TODO Auto-generated method stub
  
          String sXML = HttpGetter
                  .getDoc("http://api.geonames.org/weatherIcao?ICAO=LSZH&amp;username=rheh&amp;style=full");
  
          Document dom = HttpGetter.loadXMLFromString(sXML);
  
          Element el = (Element) dom.getDocumentElement();
  
          System.out.println(HttpGetter.getTextValue(el, "stationName"));
          System.out.println(HttpGetter.getTextValue(el, "temperature"));
  
      }
  
      public static String getDoc(String urlToRead) {
  
          URL url;
          HttpURLConnection conn;
          BufferedReader rd;
          String line;
          String result = "";
  
          try {
  
              url = new URL(urlToRead);
              conn = (HttpURLConnection) url.openConnection();
              conn.setRequestMethod("GET");
  
              rd = new BufferedReader(
                      new InputStreamReader(conn.getInputStream()));
  
              while ((line = rd.readLine()) != null) {
                  result += line;
              }
  
              rd.close();
  
          } catch (Exception e) {
  
              e.printStackTrace();
  
          }
  
          return result;
      }
  
  
      public static Document loadXMLFromString(String xml) throws Exception {
          DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
          DocumentBuilder builder = factory.newDocumentBuilder();
          InputSource is = new InputSource(new StringReader(xml));
          return builder.parse(is);
      }
  
      private static String getTextValue(Element ele, String tagName) {
          String textVal = null;
          NodeList nl =  ele.getElementsByTagName(tagName);
          if (nl != null &amp;&amp; nl.getLength() &gt; 0) {
              Element el = (Element) nl.item(0);
              textVal = ((Node) el).getFirstChild().getNodeValue();
          }
  
          return textVal;
      }
  
  
  }
  </code></pre>
  
  <p>Below is the error i am seeing :</p>
  
  <pre><code>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 java.net.Socket.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.http.HttpClient.&lt;init&gt;(Unknown Source)
      at sun.net.www.http.HttpClient.New(Unknown Source)
      at sun.net.www.http.HttpClient.New(Unknown Source)
      at sun.net.www.protocol.http.HttpURLConnection.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.http.HttpURLConnection.connect(Unknown Source)
      at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
      at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
      at narsiRestApiTest.HttpGetter.getDoc(HttpGetter.java:49)
      at narsiRestApiTest.HttpGetter.main(HttpGetter.java:23)
  [Fatal Error] :1:1: Premature end of file.
  Exception in thread "main" org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Premature end of file.
      at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source)
      at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source)
      at narsiRestApiTest.HttpGetter.loadXMLFromString(HttpGetter.java:71)
      at narsiRestApiTest.HttpGetter.main(HttpGetter.java:25)
  </code></pre>
  
  <p>Thanks</p>