簡體   English   中英

WebException getResponse()無法捕獲

[英]WebException getResponse() not catching

這可能是一個愚蠢的問題,我只是缺少明顯的東西。 我到處都在處理Web錯誤等問題,但似乎找不到為什么發生這種情況的答案。

基本上,我正在從中調用的服務器發送json並返回,但是在請求上存在間歇性問題-即502 BadGateway-沒有其他問題。 當我說斷斷續續時,我強調一點,因為它可以完美地處理200個請求,而不會在5個小時內失敗,然后拋出502,但也可能在第一個請求上發生。 很難預測。 另外,應該指出,我無權訪問服務器,它是配置文件或類似的東西。 完全遠程。

在開發環境中,我可以繼續處理異常,並且程序可以繼續運行,並且在下一個請求下,如果我未成功接收到它處理成功的502,則對​​下一個請求使用相同的請求到同一服務器。 但是,獨立於開發平台之外,我沒有任何異常數據就陷入了嚴重崩潰。

我試過四處移動try / catch,將問題調用與其余代碼分開,將其組合在一起,依此類推,而我總是收到同樣的問題。

已經很晚了,我可能只是處理不正確,所以請多多指教。 我拒絕今晚放棄這件事-大約一個星期以來,我一直很痛苦。 如果我只是磨砂膏,而且會以新鮮的眼神看着我,請嘲笑我並叫我出來。

提前致謝

ps。 在getResponse()處發出異常

try
        { 
            using (HttpWebResponse inboundResponse = (HttpWebResponse)outboundRequest.GetResponse()) //exception here for 502
            using (StreamReader stream = new StreamReader(inboundResponse.GetResponseStream()))
            using (JsonTextReader reader = new JsonTextReader(stream))
            {
                List<T> outputData = new List<T>();
                while (reader.Read())
                {
                    JsonSerializer serializer = new JsonSerializer();
                    var tempData = serializer.Deserialize<T>(reader);
                    outputData.Add(tempData);
                }
                inboundResponse.Close();
                return outputData;
            }
        }
        catch (WebException e)
        {
            if (e.Status == WebExceptionStatus.ProtocolError)
            {
                UIController.addSystemMessageToChat("Protocol Error");
                switch(((HttpWebResponse)e.Response).StatusCode)
                {
                    case HttpStatusCode.BadGateway:
                        UIController.addSystemMessageToChat("Resending Response");
                        //process exception
                    default:
                        throw;
                }
            }

        }

它可能不是WebException

嘗試將Exception的catch添加到處理程序中:

try
{ 
    using (HttpWebResponse inboundResponse = (HttpWebResponse)outboundRequest.GetResponse()) //exception here for 502
    using (StreamReader stream = new StreamReader(inboundResponse.GetResponseStream()))
    using (JsonTextReader reader = new JsonTextReader(stream))
    {
        List<T> outputData = new List<T>();
        while (reader.Read())
        {
            JsonSerializer serializer = new JsonSerializer();
            var tempData = serializer.Deserialize<T>(reader);
            outputData.Add(tempData);
        }
        inboundResponse.Close();
        return outputData;
    }
}
catch (WebException e)
{
    if (e.Status == WebExceptionStatus.ProtocolError)
    {
        UIController.addSystemMessageToChat("Protocol Error");
        switch(((HttpWebResponse)e.Response).StatusCode)
        {
            case HttpStatusCode.BadGateway:
                UIController.addSystemMessageToChat("Resending Response");
                //process exception
            default:
                throw;
        }
    }

}
catch (Exception e)
{
    // catch and handle an unknown / unexpected exception type
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM