如何获得错误信息时,HttpWebRequest.GetResponse()失败 [英] How to get error information when HttpWebRequest.GetResponse() fails

查看:761
本文介绍了如何获得错误信息时,HttpWebRequest.GetResponse()失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发起HttpWebRequest和然后检索它的响应。偶尔,我得到一个500(或至少5 ##)错误,但没有说明。我有超过两个端点控制和想接收端,以获得更多一点的信息。例如,我想从服务器传递异常信息到客户端。这可能使用HttpWebRequest和HttpWebResponse



代码:

 尝试
{
的HttpWebRequest的WebRequest = HttpWebRequest.Create(URL)为HttpWebRequest的;
webRequest.Method = WebRequestMethods.Http.Get;
webRequest.Credentials =新的NetworkCredential(用户名,密码);
webRequest.ContentType =应用/的X WWW的形式,进行了urlencoded
使用(HttpWebResponse响应= webRequest.GetResponse()作为HttpWebResponse)
{
如果(response.StatusCode == HttpStatusCode.OK)
{
//做的东西与response.GetResponseStream();
}
}
}
赶上(异常前)
{
ShowError(除息);
//如果服务器比webRequest.GetResponse()方法
//抛出一个异常返回500错误,我得到的是远程服务器返回错误:(500)
}

任何帮助,这将非常感激。


解决方案

这可能使用HttpWebRequest和HttpWebResponse?




您可以有你的Web服务器只是赶上并写入异常文本到响应的身体,然后设置状态代码为500。现在,当它遇到一个500错误,客户端将抛出一个异常,但你可以阅读响应流并获取异常的消息。



所以,你可以赶上的引发WebException 是如果非200状态码从服务器返回并阅读它的身体究竟会被抛出:

 使用(VAR流= ex.Response.GetResponseStream())
使用(VAR读者=新的StreamReader赶上(引发WebException前)
{
(流))
{
Console.WriteLine(reader.ReadToEnd());
}
}
赶上(异常前)
{
//有更严重的事情
//例如像你没有网络访问
//我们不能谈论服务器异常这里为
//服务器可能从未达到
}


I am initiating an HttpWebRequest and then retrieving it's response. Occasionally, I get a 500 (or at least 5##) error, but no description. I have control over both endpoints and would like the receiving end to get a little bit more information. For example, I would like to pass the exception message from server to client. Is this possible using HttpWebRequest and HttpWebResponse?

Code:

try
{
    HttpWebRequest webRequest = HttpWebRequest.Create(URL) as HttpWebRequest;
    webRequest.Method = WebRequestMethods.Http.Get;
    webRequest.Credentials = new NetworkCredential(Username, Password);
    webRequest.ContentType = "application/x-www-form-urlencoded";
    using(HttpWebResponse response = webRequest.GetResponse() as HttpWebResponse)
    {
        if(response.StatusCode == HttpStatusCode.OK)
        {
            // Do stuff with response.GetResponseStream();
        }
    }
}
catch(Exception ex)
{
    ShowError(ex);
    // if the server returns a 500 error than the webRequest.GetResponse() method
    // throws an exception and all I get is "The remote server returned an error: (500)."
}

Any help with this would be much appreciated.

解决方案

Is this possible using HttpWebRequest and HttpWebResponse?

You could have your web server simply catch and write the exception text into the body of the response, then set status code to 500. Now the client would throw an exception when it encounters a 500 error but you could read the response stream and fetch the message of the exception.

So you could catch a WebException which is what will be thrown if a non 200 status code is returned from the server and read its body:

catch (WebException ex)
{
    using (var stream = ex.Response.GetResponseStream())
    using (var reader = new StreamReader(stream))
    {
        Console.WriteLine(reader.ReadToEnd());
    }
}
catch (Exception ex)
{
    // Something more serious happened
    // like for example you don't have network access
    // we cannot talk about a server exception here as
    // the server probably was never reached
}

这篇关于如何获得错误信息时,HttpWebRequest.GetResponse()失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆