如何判断一个网页是否出现404错误

来源:互联网 发布:网络上下其手的意思 编辑:程序博客网 时间:2024/06/16 07:00
上周五,接到单位的一个活,让我测试一下,单位网站的资料页面,哪些页面不好用。当时以为没多少东西,一个一个试一下就行了。可是在数据库里进行查询的发现,一共有六千多个,这要是一个一个查,我周未就不用休息了。
得想一个方法让他自己查,然后告诉我哪个出了问题。
在这里我想到了,以前写的一个使用HttpWebRequest的POST取得网页内容。我们可以采用它来取得相应值。
在这里我用代码试了一下,发现如果页面没有或是报400的错误的时候,HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();这句会报错。这时我就想到,用try.....catch来解决这个方法。
下面是我的代码。
这里要说明一点的是,我的页面是有规则的,不是无规则的,因为如果是无规则的话,我想你就得一个一个试了,处非,这些页面全都保存在数据库中的一个字段里,否则你用程序来判断还不用手动的快。
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.IO;
  8. using System.Text;
  9. using System.Net;
  10. using System.Threading;
  11. using System.Data;
  12. using System.Collections;
  13. namespace getPageValue
  14. {
  15.     public partial class getPageUrlNullNum : System.Web.UI.Page
  16.     {
  17.         DBClass db = new DBClass();
  18.         protected void Page_Load(object sender, EventArgs e)
  19.         {
  20.             if (!Page.IsPostBack)
  21.             {
  22.             }
  23.         }
  24.         protected void Button1_Click(object sender, EventArgs e)
  25.         {
  26.             string Url = "http://studybar.cncmax.hlj.net/classonline/contentrj/";
  27.             string strID = "";
  28.             int num = 0;
  29.             string strsql = "select Identifier,location from BaseResource where resourcetypeid=25";
  30.             DataTable dt = db.GetDataTable(strsql);
  31.             for (int i = 0; i < dt.Rows.Count; i++)
  32.             {
  33.                 string strUrl = Url + dt.Rows[i]["location"].ToString();
  34.                 HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(strUrl);
  35.                 myRequest.Method = "POST";
  36.                 myRequest.ContentType = "application/x-www-form-urlencoded";
  37.                 Stream myStream = myRequest.GetRequestStream();
  38.                 myStream.Close();
  39.                 try
  40.                 {
  41.                     HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
  42.                     StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.Default);
  43.                     string content = reader.ReadToEnd();
  44.                 }
  45.                 catch
  46.                 {
  47.                     num++;
  48.                     strID += dt.Rows[i]["Identifier"].ToString() + ",";
  49.                     //string strErr = "insert into Temp_ErrID(ErrID)values('" + dt.Rows[i]["Identifier"].ToString() + "')";
  50.                     //db.ExecuteSql(strErr);
  51.                 }
  52.             }
  53.             this.TextBox1.Text = num.ToString();
  54.             this.TextBox2.Text = strID;
  55.         }
  56.        
  57.     }
  58. }
这是我的程序,
DBClass db = new DBClass();是我写的一个基础类,用来操作数据库的,你可以自己写一个自己的。

原创粉丝点击