首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >读取URL,获取读取超时错误

读取URL,获取读取超时错误
EN

Stack Overflow用户
提问于 2015-06-09 17:21:39
回答 1查看 477关注 0票数 0

您好,我正在使用以下代码读取URL:

代码语言:javascript
复制
    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();
        }
      }
    }
  }
}

它给出了以下错误:

代码语言:javascript
复制
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) 

请告诉我发生这种情况的原因,以及解决方法。

当我在我的办公室局域网之外运行这段代码时,它工作得很好。但不是在办公室局域网中。

感谢并问候Abhishek

EN

回答 1

Stack Overflow用户

发布于 2015-06-09 17:29:38

您的URL:

没有代理是不可访问的(例如,我不能从这里访问它),难怪不能从流中读取。

检查您的代理设置。你可以在浏览器中尝试使用/不使用代理的url,看看有什么不同。

正如@Jens评论的那样,看看this

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30727996

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档