首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WebException getResponse()未捕获

WebException getResponse()未捕获
EN

Stack Overflow用户
提问于 2016-01-12 13:00:02
回答 1查看 290关注 0票数 0

这可能是个愚蠢的问题,我只是遗漏了一些显而易见的问题。我环顾四周看过处理网络错误和类似的事情,但我似乎找不到答案来解释为什么会发生这种情况。

基本上,我所调用的服务器发送json和回传,但在请求(即502 BadGateway )上有间歇性问题--没有其他问题。当我说断断续续的时候,我强调,因为它可以完美地工作200个请求,不失败超过5个小时,然后抛出502,但它也可能发生在第一个请求。很难预测。另外,应该注意的是,我没有访问服务器的权限,这是信任或类似的东西。完全遥远。

在dev环境中,我可以通过异常继续运行,程序继续运行--使用相同的请求--在下一个请求中,如果我不成功地接收502它处理的话。然而,独立于dev平台之外,我就会遇到一次不例外的数据崩溃。

我尝试移动try/catch,将问题调用从代码的其余部分分离出来,然后将其组合在一起,这样,我总是收到相同的问题。

已经很晚了,我可能只是没有正确地处理它,所以任何的指导都是非常感激的。今晚我拒绝放弃这件事--一周来一直是我身边的一根刺。如果我只是个清洁工,能看到新鲜的眼睛,那就嘲笑我,叫我出去。

提前感谢

ps。异常在getResponse()处发出

代码语言:javascript
复制
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;
                }
            }

        }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-01-12 13:05:22

它可能不是WebException

尝试在处理程序中添加Exception的捕获:

代码语言:javascript
复制
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
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34744384

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档