C# 아래의 Uri.Builder
5636 단어 C#Uri.Builder
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
namespace Uri
{
class Builder
{
private string scheme = "http";
private string host = "";
private StringBuilder path;
private Boolean endwithSlash = false;
private IDictionary<string, string> param = null;
public Builder()
{
scheme = "http";
host = "";
path = new StringBuilder("");
endwithSlash = false;
param = new Dictionary<string, string>();
}
public Builder SetScheme(string scheme)
{
this.scheme = scheme;
return this;
}
public Builder SetHost(string host)
{
this.host = host;
return this;
}
public Builder Path(string path)
{
return AppendPath(ParsePath(path));
}
public Builder EncodePath(string path)
{
return AppendEncodePath(ParsePath(path));
}
public Builder AppendPath(string path)
{
if (path.Length > 0)
{
// start with '/'
if (!endwithSlash && !path.StartsWith("/"))
this.path.Append("/");
this.path.Append(path);
if (path.EndsWith("/"))
endwithSlash = true;
else
endwithSlash = false;
}
return this;
}
public Builder AppendEncodePath(string path)
{
return AppendPath(Encode(path));
}
private string ParsePath(string path)
{
int posScheme = path.IndexOf("://");
if (posScheme > 0)
{
// get scheme
scheme = path.Substring(0, posScheme);
path = path.Substring(posScheme + 3);
}
int posHost = path.IndexOf("/");
if (posHost == -1)
{
// not found '/'
host = path;
path = "";
}
else
{
host = path.Substring(0, posHost);
path = path.Substring(posHost + 1);
}
return path;
}
public Builder AppendParameter(string key, string value)
{
param.Add(key, value);
return this;
}
public Builder AppendEncodeParameter(string key, string value)
{
return AppendParameter(key, Encode(value));
}
public string ToString()
{
// generate URL string
StringBuilder url = new StringBuilder("");
// scheme
url.Append(scheme).Append("://");
// host
url.Append(host);
// path
if (path.Length > 0)
url.Append(path);
// param
if (param.Count > 0)
{
string pre = "?";
foreach (KeyValuePair<string, string> p in param)
{
url.Append(pre);
if (p.Value != null)
url.AppendFormat("{0}={1}", p.Key, p.Value);
else
url.Append(p.Key);
pre = "&";
}
}
return url.ToString();
}
private string Encode(string value)
{
return HttpUtility.UrlEncode(value);
}
public Builder Parse(string url)
{
// seperate path and parameters
string[] div = url.Split(new string[] { "?" }, 2, StringSplitOptions.None);
Path(div[0]);
if (div.Length == 2)
ParseParameters(div[1]);
return this;
}
private void ParseParameters(string param)
{
if (param.Length == 0)
return;
string[] para = param.Split(new string[] { "&" }, StringSplitOptions.None);
foreach (string p in para)
{
string[] val = p.Split(new string[] { "=" }, 2, StringSplitOptions.None);
if (val.Length == 2)
AppendParameter(val[0], val[1]);
else
AppendParameter(val[0], null);
}
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
WebView2를 Visual Studio 2017 Express에서 사용할 수 있을 때까지Evergreen .Net Framework SDK 4.8 VisualStudio2017에서 NuGet을 사용하기 때문에 패키지 관리 방법을 packages.config 대신 PackageReference를 사용해야...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.