0

私はリンク チェッカー/壊れたリンク ファインダーに取り組んでおり、多くの誤検知が発生しています。再確認した後、多くのエラー コードが webexceptions を返していることに気付きましたが、実際にはダウンロード可能でしたが、他の場合にはステータスコードが 404 であり、私はブラウズからページにアクセスできます。

ここにコードがありますが、かなり醜く、もっと実用的なものを用意したいと思っています。すべてのステータスコードは、有効なリンクであるため、壊れたリンクに追加したくないものをフィルタリングするために使用される場合、非常に大きなものです(すべてテストしました)。私が修正する必要があるのは、構造 (可能であれば) と false 404 を取得しない方法です。

ありがとうございました!

try
{
   HttpWebRequest request = ( HttpWebRequest ) WebRequest.Create ( uri );
   request.Method = "Head";
   request.MaximumResponseHeadersLength = 32; // FOR IE SLOW SPEED
   request.AllowAutoRedirect = true;
   using ( HttpWebResponse response = ( HttpWebResponse ) request.GetResponse() )
   {
      request.Abort();
   }
   /* WebClient wc = new WebClient();
     wc.DownloadString( uri ); */

   _validlinks.Add ( strUri );
}
catch ( WebException wex )
{
   if (    !wex.Message.Contains ( "The remote name could not be resolved:" ) &&
           wex.Status != WebExceptionStatus.ServerProtocolViolation )
   {
      if ( wex.Status != WebExceptionStatus.Timeout )
      {
         HttpStatusCode code = ( ( HttpWebResponse ) wex.Response ).StatusCode;
         if (
            code != HttpStatusCode.OK &&
            code != HttpStatusCode.BadRequest &&
            code != HttpStatusCode.Accepted &&
            code != HttpStatusCode.InternalServerError &&
            code != HttpStatusCode.Forbidden &&
            code != HttpStatusCode.Redirect &&
            code != HttpStatusCode.Found
         )
         {
            _brokenlinks.Add ( new Href ( new Uri ( strUri , UriKind.RelativeOrAbsolute ) , UrlType.External ) );
         }
         else _validlinks.Add ( strUri );
      }
      else _brokenlinks.Add ( new Href ( new Uri ( strUri , UriKind.RelativeOrAbsolute ) , UrlType.External ) );
   }
   else _validlinks.Add ( strUri );
}
4

1 に答える 1

1

多くの Web サイトで必要とされるため、UserAgent ヘッダーを追加する必要があります。

于 2010-06-10T14:59:51.983 に答える