繁体   English   中英

我怎么能用.NET学习我的客户端IP?

[英]how can i learn my client ip with .NET?

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

您是否阅读了获取的HTML中的注释:

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

所以这应该让你去:

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

使用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);
        }
    }

尝试使用
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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM