您好,登錄后才能下訂單哦!
在現在的項目中,使用到uri的部分較多。
uri的解析方法:
public static Uri ReturnUri(this string val) { if (string.IsNullOrEmpty(val)) { throw new ArgumentNullException(val); } try { UriBuilder uri; if (val.StartsWith("http://", StringComparison.Ordinal)) { uri = new UriBuilder(val); } else if (val.StartsWith("https://", StringComparison.Ordinal)) { uri = new UriBuilder(val); } else if (val.Contains(":")) { var split = val.Split(':', '/'); int port; int.TryParse(split[1], out port); uri = split.Length == 3 ? new UriBuilder(Uri.UriSchemeHttp, split[0], port, "/" + split[2]) : new UriBuilder(Uri.UriSchemeHttp, split[0], port); } else { uri = new UriBuilder(Uri.UriSchemeHttp, val); } return uri.Uri; } catch (Exception exception) { throw new Exception(exception.Message, exception); } }
2.uri解析的重載:
public static Uri ReturnUri(this string val, int port, bool ssl = default(bool)) { if (string.IsNullOrEmpty(val)) { throw new ArgumentNullException(val); } try { UriBuilder uri; if (val.StartsWith("http://", StringComparison.Ordinal)) { var split = val.Split('/'); uri = split.Length >= 4 ? new UriBuilder(Uri.UriSchemeHttp, split[2], port, "/" + split[3]) : new UriBuilder(new Uri($"{val}:{port}")); } else if (val.StartsWith("https://", StringComparison.Ordinal)) { var split = val.Split('/'); uri = split.Length >= 4 ? new UriBuilder(Uri.UriSchemeHttps, split[2], port, "/" + split[3]) : new UriBuilder(Uri.UriSchemeHttps, split[2], port); } else if(ssl) { uri = new UriBuilder(Uri.UriSchemeHttps, val, port); } else { uri = new UriBuilder(Uri.UriSchemeHttp, val, port); } return uri.Uri; } catch (Exception exception) { throw new Exception(exception.Message, exception); } }
public static Uri ReturnUriWithSubDir(this string val, int port, bool ssl, string subDir) { var uriBuilder = new UriBuilder(val); if (ssl) { uriBuilder.Scheme = Uri.UriSchemeHttps; } if (!string.IsNullOrEmpty(subDir)) { uriBuilder.Path = subDir; } uriBuilder.Port = port; return uriBuilder.Uri; }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。