WebException阅读WebException的响应流时, [英] WebException when reading a WebException's response stream
本文介绍了WebException阅读WebException的响应流时,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我从.net中的Web服务器进行通信。 Web服务器抛出500内部服务器错误,并写了一个详细的错误信息。
我想读的是从网络异常收到的错误消息,但得到其他网络异常。为什么第二WebException抛出?
尝试
{
VAR WebResponse类=(HttpWebResponse)webRequest.GetResponse();
}
赶上(WebException E)
{
如果(e.Status == WebExceptionStatus.ProtocolError)
{
//下一行抛出一个网络异常
Console.WriteLine(新的StreamReader(e.Response.GetResponseStream())ReadToEnd());
}
}
解决方案
这是为什么奇怪?尝试从MSDN如下:
尝试{
//创建一个无效的网站Web请求。替换了无效的网站强于同一个无效的名称创建通话。
HttpWebRequest的myHttpWebRequest =(HttpWebRequest的)WebRequest.Create(非法网站);
//获取上述要求,相关的响应。
使用(HttpWebResponse myHttpWebResponse =
(HttpWebResponse)myHttpWebRequest.GetResponse()){
myHttpWebResponse.Close();
}
}
赶上(WebException E){
Console.WriteLine(该计划预计将抛出WebException上成功运行。+
\ñ\ n异常消息:+ e.Message);
如果(e.Status == WebExceptionStatus.ProtocolError){
变种响应=((HttpWebResponse)e.Response);
Console.WriteLine(状态code:{0},response.Status code);
Console.WriteLine(状态说明:{0},response.StatusDescription);
尝试 {
使用(VAR流= response.GetResponseStream()){
使用(VAR读卡器=新的StreamReader(流)){
VAR文本= reader.ReadToEnd();
Console.WriteLine(文本);
}
}
}赶上(WebException前){
//呵呵,好了,我们尝试
}
}
}
赶上(例外五){
Console.WriteLine(e.Message);
}
I'm communicating with a web server from .Net. The web server throws a 500 internal server error and writes a detailed error message.
I'm trying to read the error message that is received from a web exception, but getting another web exception. Why is the second WebException thrown?
try
{
var webResponse = (HttpWebResponse)webRequest.GetResponse();
}
catch (WebException e)
{
if (e.Status == WebExceptionStatus.ProtocolError)
{
// the next line throws a web exception
Console.WriteLine(new StreamReader(e.Response.GetResponseStream()).ReadToEnd());
}
}
解决方案
Why is this surprising? Try the following from MSDN:
try {
// Create a web request for an invalid site. Substitute the "invalid site" strong in the Create call with a invalid name.
HttpWebRequest myHttpWebRequest = (HttpWebRequest) WebRequest.Create("invalid site");
// Get the associated response for the above request.
using (HttpWebResponse myHttpWebResponse =
(HttpWebResponse) myHttpWebRequest.GetResponse()) {
myHttpWebResponse.Close();
}
}
catch(WebException e) {
Console.WriteLine("This program is expected to throw WebException on successful run."+
"\n\nException Message :" + e.Message);
if(e.Status == WebExceptionStatus.ProtocolError) {
var response = ((HttpWebResponse)e.Response);
Console.WriteLine("Status Code : {0}", response.StatusCode);
Console.WriteLine("Status Description : {0}", response.StatusDescription);
try {
using (var stream = response.GetResponseStream()) {
using (var reader = new StreamReader(stream)) {
var text = reader.ReadToEnd();
Console.WriteLine(text);
}
}
} catch (WebException ex) {
// Oh, well, we tried
}
}
}
catch(Exception e) {
Console.WriteLine(e.Message);
}
这篇关于WebException阅读WebException的响应流时,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文