|
如何我们来测试一个网站的资料页面哪些页面不好用呢?如果数据量大的话,一个个查肯定划不来的,下面咱们自己写程序来解决这个问题
在这里我们用到了一个知识点,使用HttpWebRequest的POST取得网页内容。这篇文章以前发过,链接地址如下: 如何使用HttpWebRequest的POST取得网页的内容? /html/net/aspnet/20081202/3026.html
异步操作使用HttpWebRequest的POST取得网页的内容 /html/net/aspnet/20081202/3027.html 我们可以采用以上方法来取得相应值。
在这里我用代码试了一下,发现如果页面没有或是报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();是我写的一个基础类,用来操作数据库的,你可以自己写一个自己的。
|