Reading URL, getting read times out error

527 views Asked by At

Hi I am using following code to reading URL:

    import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;


public class JavaHttpUrlConnectionReader
{
  public static void main(String[] args)
  throws Exception
  {
    new JavaHttpUrlConnectionReader();
  }

  public JavaHttpUrlConnectionReader()
  {
    try
    {
      String myUrl = "http://epaperbeta.timesofindia.com/NasData/PUBLICATIONS/THETIMESOFINDIA/Delhi/2015/06/09/PageIndex/09_06_2015.xml";
      // if your url can contain weird characters you will want to
      // encode it here, something like this:
      // myUrl = URLEncoder.encode(myUrl, "UTF-8");

      String results = doHttpUrlConnectionAction(myUrl);
      System.out.println(results);
    }
    catch (Exception e)
    {
      // deal with the exception in your "controller"
    }
  }

  /**
   * Returns the output from the given URL.
   */
  private String doHttpUrlConnectionAction(String desiredUrl)
  throws Exception
  {
    URL url = null;
    BufferedReader reader = null;
    StringBuilder stringBuilder;

    try
    {
      // create the HttpURLConnection
      url = new URL(desiredUrl);
      HttpURLConnection connection = (HttpURLConnection) url.openConnection();

      // just want to do an HTTP GET here
      connection.setRequestMethod("GET");

      // uncomment this if you want to write output to this url
      //connection.setDoOutput(true);

      // give it 15 seconds to respond
      connection.setReadTimeout(35*1000);
      connection.connect();

      // read the output from the server
      reader = new BufferedReader(new   InputStreamReader(connection.getInputStream()));
      stringBuilder = new StringBuilder();

      String line = null;
      while ((line = reader.readLine()) != null)
      {
        stringBuilder.append(line + "\n");
      }
      return stringBuilder.toString();
    }
    catch (Exception e)
    {
      e.printStackTrace();
      throw e;
    }
    finally
    {
      // close the reader; this can throw an exception too, so
      // wrap it in another try/catch block.
      if (reader != null)
      {
        try
        {
          reader.close();
        }
        catch (IOException ioe)
        {
          ioe.printStackTrace();
        }
      }
    }
  }
}

It gives me following error:

java.net.SocketTimeoutException: Read timed out 
at java.net.SocketInputStream.socketRead0(Native Method) 
at java.net.SocketInputStream.read(SocketInputStream.java:129) 
at java.io.BufferedInputStream.fill(BufferedInputStream.java:218) 
at java.io.BufferedInputStream.read1(BufferedInputStream.java:258) 
at java.io.BufferedInputStream.read(BufferedInputStream.java:317) 
at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:687) 
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:632) 
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1072) 
at JavaHttpUrlConnectionReader.doHttpUrlConnectionAction(JavaHttpUrlConnectionReader.java:77) 
at JavaHttpUrlConnectionReader.<init>(JavaHttpUrlConnectionReader.java:33) 
at JavaHttpUrlConnectionReader.main(JavaHttpUrlConnectionReader.java:21) 

Kindly tell me the reason why it occurs, and solution for it.

When I run this code outside of my office LAN, it is working fine. but not in office LAN.

Thanks & Regards Abhishek

1

There are 1 answers

2
GingerHead On

Your URL:

http://epaperbeta.timesofindia.com/NasData/PUBLICATIONS/THETIMESOFINDIA/Delhi/2015/06/09/PageIndex/09_06_2015.xml

is not accessible without a proxy (for example I can't access it from here), no wonder why cannot be read from the stream.

Check your proxy settings. you could try the url in the browser with/without proxy and see the difference.

As @Jens commented look at this.