2013-12-16 67 views
0
URL queryUrl = new URL(url); 
InputStream inputStream = null; 
HttpsURLConnection connection = (HttpsURLConnection) queryUrl.openConnection(); 
connection.setRequestProperty("User-Agent", "My Client"); 
connection.setRequestMethod("GET"); 
connection.setDoInput(true); 
inputStream = connection.getInputStream(); 

嗨, 我正在使用上面的代碼來執行HttpGet查詢。 我幾次嘗試一次,發現服務器返回的錯誤代碼爲502或504(這兩種情況都會發生)。 排除異常:當試圖獲得InputStream時獲取錯誤502/504

inputStream = connection.getInputStream() 

任何想法?

您的幫助,將不勝感激。 在此先感謝

+0

什麼類型的異常?什麼是服務器日誌說? –

+0

你會發布錯誤StackTrace! –

+0

服務器返回的HTTP響應代碼:504爲URL:....... \t sun.net.www.protocol.http.HttpURLConnection.getInputStream(未知來源)\t sun.net.www.protocol.https.HttpsURLConnectionImpl .getInputStream(Unknown Source)\t Utils.queryGET(BtcE.java:330) –

回答

1

5xx中的錯誤代碼指示服務器或代理的某些問題。 RFC聲明

Response status codes beginning with the digit "5" indicate cases in which the server is 
aware that it has erred or is incapable of performing the request. Except when responding 
to a HEAD request, the server SHOULD include an entity containing an explanation of the 
error situation, and whether it is a temporary or permanent condition. User agents SHOULD 
display any included entity to the user. These response codes are applicable to any request method. 

請檢查通過讀取URL連接的錯誤蒸汽如下什麼是實際的錯誤: 如果HTTP響應代碼爲4NN(客戶端錯誤)或5NN(服務器錯誤),那麼你可能想閱讀HttpURLConnection#getErrorStream()以查看服務器是否發送了任何有用的錯誤信息。

InputStream error = ((HttpURLConnection) connection).getErrorStream(); 

此外,我認爲與HTTP請求時,您可以使用Apache的HttpClient,而不是與HttpURLConnection的直接合作。使用HttpClient更容易。

+0

感謝您的回覆,我會將errorCode輸出添加到我的代碼中,並嘗試分析。與此同時 - 我在那裏做了什麼可能會導致異常的錯誤? –

+0

由於錯誤是與狀態代碼5xx,我懷疑來自客戶端(你)的任何問題。請在您收到5xx響應期間檢查Web服務器日誌以及代理日誌以獲取任何線索。 – thiyaga

相關問題