C# 如何使用.NET学习客户端ip?
我需要从whatismyip.com获得我的客户端ip。但我认为正则表达式模式是不正确的?你能帮我这个忙吗?试着用C# 如何使用.NET学习客户端ip?,c#,.net,visual-studio,network-programming,C#,.net,Visual Studio,Network Programming,我需要从whatismyip.com获得我的客户端ip。但我认为正则表达式模式是不正确的?你能帮我这个忙吗?试着用 这要简单得多 或者,您想准确地解析信息,比如whatismyip.com?这样做: class Program { static void Main(string[] args) { string whatIsMyIp = "http://www.whatismyip.com/automation/n09230945.asp"; W
这要简单得多
或者,您想准确地解析信息,比如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();
}
}
您是否阅读了获取的HTML中的注释: 请将代码设置为scrape 您的IP地址来自 www.whatismyip.com/automation/n09230945.asp 有关更多信息,请参阅我们的 “推荐的自动化实践” 论坛中的线程 因此,这应该让你开始:
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);
}
}
这就是在ASP.NETC中获取ip的方式#
您为什么使用whatismyip.com检索您的ip?:)投票结束。同一个问题不要问两次。如果你的问题没有得到答案,试着用更多的细节来扩展它。值得一提的是,我相信我对您的原始问题的回答应该是这样的:是的,Jørns对原始问题的回答完全足够了。使用是一种很好的技术,可以自动删除变量使用的资源。。
string pstrClientAddress = HttpContext.Current.Request.UserHostAddress;