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);
}
}
}
}
分享到:
相关推荐
string modifiedUrl = builder.Uri.ToString(); ``` 如果你不想依赖`HttpUtility.ParseQueryString`,可以编写自己的方法来解析URL参数。以下是一个简单的实现: ```csharp public static Dictionary, string> ...
string url = builder.ToString(); // 输出: http://example.com/mypage?key=value ``` 4. **处理查询参数**: 当需要操作URL查询字符串时,`HttpUtility.ParseQueryString`方法很有用。它能将查询字符串转换为`...
client.BaseAddress = new Uri("http://example.com"); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); ``` 2. **发送GET请求**: GET请求用于从服务器...
builder.AppendFormat("{0:X2}", num); } stream.Close(); return builder.ToString(); } ``` 这段代码做了以下几件事: 1. 创建一个`DESCryptoServiceProvider`实例,这将是我们的加密服务提供者。 2. 设置密钥...
在IT领域,Windows Forms(简称Forms)是一种.NET Framework下的用户界面框架,用于构建桌面应用程序。而Silverlight是微软推出的一种富互联网应用(RIA)技术,主要用于网页开发,它结合了.NET Framework的部分功能...
if (Uri.TryCreate(builder.Uri, relativeUrl, out absoluteUrl)) { Console.WriteLine(absoluteUrl.ToString()); } } ``` 5. **处理编码问题**:HTML中的URL可能经过了编码,比如使用了`%`符号表示十六进制值...
CropOptions options = new CropOptions.Builder() .setAspectRatio(1, 1) // 设置裁剪比例为1:1 .setFixAspectRatio(true) // 固定裁剪比例 .build(); cropImageView.setCropMenuWindowParams(options); // ...
var uri = builder.Uri; StreamResourceInfo streamInfo = Application.GetResourceStream(uri); ``` 4. **使用相对路径**:如果资源文件和执行程序在同一目录或子目录下,可以使用相对路径。但要注意,相对...
var callDialog = new AlertDialog.Builder(this); callDialog.SetMessage($"Call {translatedNumber}?"); // 提示信息包含要拨打的电话号码 callDialog.SetNeutralButton("Call", delegate { // 创建一个...
网址构建器UrlBuilder是... SetScheme ( Uri . UriSchemeHttps );url . AddPathSegment ( " test " );url . SetQueryParam ( " nid " , 123 );Assert . AreEqual ( " https://www.shoutem.com/app/test?nid=123 " , url
Document document = builder.build("TestXml.xml"); // 进行后续处理 } catch (Exception e) { e.printStackTrace(); } } } ``` SAX解析器则逐行读取XML,适用于大文件,避免内存消耗过大。例如,使用Java...
可以通过`.AddCheck().WithTag("tagname")`添加标签,然后在路由配置中筛选特定标签的检查,例如`app.UseHealthChecks("/health", builder => builder.WithTag("database"))`。 6. **结果格式化** ASP.NET Health...
在`Startup.cs`文件中,我们可以创建一个静态方法`GetEdmModel()`,在这个方法里,我们使用`ODataConventionModelBuilder`来构建模型,例如:`builder.EntitySet("Books")`和`builder.EntitySet("Presses")`分别表示...
- **优点**:提供了强大的图形渲染能力,支持矢量图形,拥有成熟的开发工具如Adobe Flash Builder。 - **特点**:使用MXML和ActionScript进行开发,支持创建复杂的数据驱动的应用程序。 - **适用范围**:适用于创建...