2015-06-09 40 views
0

你好我使用下面的代碼讀取網址:閱讀網址,越來越讀超時錯誤

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(); 
     } 
     } 
    } 
    } 
} 

它給了我以下錯誤:

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) 

請告訴我,爲什麼它發生,併爲它提供解決方案。

當我在辦公室局域網之外運行此代碼時,它工作正常。但不在辦公室局域網中。

感謝&問候 阿布舍克

+0

也許你背後是一個代理。所以你必須添加代理支持 – Jens

+0

可能的重複[我如何使HttpURLConnection使用代理?](http://stackoverflow.com/questions/1432961/how-do-i-make-httpurlconnection-use-a-proxy ) – Jens

回答

0

你的網址:

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

也不是沒有代理訪問(例如我不能從這裏訪問它),難怪爲什麼不能從流中讀取。

檢查代理設置。您可以在瀏覽器中使用/不使用代理服務器來查看網址,並查看其差異。

由於@Jens評論看this

+0

此URL在我的Wifi上運行在瀏覽器中,並且在我的辦公室外LAN上運行。我有更多的網址,他們也沒有在上面的代碼中運行。 http://timesofindia.indiatimes.com/feeds/newsfeed/1081479906.cms?type=tnn,這也沒有運行 – zidan007

+0

看看你的互聯網設置,是否有任何代理定義? – GingerHead