Csharp/C#教程:我如何通过.NET学习我的客户端IP?分享


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

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

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

static void Main(string[] args) { const string url = "https://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( "https://www.whatismyip.com/automation/n09230945.asp")); } 

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

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

这样做是这样的:

 class Program { static void Main(string[] args) { string whatIsMyIp = "https://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的方法#

上述就是C#学习教程:我如何通过.NET学习我的客户端IP?分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

 string pstrClientAddress = HttpContext.Current.Request.UserHostAddress; 

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/955665.html

(0)
上一篇 2021年11月21日
下一篇 2021年11月21日

精彩推荐