上周五,接到单位的一个活,让我测试一下,单位网站的资料页面,哪些页面不好用。当时以为没多少东西,一个一个试一下就行了。可是在数据库里进行查询的发现,一共有六千多个,这要是一个一个查,我周未就不用休息了。
得想一个方法让他自己查,然后告诉我哪个出了问题。
在这里我想到了,以前写的一个 使用HttpWebRequest的POST取得网页内容。我们可以采用它来取得相应值。
在这里我用代码试了一下,发现如果页面没有或是报400的错误的时候,HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();这句会报错。这时我就想到,用try.....catch来解决这个方法。
下面是我的代码。
这里要说明一点的是,我的页面是有规则的,不是无规则的,因为如果是无规则的话,我想你就得一个一个试了,处非,这些页面全都保存在数据库中的一个字段里,否则你用程序来判断还不用手动的快。
这是我的程序,
DBClass db = new DBClass();是我写的一个基础类,用来操作数据库的,你可以自己写一个自己的。
得想一个方法让他自己查,然后告诉我哪个出了问题。
在这里我想到了,以前写的一个 使用HttpWebRequest的POST取得网页内容。我们可以采用它来取得相应值。
在这里我用代码试了一下,发现如果页面没有或是报400的错误的时候,HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();这句会报错。这时我就想到,用try.....catch来解决这个方法。
下面是我的代码。
这里要说明一点的是,我的页面是有规则的,不是无规则的,因为如果是无规则的话,我想你就得一个一个试了,处非,这些页面全都保存在数据库中的一个字段里,否则你用程序来判断还不用手动的快。
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.IO;
- using System.Text;
- using System.Net;
- using System.Threading;
- using System.Data;
- using System.Collections;
- namespace getPageValue
- {
- public partial class getPageUrlNullNum : System.Web.UI.Page
- {
- DBClass db = new DBClass();
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!Page.IsPostBack)
- {
- }
- }
- protected void Button1_Click(object sender, EventArgs e)
- {
- string Url = "http://studybar.cncmax.hlj.net/classonline/contentrj/";
- string strID = "";
- int num = 0;
- string strsql = "select Identifier,location from BaseResource where resourcetypeid=25";
- DataTable dt = db.GetDataTable(strsql);
- for (int i = 0; i < dt.Rows.Count; i++)
- {
- string strUrl = Url + dt.Rows[i]["location"].ToString();
- HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(strUrl);
- myRequest.Method = "POST";
- myRequest.ContentType = "application/x-www-form-urlencoded";
- Stream myStream = myRequest.GetRequestStream();
- myStream.Close();
- try
- {
- HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
- StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.Default);
- string content = reader.ReadToEnd();
- }
- catch
- {
- num++;
- strID += dt.Rows[i]["Identifier"].ToString() + ",";
- //string strErr = "insert into Temp_ErrID(ErrID)values('" + dt.Rows[i]["Identifier"].ToString() + "')";
- //db.ExecuteSql(strErr);
- }
- }
- this.TextBox1.Text = num.ToString();
- this.TextBox2.Text = strID;
- }
- }
- }
DBClass db = new DBClass();是我写的一个基础类,用来操作数据库的,你可以自己写一个自己的。