双11狂欢大促

盲盒赢 定制羽绒服

开启惊喜盲盒,抽取你的专属好礼

— 惊喜奖品抢先看 —

HttpWebRequest的异步问题

chenguang79 chenguang79 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
兄弟我做一个把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;
...全文
评论 22
收藏 已收藏
分享
返回顶部
chenguang79
.NET社区 62,274 成员 668,968 内容