WebException阅读WebException的响应流时,WebException

由网友(思念是一种病)分享简介:我从.net中的Web服务器进行通信。 Web服务器抛出500内部服务器错误,并写了一个详细的错误信息。我想读的是从网络异常收到的错误消息,但得到其他网络异常。为什么第二WebException抛出?尝试{VAR WebResponse类=(HttpWebResponse)webRequest.GetResponse...

我从.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."+
                        "nnException 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);
}

阅读全文

相关推荐

最新文章