2011-05-13 37 views
0

鉴于以下原因,为什么网站不是有效?我收到以下错误消息。为什么不会通过这个网络请求通过有效网站

try { 
    HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(@"http://wrtire1.tru-m.com/"); 
    //2 websites that do this 
    // http://wrtire1.tru-m.com/ 
    // http://acuraofaugusta.mobi/index.htm 
    webReq.Method = "HEAD";  
    HttpWebResponse webRes = (HttpWebResponse)webReq.GetResponse(); 

    if (webRes.StatusCode == HttpStatusCode.OK) 
    Label1.Text = true.ToString(); 
} catch (Exception e) { 
    Label1.Text = "The remote server returned an error: (403) Forbidden. "; 
    // or I got this: The remote server returned an error: (500) Internal Server Error. 
} 

我收到此错误信息

The remote server returned an error: (500) Internal Server Error.

+7

嗯,怎么样的错误信息?这会花5秒钟做'Label1.Text = e.Message' – mellamokb 2011-05-13 20:06:43

+5

@mellamokb'e.ToString()'会更好,因为它也会包含堆栈跟踪。 – Oded 2011-05-13 20:08:59

+3

为什么要捕捉一个异常,如果你只是忽略它呢?关于错误消息有趣的是,它们倾向于包含有用的信息。 – David 2011-05-13 20:09:49

回答

0

发生这种情况,因为Web服务器已经在你的HTTP请求的用户代理一看而拒绝访问,因为它知道你的应用程序是不是合法网页浏览器。

很多网站都会这样做,以减少漫游器和网络爬虫的流量。

Here's more information on the concept