1

当访问没有服务器运行的无效 IP 地址时,Microsoft System.Net.Browser.ClientHttpWebRequest 似乎抛出了一个状态代码为 NotFound 的 WebException,这对应于服务器的 404 响应。正确的响应不是 HTTP 状态代码,也绝对不在 400 范围内,因为客户端库从来没有机会与有效的 HTTP 服务器对话。这对我的代码来说是有问题的,因为 404 在我的服务器端点返回时具有特殊含义。

有没有办法确定服务器何时实际返回 404 (NotFound) 响应与被错误地用于描述 System.Net.Browser.ClientHttpWebRequest 类的一般连接故障?

请注意,我在 WP8 上会发生这种行为。

4

1 回答 1

1

我遇到了同样的问题,捕获 System.Net.WebException 并在 catch 语句中插入以下代码行使我能够获得服务器返回的实际响应:

catch(System.Net.WebException we)
{
   using (var streamResponse = we.Response.GetResponseStream())
   {
      using (var streamRead = new StreamReader(streamResponse))
      {
         string responseString = streamRead.ReadToEnd();

         // do something with responseString
      }
   }
}
}
于 2015-10-07T15:22:37.097 回答