在Windows上,我可以获得WebException
和StatusCode == HttpStatusCode.NotFound
,如果
在Windows上,对于这两种情况,WebException
的状态都是WebExceptionStatus.UnknownError
。
我如何区分1
病例和案例2
?
我观察到的是,当连接不好(没有找到服务器)时,ResponseUri
为null,而WebResponse
的标头包含0
项。
编辑
ResponseUri
不是空的,而是它的OriginalString
为空。
这样做安全吗?
catch (WebException ex)
{
switch (ex.Response.StatusCode)
{
...
case HttpStatusCode.NotFound:
if (ex.Response.ResponseUri == null
|| string.IsNullOrEmpty(httpWebResponse.ResponseUri.OriginalString))
DoServerNotFound();
else
DoServerReturned404();
发布于 2012-11-02 02:13:57
在进行网络呼叫之前,为什么不使用NetworkInterface.GetIsNetworkAvailable()
来查看是否有活动的网络连接。否则,你就会白白等待,浪费一个电话。如果存在连接,而调用仍然失败,您就知道这是web服务的一个问题。
我在我的应用程序中使用这个,如果没有连接,我就不用打电话了。
谢谢
最大值
发布于 2013-12-12 13:53:31
如果你得到了“找不到”的例外,因为床上的请求,这将有所帮助。
catch (WebException ex)
{
using (WebResponse webResponse = ex.Response)
{
HttpWebResponse httpResponse = (HttpWebResponse)webResponse;
using (Stream data = webResponse.GetResponseStream())
using (var reader = new StreamReader(data))
{
string string = reader.ReadToEnd();
}
}
}
https://stackoverflow.com/questions/12951610
复制相似问题