1

リンクが機能していない場合、特定のリンクのすべてのエラーを生成したいので、特定のWebサーバーエラーが表示されるはずです。

これが私のコードです。リンクが機能していない場合、すべてのエラーが発生する可能性があるため、プログラムをどこで実行する必要があるかを提案してください

  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;
    }
}
4

1 に答える 1

1

「VisualStudioとMSDNを使用してメソッドと関連する例外に関するヘルプを見つける方法」に関する回答:

  • Visual Studioで、情報が必要なメソッドを選択し、F1キーを押します。指定されたメソッドのヘルプが表示されます。または、お気に入りの検索エンジン(http://bing.comなど)でクラス+メソッド名(http://www.bing.com/search?q=HttpWebResponse.GetResponseなど)を検索することもできます。
  • 指定されたメソッドのMSDNページは、ヘルプから表示されるか、通常は検索結果の最初の結果の1つになります-読んでください。
  • ほとんどのメソッドには、例外をリストする「例外」セクションと、多くの場合、詳細をカバーする「備考」セクションが含まれています。あなたの場合、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);
    }
}
于 2013-03-05T06:27:50.783 に答える