上次说了,如何通过post来取得网页内容。可是有一个问题出现了。当时用的方法是同步操作,如果我其中的一个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)
- {
-
- 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());
-
- Thread.Sleep(1000);
- }
- Thread.Sleep(5000);
- dt.Dispose();
-
- ChangeUserCode();
- }
- }
- public void getPost(string id)
- {
-
-
-
-
-
-
- singleid = id;
-
- string singleip = db.GetSingleValue("UserCode", "UserIP", "id=" + singleid).Trim();
- string strAction = "2";
- string strCode = "";
-
- ASCIIEncoding encoding = new ASCIIEncoding();
- string postData = "ip=" + singleip;
- postData += "&action=" + strAction;
- byte[] data = encoding.GetBytes(postData);
-
- 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;
-
-
- HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
- Stream streamResponse = response.GetResponseStream();
- StreamReader streamRead = new StreamReader(streamResponse, Encoding.Default);
- string content = streamRead.ReadToEnd();
-
-
-
- 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();
-
- }
- 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";
- }
-
- 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;
- }
- }
- }
- }
- }
- }
下面的是周公在CSDN上一个帖子上给出的代码是用BeginGetRequestStream,GetResponseStream来完成的
- using System;
- using System.Net;
- using System.IO;
- using System.Text;
- using System.Threading;
- class HttpWebRequestBeginGetRequest
- {
- public static ManualResetEvent allDone = new ManualResetEvent(false);
- public static void Main()
- {
-
-
- HttpWebRequest request=(HttpWebRequest) WebRequest.Create("http://www.contoso.com/example.aspx");
-
-
- request.ContentType="application/x-www-form-urlencoded";
-
- request.Method = "POST";
-
- request.BeginGetRequestStream(new AsyncCallback(ReadCallback), request);
-
-
-
-
- allDone.WaitOne();
-
- HttpWebResponse response = (HttpWebResponse)request.GetResponse();
- Stream streamResponse = response.GetResponseStream();
- StreamReader streamRead = new StreamReader(streamResponse);
- string responseString = streamRead.ReadToEnd();
- Console.WriteLine(responseString);
-
- streamResponse.Close();
- streamRead.Close();
-
-
- response.Close();
- }
-
- private static void ReadCallback(IAsyncResult asynchronousResult)
- {
- HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
-
- Stream postStream = request.EndGetRequestStream(asynchronousResult);
- Console.WriteLine("Please enter the input data to be posted:");
- string postData = Console.ReadLine ();
-
-
- byte[] byteArray = Encoding.UTF8.GetBytes(postData);
-
- postStream.Write(byteArray, 0, postData.Length);
- postStream.Close ();
- allDone.Set();
- }
- }
这二个有什么区别呢,自己好好看一下吧。