Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在.NET中解析URI 短版_C#_Uri - Fatal编程技术网

C# 在.NET中解析URI 短版

C# 在.NET中解析URI 短版,c#,uri,C#,Uri,NET中是否有可以解析Uri的类 背景 Windows Search服务通过使用URI注册要爬网的内容。使用可以枚举各种根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-188441

NET中是否有可以解析Uri的类

背景 Windows Search服务通过使用URI注册要爬网的内容。使用可以枚举各种根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():

NET中是否有可以解析Uri的类

本机Win32函数能够正确处理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/
没有帮助()

Uri和Url之间的差异?

URL是URI的子集

  • 乌里告诉你一件事
  • Url告诉你在哪里可以买到东西
例如:

  • URI
    isbn:1631971727
    (标识一个事物)
    • URL
      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
  • 密码
  • 主机
    example.com
  • 端口
    8042
  • 路径
    /look/over/there
  • 查询
    ?名称=雪貂
  • 片段
    鼻子

您在堆栈跟踪中看到,
CreateThis()
调用的方法
resolveheloper()
将其标识为绝对uri,因此引发异常

将您的uri更改为:

mapi://{S-1-5-21-397955417-62688126-188441444-1010}/Outlook2003/Inbox/
致:

**

来自参考源.NET Framework 4.7.2:

**


System.Uri
可以解析
file:///C:/
file:///D:/
没有问题。该类不支持其他方案。Windows Search SDK中是否有用于解析这些内容的内容?您正在搜索(或可能需要编写)的库的类型可能取决于您在解析它们之后要执行的操作。我当然不能事先知道.NET解析器无法处理的每个Uri,以便诱使.NET解析器不崩溃。另外,将其更改为
//
不会导致失败:但它也会返回不正确的结果(主机丢失,查询丢失)
mapi://{S-1-5-21-397955417-62688126-188441444-1010}/Outlook2003/Inbox/
mapi:////{S-1-5-21-397955417-62688126-188441444-1010}/Outlook2003/Inbox/
internal static Uri ResolveHelper(Uri baseUri, Uri relativeUri, ref string newUriString, ref bool userEscaped, 
            out UriFormatException e)
        {
            Debug.Assert(!baseUri.IsNotAbsoluteUri && !baseUri.UserDrivenParsing, "Uri::ResolveHelper()|baseUri is not Absolute or is controlled by User Parser.");

            e = null;
            string relativeStr = string.Empty;

            if ((object)relativeUri != null)
            {
                if (relativeUri.IsAbsoluteUri)
                    return relativeUri;

                relativeStr = relativeUri.OriginalString;
                userEscaped = relativeUri.UserEscaped;
            }
            else
                relativeStr = string.Empty;

            // Here we can assert that passed "relativeUri" is indeed a relative one

            if (relativeStr.Length > 0 && (IsLWS(relativeStr[0]) || IsLWS(relativeStr[relativeStr.Length - 1])))
                relativeStr = relativeStr.Trim(_WSchars);

            if (relativeStr.Length == 0)
            {
                newUriString = baseUri.GetParts(UriComponents.AbsoluteUri, 
                    baseUri.UserEscaped ? UriFormat.UriEscaped : UriFormat.SafeUnescaped);
                return null;
            }

            // Check for a simple fragment in relative part
            if (relativeStr[0] == '#' && !baseUri.IsImplicitFile && baseUri.Syntax.InFact(UriSyntaxFlags.MayHaveFragment))
            {
                newUriString = baseUri.GetParts(UriComponents.AbsoluteUri & ~UriComponents.Fragment, 
                    UriFormat.UriEscaped) + relativeStr;
                return null;
            }

            // Check for a simple query in relative part
            if (relativeStr[0] == '?' && !baseUri.IsImplicitFile && baseUri.Syntax.InFact(UriSyntaxFlags.MayHaveQuery))
            {
                newUriString = baseUri.GetParts(UriComponents.AbsoluteUri & ~UriComponents.Query & ~UriComponents.Fragment, 
                    UriFormat.UriEscaped) + relativeStr;
                return null;
            }

            // Check on the DOS path in the relative Uri (a special case)
            if (relativeStr.Length >= 3
                && (relativeStr[1] == ':' || relativeStr[1] == '|')
                && IsAsciiLetter(relativeStr[0])
                && (relativeStr[2] == '\\' || relativeStr[2] == '/'))
            {

                if (baseUri.IsImplicitFile)
                {
                    // It could have file:/// prepended to the result but we want to keep it as *Implicit* File Uri
                    newUriString = relativeStr;
                    return null;
                }
                else if (baseUri.Syntax.InFact(UriSyntaxFlags.AllowDOSPath))
                {
                    // The scheme is not changed just the path gets replaced
                    string prefix;
                    if (baseUri.InFact(Flags.AuthorityFound))
                        prefix = baseUri.Syntax.InFact(UriSyntaxFlags.PathIsRooted) ? ":///" : "://";
                    else
                        prefix = baseUri.Syntax.InFact(UriSyntaxFlags.PathIsRooted) ? ":/" : ":";

                    newUriString = baseUri.Scheme + prefix + relativeStr;
                    return null;
                }
                // If we are here then input like "http://host/path/" + "C:\x" will produce the result  http://host/path/c:/x
            }


            ParsingError err = GetCombinedString(baseUri, relativeStr, userEscaped, ref newUriString);

            if (err != ParsingError.None)
            {
                e = GetException(err);
                return null;
            }

            if ((object)newUriString == (object)baseUri.m_String)
                return baseUri;

            return null;
        }