.NET程序访问网页,返回当前的动态IP
返回IP的正则表达式:/[(?<IP>[0-9/.]*)/]
public string returnWebIP(string URL, string regExpStr)
{
Uri uri = new Uri(URL);
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = 0;
req.CookieContainer = new CookieContainer();
req.GetRequestStream().Write(new byte[0], 0, 0);
HttpWebResponse res = (HttpWebResponse)(req.GetResponse());
StreamReader rs = new StreamReader(res.GetResponseStream(), Encoding.GetEncoding("GB18030"));
string s = rs.ReadToEnd();
rs.Close();
req.Abort();
res.Close();
Match m = Regex.Match(s, regExpStr);
if (m.Success) return m.Groups["IP"].Value;
string strnetIP = string.Empty;
return strnetIP;
}
本文介绍了一个.NET程序通过HTTP请求从指定网址获取包含当前动态IP信息的网页,并使用正则表达式从中抽取IP地址的方法。
1506

被折叠的 条评论
为什么被折叠?



