本文整理汇总了C#中System.Uri.GetParts方法的典型用法代码示例。如果您正苦于以下问题:C# Uri.GetParts方法的具体用法?C# Uri.GetParts怎么用?C# Uri.GetParts使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Uri
的用法示例。
在下文中一共展示了Uri.GetParts方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: FetchRequest
/*-------------- internal members -------------*/
//
internal void FetchRequest(Uri uri, WebRequest request)
{
_Request = request;
_Policy = request.CachePolicy;
_Response = null;
_ResponseCount = 0;
_ValidationStatus = CacheValidationStatus.DoNotUseCache;
_CacheFreshnessStatus = CacheFreshnessStatus.Undefined;
_CacheStream = null;
_CacheStreamOffset = 0L;
_CacheStreamLength = 0L;
if (!uri.Equals(_Uri))
{
// it's changed from previous call
_CacheKey = uri.GetParts(UriComponents.AbsoluteUri, UriFormat.Unescaped);
}
_Uri = uri;
}
示例2: IsBaseOfHelper
//
//
//
internal bool IsBaseOfHelper(Uri uriLink)
{
//TO
if (!IsAbsoluteUri || UserDrivenParsing)
return false;
if (!uriLink.IsAbsoluteUri)
{
//a relative uri could have quite tricky form, it's better to fix it now.
string newUriString = null;
UriFormatException e;
bool dontEscape = false;
uriLink = ResolveHelper(this, uriLink, ref newUriString, ref dontEscape, out e);
if (e != null)
return false;
if ((object)uriLink == null)
uriLink = CreateHelper(newUriString, dontEscape, UriKind.Absolute, ref e);
if (e != null)
return false;
}
if (Syntax.SchemeName != uriLink.Syntax.SchemeName)
return false;
// Canonicalize and test for substring match up to the last path slash
string me = GetParts(UriComponents.AbsoluteUri & ~UriComponents.Fragment, UriFormat.SafeUnescaped);
string she = uriLink.GetParts(UriComponents.AbsoluteUri & ~UriComponents.Fragment, UriFormat.SafeUnescaped);
unsafe
{
fixed (char* pMe = me)
{
fixed (char* pShe = she)
{
return UriHelper.TestForSubPath(pMe, (ushort)me.Length, pShe, (ushort)she.Length,
IsUncOrDosPath || uriLink.IsUncOrDosPath);
}
}
}
}
示例3: Compare
//
//
// This is for languages that do not support == != operators overloading
//
// Note that Uri.Equals will get an optimized path but is limited to true/fasle result only
//
public static int Compare(Uri uri1, Uri uri2, UriComponents partsToCompare, UriFormat compareFormat,
StringComparison comparisonType)
{
if ((object) uri1 == null)
{
if (uri2 == null)
return 0; // Equal
return -1; // null < non-null
}
if ((object) uri2 == null)
return 1; // non-null > null
// a relative uri is always less than an absolute one
if (!uri1.IsAbsoluteUri || !uri2.IsAbsoluteUri)
return uri1.IsAbsoluteUri? 1: uri2.IsAbsoluteUri? -1: string.Compare(uri1.OriginalString,
uri2.OriginalString, comparisonType);
return string.Compare(
uri1.GetParts(partsToCompare, compareFormat),
uri2.GetParts(partsToCompare, compareFormat),
comparisonType
);
}
示例4: ResolveHelper
internal static Uri ResolveHelper(Uri baseUri, Uri relativeUri, ref string newUriString, ref bool userEscaped, out UriFormatException e)
{
e = null;
string relativeStr = string.Empty;
if (relativeUri != null)
{
if (relativeUri.IsAbsoluteUri)
{
return relativeUri;
}
relativeStr = relativeUri.OriginalString;
userEscaped = relativeUri.UserEscaped;
}
else
{
relativeStr = string.Empty;
}
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;
}
if (((relativeStr[0] == '#') && !baseUri.IsImplicitFile) && baseUri.Syntax.InFact(UriSyntaxFlags.MayHaveFragment))
{
newUriString = baseUri.GetParts(UriComponents.HttpRequestUrl | UriComponents.UserInfo, UriFormat.UriEscaped) + relativeStr;
return null;
}
if (((relativeStr[0] == '?') && !baseUri.IsImplicitFile) && baseUri.Syntax.InFact(UriSyntaxFlags.MayHaveQuery))
{
newUriString = baseUri.GetParts(UriComponents.Path | UriComponents.SchemeAndServer | UriComponents.UserInfo, UriFormat.UriEscaped) + relativeStr;
return null;
}
if (((relativeStr.Length >= 3) && ((relativeStr[1] == ':') || (relativeStr[1] == '|'))) && (IsAsciiLetter(relativeStr[0]) && ((relativeStr[2] == '\\') || (relativeStr[2] == '/'))))
{
if (baseUri.IsImplicitFile)
{
newUriString = relativeStr;
return null;
}
if (baseUri.Syntax.InFact(UriSyntaxFlags.AllowDOSPath))
{
string str2;
if (baseUri.InFact(Flags.AuthorityFound))
{
str2 = baseUri.Syntax.InFact(UriSyntaxFlags.PathIsRooted) ? ":///" : "://";
}
else
{
str2 = baseUri.Syntax.InFact(UriSyntaxFlags.PathIsRooted) ? ":/" : ":";
}
newUriString = baseUri.Scheme + str2 + relativeStr;
return null;
}
}
ParsingError err = GetCombinedString(baseUri, relativeStr, userEscaped, ref newUriString);
if (err != ParsingError.None)
{
e = GetException(err);
return null;
}
if (newUriString == baseUri.m_String)
{
return baseUri;
}
return null;
}
示例5: ResolveHelper
//
// Resolves into either baseUri or relativeUri according to conditions OR if not possible it uses newUriString
// to return combined URI strings from both Uris
// otherwise if e != null on output the operation has failed
//
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;
}
示例6: IsBaseOfHelper
internal unsafe bool IsBaseOfHelper(Uri uriLink)
{
if (!this.IsAbsoluteUri || this.UserDrivenParsing)
{
return false;
}
if (!uriLink.IsAbsoluteUri)
{
UriFormatException exception;
string newUriString = null;
bool userEscaped = false;
uriLink = ResolveHelper(this, uriLink, ref newUriString, ref userEscaped, out exception);
if (exception != null)
{
return false;
}
if (uriLink == null)
{
uriLink = CreateHelper(newUriString, userEscaped, UriKind.Absolute, ref exception);
}
if (exception != null)
{
return false;
}
}
if (this.Syntax.SchemeName != uriLink.Syntax.SchemeName)
{
return false;
}
string parts = this.GetParts(UriComponents.HttpRequestUrl | UriComponents.UserInfo, UriFormat.SafeUnescaped);
string str3 = uriLink.GetParts(UriComponents.HttpRequestUrl | UriComponents.UserInfo, UriFormat.SafeUnescaped);
fixed (char* str4 = ((char*) parts))
{
char* pMe = str4;
fixed (char* str5 = ((char*) str3))
{
char* pShe = str5;
return TestForSubPath(pMe, (ushort) parts.Length, pShe, (ushort) str3.Length, this.IsUncOrDosPath || uriLink.IsUncOrDosPath);
}
}
}
示例7: MakeRelativeUri
public Uri MakeRelativeUri(Uri uri)
{
if (uri == null)
{
throw new ArgumentNullException("uri");
}
if (this.IsNotAbsoluteUri || uri.IsNotAbsoluteUri)
{
throw new InvalidOperationException(System.SR.GetString("net_uri_NotAbsolute"));
}
if ((!(this.Scheme == uri.Scheme) || !(this.Host == uri.Host)) || (this.Port != uri.Port))
{
return uri;
}
string absolutePath = uri.AbsolutePath;
string uriString = PathDifference(this.AbsolutePath, absolutePath, !this.IsUncOrDosPath);
if (CheckForColonInFirstPathSegment(uriString) && (!uri.IsDosPath || !absolutePath.Equals(uriString, StringComparison.Ordinal)))
{
uriString = "./" + uriString;
}
return new Uri(uriString + uri.GetParts(UriComponents.Fragment | UriComponents.Query, UriFormat.UriEscaped), UriKind.Relative);
}
示例8: Compare
public static int Compare(Uri uri1, Uri uri2, UriComponents partsToCompare, UriFormat compareFormat, StringComparison comparisonType)
{
if (uri1 == null)
{
if (uri2 == null)
{
return 0;
}
return -1;
}
if (uri2 == null)
{
return 1;
}
if (uri1.IsAbsoluteUri && uri2.IsAbsoluteUri)
{
return string.Compare(uri1.GetParts(partsToCompare, compareFormat), uri2.GetParts(partsToCompare, compareFormat), comparisonType);
}
if (uri1.IsAbsoluteUri)
{
return 1;
}
if (!uri2.IsAbsoluteUri)
{
return string.Compare(uri1.OriginalString, uri2.OriginalString, comparisonType);
}
return -1;
}
示例9: CombineUri
private static string CombineUri(Uri basePart, string relativePart, UriFormat uriFormat)
{
string parts;
int length;
char[] chArray;
char ch = relativePart[0];
if ((basePart.IsDosPath && ((ch == '/') || (ch == '\\'))) && ((relativePart.Length == 1) || ((relativePart[1] != '/') && (relativePart[1] != '\\'))))
{
int index = basePart.OriginalString.IndexOf(':');
if (!basePart.IsImplicitFile)
{
index = basePart.OriginalString.IndexOf(':', index + 1);
}
return (basePart.OriginalString.Substring(0, index + 1) + relativePart);
}
if (!StaticIsFile(basePart.Syntax) || ((ch != '\\') && (ch != '/')))
{
bool flag = basePart.Syntax.InFact(UriSyntaxFlags.ConvertPathSlashes);
parts = null;
if ((ch == '/') || ((ch == '\\') && flag))
{
if ((relativePart.Length >= 2) && (relativePart[1] == '/'))
{
return (basePart.Scheme + ':' + relativePart);
}
if (basePart.HostType == (Flags.HostNotParsed | Flags.IPv6HostType))
{
parts = string.Concat(new object[] { basePart.GetParts(UriComponents.UserInfo | UriComponents.Scheme, uriFormat), '[', basePart.DnsSafeHost, ']', basePart.GetParts(UriComponents.KeepDelimiter | UriComponents.Port, uriFormat) });
}
else
{
parts = basePart.GetParts(UriComponents.SchemeAndServer | UriComponents.UserInfo, uriFormat);
}
if (flag && (ch == '\\'))
{
relativePart = '/' + relativePart.Substring(1);
}
return (parts + relativePart);
}
parts = basePart.GetParts(UriComponents.KeepDelimiter | UriComponents.Path, basePart.IsImplicitFile ? UriFormat.Unescaped : uriFormat);
length = parts.Length;
chArray = new char[length + relativePart.Length];
if (length > 0)
{
parts.CopyTo(0, chArray, 0, length);
while (length > 0)
{
if (chArray[--length] == '/')
{
length++;
break;
}
}
}
}
else
{
if ((relativePart.Length >= 2) && ((relativePart[1] == '\\') || (relativePart[1] == '/')))
{
if (!basePart.IsImplicitFile)
{
return ("file:" + relativePart);
}
return relativePart;
}
if (!basePart.IsUnc)
{
return ("file://" + relativePart);
}
string str = basePart.GetParts(UriComponents.KeepDelimiter | UriComponents.Path, UriFormat.Unescaped);
for (int i = 1; i < str.Length; i++)
{
if (str[i] == '/')
{
str = str.Substring(0, i);
break;
}
}
if (basePart.IsImplicitFile)
{
return (@"\\" + basePart.GetParts(UriComponents.Host, UriFormat.Unescaped) + str + relativePart);
}
return ("file://" + basePart.GetParts(UriComponents.Host, uriFormat) + str + relativePart);
}
relativePart.CopyTo(0, chArray, length, relativePart.Length);
ch = basePart.Syntax.InFact(UriSyntaxFlags.MayHaveQuery) ? '?' : ((char) 0xffff);
char ch2 = (!basePart.IsImplicitFile && basePart.Syntax.InFact(UriSyntaxFlags.MayHaveFragment)) ? '#' : ((char) 0xffff);
string str3 = string.Empty;
if ((ch == 0xffff) && (ch2 == 0xffff))
{
length += relativePart.Length;
}
else
{
int startIndex = 0;
while (startIndex < relativePart.Length)
{
if ((chArray[length + startIndex] == ch) || (chArray[length + startIndex] == ch2))
{
break;
//.........这里部分代码省略.........
示例10: CombineUri
//
// CombineUri
//
// Given 2 URI strings, combine them into a single resultant URI string
//
// Inputs:
// <argument> basePart
// Base URI to combine with
//
// <argument> relativePart
// String expected to be relative URI
//
// Assumes:
// <basePart> is in canonic form
//
// Returns:
// Resulting combined URI string
//
private static string CombineUri(Uri basePart, string relativePart, UriFormat uriFormat)
{
//NB: relativePart is ensured as not empty by the caller
// Another assumption is that basePart is an AbsoluteUri
// This method was not optimized for efficiency
// Means a relative Uri ctor may be relatively slow plus it increases the footprint of the baseUri
char c1 = relativePart[0];
//check a special case for the base as DOS path and a rooted relative string
if (basePart.IsDosPath &&
(c1 == '/' || c1 == '\\') &&
(relativePart.Length == 1 || (relativePart[1] != '/' && relativePart[1] != '\\')))
{
// take relative part appended to the base string after the drive letter
int idx = basePart.OriginalString.IndexOf(':');
if (basePart.IsImplicitFile)
{
return basePart.OriginalString.Substring(0, idx + 1) + relativePart;
}
// The basePart has explicit scheme (could be not file:), take the DOS drive ':' position
idx = basePart.OriginalString.IndexOf(':', idx + 1);
return basePart.OriginalString.Substring(0, idx + 1) + relativePart;
}
// Check special case for Unc or absolute path in relativePart when base is FILE
if (StaticIsFile(basePart.Syntax))
{
if (c1 == '\\' || c1 == '/')
{
if (relativePart.Length >= 2 && (relativePart[1] == '\\' || relativePart[1] == '/'))
{
//Assuming relative is a Unc path and base is a file uri.
return basePart.IsImplicitFile ? relativePart : "file:" + relativePart;
}
// here we got an absolute path in relativePart,
// For compatibility with V1.0 parser we restrict the compression scope to Unc Share, i.e. \\host\share\
if (basePart.IsUnc)
{
string share = basePart.GetParts(UriComponents.Path | UriComponents.KeepDelimiter,
UriFormat.Unescaped);
for (int i = 1; i < share.Length; ++i)
{
if (share[i] == '/')
{
share = share.Substring(0, i);
break;
}
}
if (basePart.IsImplicitFile)
{
return @"\\"
+ basePart.GetParts(UriComponents.Host, UriFormat.Unescaped)
+ share
+ relativePart;
}
return "file://"
+ basePart.GetParts(UriComponents.Host, uriFormat)
+ share
+ relativePart;
}
// It's not obvious but we've checked (for this relativePart format) that baseUti is nor UNC nor DOS path
//
// Means base is a Unix style path and, btw, IsImplicitFile cannot be the case either
return "file://" + relativePart;
}
}
// If we are here we did not recognize absolute DOS/UNC path for a file: base uri
// Note that DOS path may still happen in the relativePart and if so it may override the base uri scheme.
bool convBackSlashes = basePart.Syntax.InFact(UriSyntaxFlags.ConvertPathSlashes);
string left = null;
// check for network or local absolute path
if (c1 == '/' || (c1 == '\\' && convBackSlashes))
{
if (relativePart.Length >= 2 && relativePart[1] == '/')
{
//.........这里部分代码省略.........
示例11: MakeRelativeUri
public Uri MakeRelativeUri(Uri uri)
{
if ((object)uri == null)
throw new ArgumentNullException(nameof(uri));
if (IsNotAbsoluteUri || uri.IsNotAbsoluteUri)
throw new InvalidOperationException(SR.net_uri_NotAbsolute);
// Note that the UserInfo part is ignored when computing a relative Uri.
if ((Scheme == uri.Scheme) && (Host == uri.Host) && (Port == uri.Port))
{
string otherPath = uri.AbsolutePath;
// Relative Path
string relativeUriString = PathDifference(AbsolutePath, otherPath, !IsUncOrDosPath);
// Relative Uri's cannot have a colon ':' in the first path segment (RFC 3986, Section 4.2)
if (CheckForColonInFirstPathSegment(relativeUriString)
// Except for full implicit dos file paths
&& !(uri.IsDosPath && otherPath.Equals(relativeUriString, StringComparison.Ordinal)))
relativeUriString = "./" + relativeUriString;
// Query & Fragment
relativeUriString += uri.GetParts(UriComponents.Query | UriComponents.Fragment, UriFormat.UriEscaped);
return new Uri(relativeUriString, UriKind.Relative);
}
return uri;
}
示例12: GetCanonicalKey
//
// Private stuff: We want to serialize on updates on one thread
//
private static string GetCanonicalKey(string key)
{
if( key == null ) {
throw new ArgumentNullException("key");
}
try {
Uri uri = new Uri(key);
key = uri.GetParts(UriComponents.Scheme | UriComponents.Host | UriComponents.Port | UriComponents.Path, UriFormat.SafeUnescaped);
#if !DISABLE_CAS_USE
new WebPermission(NetworkAccess.Connect, new Uri(key)).Demand();
#endif
}
catch(UriFormatException e) {
throw new ArgumentException(SR.GetString(SR.net_mustbeuri, "key"), "key", e);
}
return key;
}