简体   繁体   English

如何使用VB.NET或C#将IP地址转换为url

[英]how to convert ip address to url with VB.NET OR C#

I want to convert ip address to url. 我想将IP地址转换为url。 But i can't figure it out how to. 但我不知道如何去做。

It's not entirely clear what you want - an example would have been helpful - but something as simple as: 尚不清楚您想要什么-一个例子会有所帮助-但事情很简单:

string url = "http://" + ipAddress;

would quite possibly be enough. 可能就足够了。

EDIT: Okay, it sounds like you're trying to find the name for an IP address. 编辑:好的,听起来您正在尝试查找IP地址的名称 In some ways that's quite simple: 在某些方面,这很简单:

IPHostEntry entry = Dns.GetHostEntry("72.29.94.50");
Console.WriteLine(entry.HostName);

However, this doesn't print eggheadcafe.com. 但是,这不会打印eggheadcafe.com。 It prints something entirely different: 它打印出完全不同的内容:

72.29.94.50.static.dimenoc.com

That's entirely correct in terms of a reverse DNS lookup (run "nslookup 72.29.94.50" to see the same result)... but it's not what you were looking for. 就反向DNS查找而言,这是完全正确的(运行“ nslookup 72.29.94.50”以查看相同的结果)...但这不是您想要的。

The problem is that I believe this eggheadcafe.com is served by virtual hosting - although eggheadcafe.com is served on that IP address, so are other websites (at least potentially). 问题是,我相信此eggheadcafe.com由虚拟主机提供服务-尽管eggheadcafe.com 通过该IP地址提供的,其他网站也是如此(至少可能)。 When you visit eggheadcafe.com in your browser, it resolves to that IP address but also specifies the host name in an HTTP header. 当您在浏览器中访问eggheadcafe.com时,它将解析为该IP地址,但还会在HTTP标头中指定主机名。

Not sure what you want to achieve but guess you need to resolve a host name from IP. 不确定要实现的目标,但猜测您需要从IP解析主机名。

In this case you can use Dns.GetHostEntry method. 在这种情况下,可以使用Dns.GetHostEntry方法。

There are possibility of multiple website hosted on single IP. 有可能在单个IP上托管多个网站。 But you can get default using 但是您可以使用默认值

 IPHostEntry IpEntry = Dns.GetHostByAddress(ip); 
 return iphostentry.HostName.ToString();

Following might helpful to start with: 以下内容可能对您有所帮助:

http://www.c-sharpcorner.com/UploadFile/uchukamen/IPAddHostConverter12052005041212AM/IPAddHostConverter.aspx http://www.c-sharpcorner.com/UploadFile/uchukamen/IPAddHostConverter12052005041212AM/IPAddHostConverter.aspx

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

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