【发布时间】:2018-06-18 17:09:11
【问题描述】:
短版
.NET 中有可以解析 Uri 的类吗?
背景
Windows 搜索服务通过使用 URI 注册内容以进行爬网。使用 ISearchCrawlScopeManager 您可以枚举各种根 uri:
csc://{S-1-5-21-397955417-62688126-188441444-1010}/defaultroot://{S-1-5-21-397955417-62688126-188441444-1010}/file:///C:\file:///D:\iehistory://{S-1-5-21-397955417-62688126-188441444-1010}/mapi://{S-1-5-21-397955417-62688126-188441444-1010}/Outlook2003/Inbox/winrt://{S-1-5-21-397955417-62688126-188441444-1010}/
不幸的是 .NET Uri class 无法解析这些 Uri (dotNetFiddle):
Run-time exception (line 8): Invalid URI: The hostname could not be parsed.
Stack Trace:
[System.UriFormatException: Invalid URI: The hostname could not be parsed.]
at System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind)
.NET 中有可以解析 Uri 的类吗?
本机 Win32 函数 InternetCrackUrl 能够正确处理 Uri:
URL_COMPONENTS components;
components.dwStructSize = sizeof(URL_COMPONENTS );
components.dwSchemeLength = DWORD(-1);
components.dwHostNameLength = DWORD(-1);
components.dwUserNameLength = DWORD(-1);
components.dwPasswordLength = DWORD(-1);
components.dwUrlPathLength = DWORD(-1);
components.dwExtraInfoLength = DWORD(-1);
InternetCrackUrl(url, Length(url), 0, ref components);
mapi://{S-1-5-21-397955417-62688126-188441444-1010}/Outlook2003/Inbox/
\__/ \__________________________________________/\_________________/
| | _
Scheme HostName UrlPath
Scheme: "mapi"
HostName: "{S-1-5-21-397955417-62688126-188441444-1010}"
UrlPath: "/Outlook2003/Inbox/"
奖金聊天
将 Uri 转义应用于 uri:
-
之前:
mapi://{S-1-5-21-397955417-62688126-188441444-1010}/Outlook2003/Inbox/ -
之后:
mapi://%7BS-1-5-21-397955417-62688126-188441444-1010%7D/Outlook2003/Inbox/
没有帮助 (dotNetFiddle)。
Uri 和 Url 的区别?
Urls 是 Uris 的子集
- Uri 告诉你一件事
- 网址告诉您在哪里可以买到东西
例如:
-
URI:
isbn:1631971727(标识事物)-
网址:
isbn://amazon.com/1631971727(从哪里得到东西)
-
网址:
网址
URL 的细分是:
foo://iboyd:Trubador@example.com:8042/look/over/there?name=ferret#nose
\_/ \___/ \______/ \_________/ \__/\______________/\__________/ \__/
| | | | | | | |
scheme username password host port path query fragment
-
方案:
foo -
用户名:
iboyd - 密码:Trubador
-
主持人:
example.com -
端口:
8042 -
路径:
/look/over/there -
查询:
?name=ferret -
片段:
nose
【问题讨论】:
-
System.Uri可以毫无问题地解析file:///C:/和file:///D:/。该类不支持其他方案。 Windows Search SDK 中有什么东西可以解析这些吗?您正在搜索(或可能需要编写)的库类型可能取决于您在解析它们后想要做什么。