2013-03-05 62 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); 
    } 
}