0

这可能是一个愚蠢的问题,我只是错过了一些明显的东西。我环顾四周处理网络错误等问题,但似乎无法找到为什么会发生这种情况的答案。

基本上,我正在调用的服务器会发送 json 并返回,但在请求上有间歇性问题 - 即 502 BadGateway - 没有其他问题。当我说间歇性时,我强调,因为它可以完美地处理 200 个请求,而不会在 5 小时内失败,然后抛出 502,但它也可能发生在第一个请求上。很难预测。另外,应该注意的是我无权访问服务器,它的配置或类似的东西。完全偏远。

在开发环境中,我可以继续通过异常并且程序继续运行并且 - 使用相同请求的同一服务器 - 在下一个请求中,如果我没有收到它成功处理的 502。但是,在远离开发平台的情况下,我会遇到没有异常数据的硬崩溃。

我尝试移动 try/catch,将问题调用与其余代码分开,将其组合等等,我总是收到同样的问题。

已经很晚了,我可能只是没有正确处理它,所以任何指导都值得赞赏。今晚我拒绝放弃这件事——我的眼中钉了大约一个星期。如果我只是一个磨砂膏并且会看到新鲜的眼睛,请嘲笑我并叫我出来。

提前致谢

附言。在 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;
                }
            }

        }
4

1 回答 1

1

它可能不是一个WebException

尝试在处理程序中添加一个 catch Exception

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
}
于 2016-01-12T13:05:22.690 回答