2013-03-05 67 views
1

我想生成特定链接的所有错误,如果链接不工作,那么它应该显示特定的Web服务器错误。如何生成所有的Web服务器错误programaticallly

这是我的代码。请建议应该在哪里程序应该做,我可以得到所有的错误,如果链接不上的“如何使用Visual Studio和MSDN上找到方法和相关的例外帮助”工作

public partial class WebForm2 : System.Web.UI.Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     {   

     } 
     protected void btnRender_Click(object sender, EventArgs e) 
     { 
      string strResult = string.Empty; 

      WebResponse objResponse; 
      WebRequest objRequest = System.Net.HttpWebRequest.Create(urltxt.Text); 

      objResponse = objRequest.GetResponse(); 

      using (StreamReader sr = new StreamReader(objResponse.GetResponseStream())) 
      { 
       strResult = sr.ReadToEnd(); 
       sr.Close(); 
      } 
      strResult = strResult.Replace("<form id='form1' method='post' action=''>", ""); 
      strResult = strResult.Replace("</form>", ""); 
      //strResult = strResult.Replace("<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" /><html xmlns="http://www.w3.org/1999/xhtml">"); 
      div.InnerHtml = strResult; 

     } 

     protected void btn_createlink_Click(object sender, EventArgs e) 
     { 
      var links = TextBox1.Text.Split(new string[] { "\n", "\r" }, StringSplitOptions.RemoveEmptyEntries); 
      foreach (var link in links) 
      { 
       if (!IsLinkWorking(link)) 
       { 
        //Here you can show the error. You don't specify how you want to show it. 
        TextBox2.Text += string.Format("{0}\nNot working\n\n ", link); 
       } 
       else 
       { 
        TextBox2.Text += string.Format("{0}\n working\n\n", link); 
       } 
      } 
     } 


bool IsLinkWorking(string url) 
{ 
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url); 

    //You can set some parameters in the "request" object... 
    request.AllowAutoRedirect = true; 
    ServicePointManager.ServerCertificateValidationCallback = (s, cert, chain, ssl) => true; 

    try 
    { 
     HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 

     return true; 
    } 
    catch 
    { 
     //TODO: Check for the right exception here 
     return false; 
    } 
} 
+0

请添加上你有什么问题的代码信息。 – 2013-03-05 05:51:45

+0

如果(!!IsLinkWorking(link)) { TextBox2.Text + = string.Format(“{0} \ nNot working \ n \ n”,link),我想生成类似401错误的错误代码。 } else { TextBox2.Text + = string.Format(“{0} \ n working \ n \ n”,link); }} – hitarth 2013-03-05 05:53:21

+0

@AlexeiLevenkov我需要打印特定的错误,而不是没有工作的打印 – hitarth 2013-03-05 06:00:25

回答

1

答:

  • 在Visual Studio中选择你需要的信息并按下F1。将显示给定方法的帮助。或者,您可以搜索您最喜欢的搜索引擎(即http://bing.com),以获取课程+方法名称,即http://www.bing.com/search?q=HttpWebResponse.GetResponse
  • 有关给定方法的MSDN页面将从帮助中提出,或者通常会成为搜索结果中的第一个结果 - 请阅读它。
  • 大多数方法都包含“例外”部分,其中列出了例外情况,并且通常包含“备注”部分以涵盖详细信息。 在你的情况下,HttpWebResponse.GetResponse表明它抛出异常和备注部分WebException进入异常的细节。特别是它提到了WebException.Response属性,其中涵盖了您正在查找的内容(包括示例代码)。从Status文章显示使用

部分样品和Response.StatusCode

try 
{ 
    var myHttpWebRequest = (HttpWebRequest) WebRequest.Create(pathThatReturns404); 
    var myHttpWebResponse = (HttpWebResponse) myHttpWebRequest.GetResponse(); 
} 
catch(WebException e) 
{ 
    if(e.Status == WebExceptionStatus.ProtocolError) 
    { 
     Console.WriteLine("Status Code : {0}", 
      ((HttpWebResponse)e.Response).StatusCode); 
    } 
}