Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.net.ConnectException: Connection timed out while connecting through a proxy

I am trying to download a zip file from a URL and store it in the local system using java code. I am also using system proxy for the same. Its unable to connect to the url. Any idea?

public static void main()
{
   try
   {
      long startTime = System.currentTimeMillis();

      System.out.println("Connecting to the url...\n");
      System.setProperty("http.proxyHost", " http://abc.com");
      System.setProperty("http.proxyPort","1111");

      URL url = new URL("http://sourceforge.net/projects/sevenzip/files/7-Zip/9.20/7z920.exe/download?use_mirror=nchc");
      url.openConnection();
      InputStream reader = url.openStream();
      FileOutputStream writer = new FileOutputStream("/home/user/result/apps.zip);
      byte[] buffer = new byte[153600];
      int totalBytesRead = 0;
      int bytesRead = 0;

      System.out.println("Reading ZIP file 150KB blocks at a time.\n");

      while ((bytesRead = reader.read(buffer)) > 0)
      {  
         writer.write(buffer, 0, bytesRead);
         buffer = new byte[153600];
         totalBytesRead += bytesRead;
      }

      long endTime = System.currentTimeMillis();

      System.out.println("Done downloading. " + (new Integer(totalBytesRead).toString()) + " bytes read (" + (new Long(endTime - startTime).toString()) + " millseconds).\n");
      writer.close();
      reader.close();
   }
   catch (MalformedURLException e)
   {
      e.printStackTrace();
   }
   catch (IOException e)
   {
      e.printStackTrace();
   }
   unZip(INPUT_ZIP_FILE);

}

i get the following error:

java.net.ConnectException: Connection timed out
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
    at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
    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.<init>(HttpClient.java:233)
    at sun.net.www.http.HttpClient.New(HttpClient.java:306)
    at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:977)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:925)
    at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:836)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1172)
    at java.net.URL.openStream(URL.java:1010)
    at test.main(test.java:33)

It says unknown host when i try to ping the url, but i am able to ping urls like www.google.com

like image 537
Chirag Avatar asked Oct 09 '12 08:10

Chirag


People also ask

How do I fix Java net ConnectException connection timed out?

1) First try to ping the destination host, if the host is ping-able it means the client and server machine are in the network. 2) Try connecting to server host and port using telnet. If you are able to connect means something is wrong with your client code.

What does Java net ConnectException Connection refused mean?

Below are some of the possible reason why java.net.ConnectException: Connection refused comes: 1) Client and Server, either or both of them are not in the network. 2) Server is not running. The second most common reason is the server is down and not running.

Why do we get Java net ConnectException?

java. net. ConnectException: Connection refused: connect is the most frequent kind of occurring networking exception in Java whenever the software is in client-server architecture and trying to make a TCP connection from the client to the server.

How do I fix socket timeout exception?

Using try/catch/finally If you are a developer, so you can surround the socket connection part of your code in a try/catch/finally and handle the error in the catch. You might try connecting a second time, or try connecting to another possible socket, or simply exit the program cleanly.


1 Answers

Try this page on how to work with proxies - looks comprehensive.

like image 103
RonK Avatar answered Sep 21 '22 21:09

RonK