我如何通过.NET学习我的客户端IP?

我需要从whatismyip.com我的客户端IP。 但是我认为正则表达式模式不正确? 你能帮我这个patttern吗?

使用www.whatismyip.com的自动化界面可以更轻松地实现这一点,因此不需要任何正则表达式:

static void Main(string[] args) { const string url = "http://www.whatismyip.com/automation/n09230945.asp"; var client = new WebClient(); try { var myIp = client.DownloadString(url); Console.WriteLine("Your IP: " + myIp); } catch (Exception ex) { Console.WriteLine("Error contacting website: " + ex.Message); } } 

您是否阅读了获取的HTML中的评论:

请将您的代码设置为从www.whatismyip.com/automation/n09230945.asp获取IP。有关详细信息,请参阅论坛中的“推荐的自动化实践”主题。

所以这应该让你去:

 using (var client = new WebClient()) { Console.WriteLine(client.DownloadString( "http://www.whatismyip.com/automation/n09230945.asp")); } 

尝试使用
http://www.whatismyip.org/
它简单得多。

或者你想要解析whatismyip.com的信息?

这样做是这样的:

 class Program { static void Main(string[] args) { string whatIsMyIp = "http://www.whatismyip.com/automation/n09230945.asp"; WebClient wc = new WebClient(); UTF8Encoding utf8 = new UTF8Encoding(); string requestHtml = ""; try { requestHtml = utf8.GetString(wc.DownloadData(whatIsMyIp)); } catch (WebException we) { // do something with exception Console.Write(we.ToString()); } IPAddress externalIp = null; externalIp = IPAddress.Parse(requestHtml); Console.Write("IP Numaram:" + externalIp.ToString()); Console.ReadKey(); } } 

这是你在ASP.NET C中获取ip的方法#

 string pstrClientAddress = HttpContext.Current.Request.UserHostAddress;