关于HttpWebRequest 遇到404如何处理
C# code
HttpWebRequest request = (HttpWebRequest)(WebRequest.Create("http://www.baidu.com"));
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
HttpWebRequest request = (HttpWebRequest)(WebRequest.Create("http://www.baidu.com")); 这里如果返回403 或者404如何判断呢??由于程序特殊不能使用catch
使用 if (response.StatusCode == HttpStatusCode.OK) 判断使能实现判断 因为执行到WebRequest.Create("http://www.baidu.com")就会报错
------解决方案--------------------
HttpStatusCode是个枚举,你看看帮助,都列出来了,404是NotFound,403是Forbidden
------解决方案--------------------
if(response.StatusCode == HttpStatusCode.NotFound)
参见
http://msdn.microsoft.com/zh-cn/library/system.net.httpstatuscode.aspx
------解决方案--------------------if(response.StatusCode == HttpStatusCode.NotFound)
if(response.StatusCode == HttpStatusCode.Forbidden)
------解决方案--------------------