- 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 Four : System.Web.UI.Page
- {
- private delegate string DelegateName(string strIP, out string OldIP);
- public DBClass db = new DBClass();
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!Page.IsPostBack)
- {
- DelegateName dn = new DelegateName(MethodName);
- string strsql = "select top 30 * from UserCode where UserIP<>'' and (Code is null or code = '')";
- DataTable dt = db.GetDataTable(strsql);
- for (int i = 0; i < dt.Rows.Count; i++)
- {
- AsyncCallback acb = new AsyncCallback(CallBackMethod);
- string OldIP;
- IAsyncResult iar = dn.BeginInvoke(dt.Rows[i]["id"].ToString(), out OldIP, acb, dn);
- //每次调用间隔一秒,通过实验得到,如果你的数据量大,最好间隔5秒
- iar.AsyncWaitHandle.WaitOne();
- Thread.Sleep(5000);
- }
- dt.Dispose();
- }
- }
- private void CallBackMethod(IAsyncResult ar)
- {
- //从异步状态ar.AsyncState中,获取委托对象
- DelegateName dn = (DelegateName)ar.AsyncState;
- //输出参数
- string OldIP;
- //一定要EndInvoke,否则你的下场很惨
- string r = dn.EndInvoke(out OldIP, ar);
- //MessageBox.Show("异步完成喽!i的值是" + i.ToString() + ",r的值是" + r);
- string strsql = "update UserCode set Code='" + r + "' where id=" + OldIP + "";
- try
- {
- db.ExecuteSql(strsql);
- }
- catch
- {
- }
- }
- private string MethodName(string strID, out string OldIP)
- {
- //定义变量
- //singleid 传入的用户信息ID
- //singleip 根据用户信息ID,而取到的IP
- //strAction post必传值之一
- //strCode 传入用户信息表中的code值
- //
- OldIP = strID;
- string singleid = strID;
- 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();
- HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
- StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.Default);
- string content = reader.ReadToEnd();
- //判断是否是有省市的信息,如果信息中有省,市,则进行存入数据库,如为127.0.0.1,是直接给本省code。其它信息,则不给记录
- 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";
- }
- //strIP =
- return strCode;
- }
- }
- }
异步操作的一个例子
最新推荐文章于 2023-01-29 16:23:27 发布