如何检查两个不同的 URL 是否指向同一资源?

发布于 2024-09-07 04:00:49 字数 929 浏览 3 评论 0 原文

假设我有一个网站托管在名为“linux”的计算机上,它属于两个不同网络的一部分,地址分别为 192.168.1.1 和 10.1.1.1。当在“linux”上本地工作时,我可以通过以下 URL 访问此网站:

http://192.168.1.1/ http://10.1.1.1/ http://linux/ http://localhost/ http://127.0.0.1/

在网络“10.1.1.0/24”上的另一台计算机上工作我可以使用以下:

http://10.1.1.1/ http://linux/

但在“192.168.1.0/24”上我只能使用:

http://192.168.1.1/ http://linux/

我正在开发一个比较 URL 的应用程序,在此应用程序的上下文中,如果两个 URL 指向相同的资源,则它们相等。

有没有一种快速方法可以使用 C# 中的 URI 类进行这种比较?

Lets say I have a web site hosted on a computer named "linux" and it is part of two different networks with the addresses 192.168.1.1 and 10.1.1.1. When working locally on "linux", I can access this web site through the following URLs :

http://192.168.1.1/
http://10.1.1.1/
http://linux/
http://localhost/
http://127.0.0.1/

Working on another machine on the network "10.1.1.0/24" I can use the following :

http://10.1.1.1/
http://linux/

But on "192.168.1.0/24" I can only use :

http://192.168.1.1/
http://linux/

I'm developing an application that compares URLs, and on this application's context two URLs are equal if they point to the same resource.

Is there a quick way of doing that kind of comparison using the URI class in C# ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

〆凄凉。 2024-09-14 04:00:49

URI 类无法帮助你;它只能确定两个 URI 是否是相同的 URI,并且只有当它们具有相同的服务器名称/IP 地址时,它们才是相同的 URI。

最好的选择是从两个 URL 下载资源,并通过检查是否相等并假设两个资源是相同的资源(如果它们是相同的位序列)来查看它是否是相同的资源。

这对我来说不是一个选择,性能成本可能太大。我希望能够匹配 URI 的“主机地址”部分,然后剩下要比较的就是相对路径和端口... – Thiado de Arruda

不幸的是,里面没有任何内容框架可以开箱即用地执行您想要的操作,因为没有可靠的方法将不同的主机名映射到同一主机,从而映射到相同的资源。

您必须对主机进行某种映射到这些主机的各种别名,并自己查询它们。 URI.Host 属性将为您获取相关 URI 的主机名,但您必须自己翻译它。框架中的任何内容都无法为您做这件事。

There's no way the URI class will help you; it can only determine if two URIs are the same URI, and they're only the same URI if they have the same server name / IP address.

The best option is to download the resource from both URLs and see if it is the same resource by checking for equality and assuming that two resources are the same resource if they're the same sequence of bits.

That isn't an option for me, the perfomance cost could be too great. I was hoping for something that could match the 'host address' part of the URI's, then all that was left to compare would be the relative path and port... – Thiado de Arruda

Unfortunately, there is nothing in the framework that will do what you want out of the box, because there's no reliable way of mapping different host names to the same host and therefore the same resource.

You are going to have to have some sort of mapping of hosts to the various aliases of those hosts and query for them yourself. The URI.Host property will get the host name of the URI in question for you, but you'll have to translate it yourself. Nothing in the framework is going to be able to do it for you.

静若繁花 2024-09-14 04:00:49

您也许可以使用 python 使用 beautiful soup 来完成此操作(抓取页面并检查一些元素是否相同),但是它会比较慢,因为您需要提取实际的 Web 数据。

You may be able to do this with beautiful soup using python (scrape the page and check if a few elements are the same) however it will be slower since you'll need to pull the actual web data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文