兄弟我做一个把IP转化为相应地址的程序。现在想做到异步功能,可是不知道如何处理,下面是我的代码,请问在什么地方修改变成异步呢。
我现在的代码是给了一个定的IP,因为我以后,要进行从数据库里,提取大量IP进行转化。所以要采用异步。下面是我的程序string strIP = this.txtIP.Text.Trim();
string strAction = "2";
ASCIIEncoding encoding = new ASCIIEncoding();
string postData = "ip=" + strIP;
postData += "&action=" + strAction;
byte[] data = encoding.GetBytes(postData); HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://www.ip138.com/ips.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();
string con = content.Substring(content.IndexOf("本站主数据") + 6, content.IndexOf("</li><li>参考数据一") - content.IndexOf("本站主数据") - 1); this.lblViewIP.Text = con;
我现在的代码是给了一个定的IP,因为我以后,要进行从数据库里,提取大量IP进行转化。所以要采用异步。下面是我的程序string strIP = this.txtIP.Text.Trim();
string strAction = "2";
ASCIIEncoding encoding = new ASCIIEncoding();
string postData = "ip=" + strIP;
postData += "&action=" + strAction;
byte[] data = encoding.GetBytes(postData); HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://www.ip138.com/ips.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();
string con = content.Substring(content.IndexOf("本站主数据") + 6, content.IndexOf("</li><li>参考数据一") - content.IndexOf("本站主数据") - 1); this.lblViewIP.Text = con;
但是丛来还没有用过异步的,
所以帮不了你
但是我可以告诉你,
同步肯定也没问题啊,
我们就没有用异步,网站的访问量也很大的
Thread
如果仅仅是这个原因 我的同步也完全能够实现
就是整体加一下try
catch的时候不进行任何操作直接继续就可以啦