|
上文我们说了,如何使用HttpWebRequest的POST取得网页的内容,访问地址是:/html/net/aspnet/20081202/3026.html。可是有一个问题出现了。当时用的方法是同步操作,如果我其中的一个IP或是在进行转化的过程中,出现了问题,哪么这个程序就会停下来,当然了,有的朋友可能会说,用try...catch也可以啊。因为我是循环取值,所以在catch里加一个continue就行了。可是以前没有搞过异步操作,所以想用这个机会搞一下。就看了一下。
这里主要说一下用BeginGetResponse,EndGetResponse来完成异步操作。 先说一下,我的要求: 一,从数据库中取出所有的IP 二,利用httpwebrequest的post方法在网上取得它的相应地址。当然了,是用post还是用get主要是看网上的网站是用什么方法来传值。 三,将所得到的值写入数据库
下面我把我的代码放上来,前台没有什么东西,运行的结果是直接写入数据库, 数据库名为DBCT_Dev 省份表S_Province,其中ProvinceName为省分名。ProvinceCode为省分相对的Code 市级表S_City,其中CityName为市名。ZipCode为市相对应的Code。 这二个表是在网上找到的,有很多中国省市关联表,大家可以去下一个。 用户IP表UserCode,其中Code为其IP所对应的省市Code,IP为用户的IP地址。
好了,下面是代码
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 two : System.Web.UI.Page { public DBClass db = new DBClass();//我自己写的一个类,主要是用来做一些数据库操作的,你可以使用你自己的 public static Hashtable ht = new Hashtable(); private static ManualResetEvent allDone = new ManualResetEvent(false); public static string singleid;
protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { //将数据库中Code为空ip不为空的记录存入,放到一个DataTable中。 string strsql = "select * from UserCode where UserIP<>'' and (Code is null or code = '')"; DataTable dt = db.GetDataTable(strsql); for (int i = 0; i < dt.Rows.Count; i++) { //调用转化方法 getPost(dt.Rows[i]["id"].ToString());
//每次调用间隔一秒,通过实验得到,如果你的数据量大,最好间隔5秒 Thread.Sleep(1000); } Thread.Sleep(5000); dt.Dispose();
//调用将转化信息存入数据库的方法 ChangeUserCode(); } }
public void getPost(string id) { //定义变量 //singleid 传入的用户信息ID //singleip 根据用户信息ID,而取到的IP //strAction post必传值之一 //strCode 传入用户信息表中的code值 // singleid = id;
//通过GetSingleValue方法,得到相应的值,这里GetSingleValue(表名,要得到的值,条件语句) string singleip = db.GetSingleValue("UserCode", "UserIP", "id=" + singleid).Trim(); string strAction = "2"; string strCode = "";
//将得到的post值,转化为byte型 ASCIIEncoding encoding = new ASCIIEncoding(); string postData = "ip=" + singleip; postData += "&action=" + strAction; byte[] data = encoding.GetBytes(postData);
//建立HttpWebRequest HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://www2.ip138.com/ips8.asp"); myRequest.Method = "POST"; myRequest.ContentType = "application/x-www-form-urlencoded"; myRequest.ContentLength = data.Length;
Stream myStream = myRequest.GetRequestStream(); myStream.Write(data, 0, data.Length); myStream.Close();
//开始调用异步操作 myRequest.BeginGetResponse(new AsyncCallback(ReadCallback), myRequest);
allDone.WaitOne(); }
private void ReadCallback(IAsyncResult asynchronousResult) { HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
//Stream postStream = request.EndGetResponse(asynchronousResult);
//异步回调方法使用 EndGetResponse 方法返回实际的 WebResponse。 HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult); Stream streamResponse = response.GetResponseStream(); StreamReader streamRead = new StreamReader(streamResponse, Encoding.Default); string content = streamRead.ReadToEnd(); //string con = responseString.Substring(responseString.IndexOf("本站主数据") + 6, responseString.IndexOf("</li><li>参考数据一") - responseString.IndexOf("本站主数据") - 1); //Response.Write(con + "<br>");
//判断是否是有省市的信息,如果信息中有省,市,则进行存入数据库,如为127.0.0.1,是直接给本省code。其它信息,则不给记录 string singleip = ""; string strCode = ""; if (singleip == "127.0.0.1") { strCode = "0008"; } else { if (content.IndexOf("省") == -1 && content.IndexOf("市") == -1 && singleid != "127.0.0.1") { if (content.IndexOf("查询太频繁") != -1) { strCode = "查询太频繁"; } else { strCode = "0035"; } } else { if (content.IndexOf("省") != -1) { string con = content.Substring(content.IndexOf("本站主数据") + 6, content.IndexOf("</li><li>参考数据一") - content.IndexOf("本站主数据") - 1); string strpro = con.Substring(0, con.IndexOf("省") + 1); strCode = db.GetSingleValue("S_Province", "ProvinceCode", "ProvinceName='" + strpro + "'").Trim(); //strCode = strpro; }
if (content.IndexOf("市") != -1) { string con = content.Substring(content.IndexOf("本站主数据") + 6, content.IndexOf("</li><li>参考数据一") - content.IndexOf("本站主数据") - 1); string strpro = con.Substring(con.IndexOf("省") + 1, con.IndexOf("市") - con.IndexOf("省")); strCode += db.GetSingleValue("S_City", "ZipCode", "CityName='" + strpro + "'").Trim(); ; } } }
if (strCode == "") { strCode = "0035"; } //将信息存入hashtable ht.Add(singleid, strCode); streamResponse.Close(); streamRead.Close(); response.Close(); allDone.Set(); }
public void ChangeUserCode() { if (ht.Count > 0) { foreach (DictionaryEntry objDE in ht) { string strsql = "update UserCode set Code='" + objDE.Value.ToString() + "' where id=" + objDE.Key.ToString() + ""; try { db.ExecuteSql(strsql); } catch { continue; } } } } } }
共2页: 上一页 1 [2] 下一页
|