简体   繁体   English

确定2个URL是否相同的最安全的方法是什么?

[英]What's the safest way to determine if 2 URLs are the same?

If I have URL A say http://www.example.com/ and another, say http://www.example.com . 如果我有URL A说http://www.example.com/和另一个,请说http://www.example.com What would be the safest way to determine if both is the same, without querying for the web page and do a diff? 什么是最安全的方法来确定两者是否相同,而不查询网页并进行差异?

EXAMPLES: 例子:

  1. http://www.example.com/ VS http://www.example.com (Mentioned above) http://www.example.com/ VS http://www.example.com (上文提到)
  2. http://www.example.com/aa/../ VS http://www.example.com http://www.example.com/aa/../ VS http://www.example.com

EDIT: Clarifications: Just want to know if the URLs are the same in the context of being equivalent according to the RFC 1738 standard. 编辑:澄清:只是想根据RFC 1738标准知道URL在等效的上下文中是否相同。

In .Net, you can use the System.Uri class. 在.Net中,您可以使用System.Uri类。

let u1 = new Uri(" http://www.google.com/ ");; 让u1 =新的Uri(“ http://www.google.com/ ”);;

val u1 : Uri = http://www.google.com/ val u1:Uri = http://www.google.com/

let u2 = new Uri(" http://www.google.com ");; 让u2 =新的Uri(“ http://www.google.com ”);;

val u2 : Uri = http://www.google.com/ val u2:Uri = http://www.google.com/

u1.Equals(u2);; u1.Equals(U2);;

val it : bool = true val it:bool = true

For more fine-grained comparison, you can use the Uri.Compare method. 要进行更细粒度的比较,可以使用Uri.Compare方法。 There are also static methods to deal with various forms of escaping and encoding of characters in the Uri string, which will no doubt prove useful when dealing with the subject thoroughly. 还有静态方法来处理Uri字符串中各种形式的字符转义和编码,这无疑在彻底处理主题时非常有用。

There is very little you can do without requesting the URL. 没有请求URL,你几乎无能为力。 But you can define several heuristics: 但您可以定义几个启发式方法:

  1. Remove trailing slashes 删除尾部斜杠
  2. Consider .htm and .html the same 考虑.htm.html是一样的
  3. Assume /base/ and /base/index.html are the same 假设/base//base/ /base/index.html是相同的
  4. Remove query string parameters (maybe, maybe not, depends on your needs) 删除查询字符串参数(可能,可能不是,取决于您的需要)
  5. Consider url.com and www.url.com the same. 考虑url.comwww.url.com相同。

It is all very dependent on what exactly you mean by URLs which are the "same". 这完全取决于“相同”的URL究竟是什么意思。

For the benefit of those of you who don't know F#, here's a quick and dirty but complete C# console app that demonstrates the use of the Uri class to tell if two URLs are the same. 为了让那些不了解F#的人受益,这里有一个快速而肮脏但完整的C#控制台应用程序,它演示了如何使用Uri类来判断两个URL是否相同。 When you run this code, you should see two lines: "true", followed by "false": 运行此代码时,您应该看到两行:“true”,后跟“false”:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(IsSameUrl("http://stackoverflow.com/", "http://stackoverflow.com").ToString());
            Console.WriteLine(IsSameUrl("http://stackoverflow.com/", "http://codinghorror.com").ToString());
            Console.ReadKey();
        }

        static bool IsSameUrl(string url1, string url2)
        {
            Uri u1 = new Uri(url1);
            Uri u2 = new Uri(url2);
            return u1.Equals(u2);
        }
    }
}

There are few things to add to Yuval A answers: Yuval A答案中添加的内容很少:

  • www.google.com and http://www.google.com may points to the same target www.google.com和http://www.google.com可能指向同一目标
  • www.google.com and google.com points to the same page (but it is implemented by redirecting) www.google.com和google.com指向同一页面(但通过重定向实现)
  • Url may be encoded (see HttpUtility.UrlEncode / Decode methods) 可能会对Url进行编码(请参阅HttpUtility.UrlEncode / Decode方法)

暂无
暂无

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

相关问题 启动不受信任的URL的最安全方法是什么? - What's the safest way to launch an untrusted URL? 防止程序多个实例最安全的方法是什么? - What is the safest way to prevent multiple instances of a program? 清除容器的Controls属性并确保正确处置控件的最安全方法是什么? - What is the safest way to clear a container's Controls property and make sure the controls are properly disposed? 在运行时调整WinForms应用程序主窗体的起始大小和位置的最安全方法是什么? - What's the safest way to adjust my WinForms application main form starting size and position at run time? 确定NamedDataSlot是否存在的最佳方法是什么 - What's the best way to determine if NamedDataSlot exists 确定应用程序位置的正确方法是什么? - What is the proper way to determine an application's location? 在WPF中将数据绑定到另一个xaml文件中的元素的最安全方法是什么 - What is the safest way of databinding to an element in another xaml file in WPF 从控制器发送数据到视图的最安全和最干净的方法是什么? - What is the safest and cleanest way to send data from controller to view? 使用 HttpClient 发送大量请求的最快和最安全的方法是什么? - What is the fastest and safest way to send large number of requests with HttpClient? 通过代码将文件保存到备份服务器的最简单和/或最安全的方式? - easiest and/or safest way(s) to save a file to a backup server by code and how?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM