获取外网访问者的真实ip,在线等待
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("http://www.ip138.com/ip2city.asp");
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);
WebResponse res = req.GetResponse();
StreamReader rs = new StreamReader(res.GetResponseStream(), Encoding.GetEncoding("gb2312"));
string s = rs.ReadToEnd();
rs.Close();
req.Abort();
res.Close();
Match m = Regex.Match(s, @"(\d+)\.(\d+)\.(\d+)\.(\d+)");
string ipHost = string.Empty;
到这 WebResponse res = req.GetResponse();一步报远(程服务器返回错误: (403) 已禁止。)求解
------解决方案--------------------
参考1:
C# code
string GetInternalIpAddress1()
{
try
{
string strUrl = "http://www.3322.org/dyndns/getip"; //获得IP的网址了
Uri uri = new Uri(strUrl);
WebRequest wr = WebRequest.Create(uri);
Stream s = wr.GetResponse().GetResponseStream();
StreamReader sr = new StreamReader(s, Encoding.Default);
string all = sr.ReadToEnd(); //读取网站的数据
all = all.Replace("\r\n", "");
return all;
}
catch (Exception ex)
{
return "";
}
}