C# 获取真实IP地址
电脑版发表于:2020/4/30 9:32
//192.168开头的IP是一个代理Ip并不是你真实的IP
public string GetRelayIP()
{
WebClient myWebClient = new WebClient();
myWebClient.Credentials = CredentialCache.DefaultCredentials;
byte[] pageData = myWebClient.DownloadData("https://www.ip38.com/");
string pageHtml = Encoding.UTF8.GetString(pageData);
pageHtml = pageHtml.Replace('\'', '\"');
pageHtml = pageHtml.Replace("ip", "\"ip\"");
pageHtml = pageHtml.Replace("address", "\"address\"");
//根据返回会来的html进行切割 定位到ip
string pram = "<a href=/\"ip\".php?\"ip\"";
string[] arr = pageHtml.Split(pram.ToCharArray());
string strtempa = "=";
string strtempb = ">";
string ipstr = arr[1];
int IndexofA = ipstr.IndexOf(strtempa);
int IndexofB = ipstr.IndexOf(strtempb);
string ip = ipstr.Substring(IndexofA + 1, IndexofB - IndexofA - 1);
return ip;
}