2014-01-23 77 views
3

HttpURLConnection錯誤(由對等方重置連接)

問題: 嘗試「連接」時,我有一個間歇但頻繁的故障。 代碼,調試消息和堆棧跟蹤如下。我曾看過類似的 問題的答案,以及android開發人員的幫助,但無法找到 答案的失敗。

任何幫助表示讚賞。

的代碼:

public String doInBackground (String... urlArray) { 
String string; 
InputStream is = null; 
int responseCode = 0; 

Log.d("Data", "doInBackground "); 
try { 
    URL url = new URL(urlArray[0]); 
    // Create a new HTTP URL connection 
    HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
    Log.d("Data", "connection= " + connection); 
    connection.setConnectTimeout(10000); 
    connection.setReadTimeout(10000); 
    connection.setDoInput(true); 
    connection.connect(); 

    responseCode = connection.getResponseCode(); 
    Log.d("Data", "responseCode= " + responseCode); 
    if (responseCode == HttpURLConnection.HTTP_OK) { 
     is = connection.getInputStream(); 
     int len=50000; 
     string = readIt(is, len); 
     connection.disconnect(); 
     is.close(); 
     return string; 
     } 
     else { 
     connection.disconnect(); 
     return "File Not Found"; 
     } 
} 
catch (IOException e) { 
Log.d("Data", "e.getCause()= " + e.getCause()); 
Log.d("Data", "e.getMessage()= " + e.getMessage()); 
e.printStackTrace(); 
return null; 
} 
} 
 
Debug messages when it works: 

01-23 11:57:00.029: D/Data(21987): doInBackground 
01-23 11:57:00.029: D/Data(21987): connection= com.android.okhttp.internal.http.HttpURLConnectionImpl:http://finance.yahoo.com/d/quotes.csv?s=^GSPC+^GSPTSE+^IXIC+^NDX+^TNX&f=snxl1d1t1c1ohgv&ignore=.csv 
01-23 11:57:00.549: D/Data(21987): responseCode= 200 
01-23 11:57:00.549: D/Data(21987): readIt start 
01-23 11:57:00.609: D/Data(21987): readIt end 
01-23 11:57:00.609: D/Data(21987): onPostExecute start 
01-23 11:57:00.629: D/Data(21987): onPostExecute end 

Debug messages when it fails: 
============================= 
01-23 11:57:03.839: D/Data(21987): doInBackground 
01-23 11:57:03.839: D/Data(21987): connection= com.android.okhttp.internal.http.HttpURLConnectionImpl:http://finance.yahoo.com/d/quotes.csv?s=^GSPC+^GSPTSE+^IXIC+^NDX+^TNX&f=snxl1d1t1c1ohgv&ignore=.csv 
01-23 11:57:05.939: D/Data(21987): e.getCause()= libcore.io.ErrnoException: recvfrom failed: ECONNRESET (Connection reset by peer) 
01-23 11:57:05.939: D/Data(21987): e.getMessage()= recvfrom failed: ECONNRESET (Connection reset by peer) 
01-23 11:57:05.979: D/Data(21987): onPostExecute start 
01-23 11:57:06.009: D/Data(21987): onPostExecute end 


The print stack trace: 
============================= 
01-23 11:57:05.939: W/System.err(21987): java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by peer) 
01-23 11:57:05.949: W/System.err(21987): at libcore.io.IoBridge.maybeThrowAfterRecvfrom(IoBridge.java:545) 
01-23 11:57:05.949: W/System.err(21987): at libcore.io.IoBridge.recvfrom(IoBridge.java:509) 
01-23 11:57:05.949: W/System.err(21987): at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488) 
01-23 11:57:05.949: W/System.err(21987): at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46) 
01-23 11:57:05.949: W/System.err(21987): at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240) 
01-23 11:57:05.949: W/System.err(21987): at java.io.InputStream.read(InputStream.java:162) 
01-23 11:57:05.949: W/System.err(21987): at java.io.BufferedInputStream.fillbuf(BufferedInputStream.java:142) 
01-23 11:57:05.949: W/System.err(21987): at java.io.BufferedInputStream.read(BufferedInputStream.java:227) 
01-23 11:57:05.949: W/System.err(21987): at com.android.okhttp.internal.Util.readAsciiLine(Util.java:316) 
01-23 11:57:05.959: W/System.err(21987): at com.android.okhttp.internal.http.RawHeaders.fromBytes(RawHeaders.java:308) 
01-23 11:57:05.959: W/System.err(21987): at com.android.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:135) 
01-23 11:57:05.959: W/System.err(21987): at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:644) 
01-23 11:57:05.959: W/System.err(21987): at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:347) 
01-23 11:57:05.959: W/System.err(21987): at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:296) 
01-23 11:57:05.959: W/System.err(21987): at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:503) 
01-23 11:57:05.959: W/System.err(21987): at com.retepcivorodot.stockcharts.QuoteListFragment$MyAsyncTask.doInBackground(QuoteListFragment.java:949) 
01-23 11:57:05.959: W/System.err(21987): at com.retepcivorodot.stockcharts.QuoteListFragment$MyAsyncTask.doInBackground(QuoteListFragment.java:1) 
01-23 11:57:05.959: W/System.err(21987): at android.os.AsyncTask$2.call(AsyncTask.java:288) 
01-23 11:57:05.969: W/System.err(21987): at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
01-23 11:57:05.969: W/System.err(21987): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
01-23 11:57:05.969: W/System.err(21987): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
01-23 11:57:05.969: W/System.err(21987): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
01-23 11:57:05.969: W/System.err(21987): at java.lang.Thread.run(Thread.java:841) 
01-23 11:57:05.969: W/System.err(21987): Caused by: libcore.io.ErrnoException: recvfrom failed: ECONNRESET (Connection reset by peer) 
01-23 11:57:05.979: W/System.err(21987): at libcore.io.Posix.recvfromBytes(Native Method) 
01-23 11:57:05.979: W/System.err(21987): at libcore.io.Posix.recvfrom(Posix.java:141) 
01-23 11:57:05.979: W/System.err(21987): at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:164) 
01-23 11:57:05.979: W/System.err(21987): at libcore.io.IoBridge.recvfrom(IoBridge.java:506) 
01-23 11:57:05.979: W/System.err(21987): ... 21 more 

回答

1

我很肯定這無關你的代碼,只有當你可以用一個特定的請求重現。

ECONNRESET表示連接被服務器關閉(也可以是防火牆等)。

我有同樣的問題一次約50%的請求,事實證明,羣集的一方有虛擬IP映射的硬件缺陷。所以當負載均衡器決定將請求傳遞給左側時,它就起作用了,而右側則失敗了。

試着找出你的請求與成功/失敗之間是否存在某種關聯。可能某種應用程序防火牆在執行某些請求時會斷開您的連接。也許是拒絕服務的保護...

如果成功/失敗是絕對隨機的,那麼可能是Android手機和雅虎服務器之間的設備壞了。

您可以跟蹤提交請求所需的時間,直到您收到回覆。失敗的請求是否總是需要更多時間?我可以想象,您所調用的REST服務的Web服務器隊列可能會在連接滿時斷開連接。這也可以通過負載均衡器或防火牆來完成,因爲大多數Web服務器都會使用HTTP代碼503進行回答。

相關問題