C#下的Uri.Builder

本文详细介绍了HTTP URL的构建和解析过程,包括设置方案、主机、路径、参数等关键步骤,以及如何使用编码来处理特殊字符。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


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);
}
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值