C#中的UriBuilder 可以很方便的帮助我们拼接url,测试用例如下
using NUnit.Framework;
using System;
public class UriTest
{
[Test]
public void UriSimplePasses()
{
UriBuilder uriBuilder = new UriBuilder();
uriBuilder.Scheme = "https";
uriBuilder.Host = "github.com";
uriBuilder.Path = "amosbake";
Uri uri = uriBuilder.Uri;
Assert.AreEqual("https://github.com/amosbake", uri.ToString());
}
[Test]
public void UriQueryPasses()
{
UriBuilder uriBuilder = new UriBuilder();
uriBuilder.Scheme = "https";
uriBuilder.Host = "api.github.com";
uriBuilder.Path = "repos/vmg/redcarpet/issues";
uriBuilder.Query = "state=closed";
Uri uri = uriBuilder.Uri;
Assert.AreEqual("https://api.github.com/repos/vmg/redcarpet/issues?state=closed", uri.ToString());
}
[Test]
public void UriFilePasses()
{
UriBuilder uriBuilder = new UriBuilder();
uriBuilder.Scheme = "c";
uriBuilder.Host = @"temp";
uriBuilder.Path = "log.txt";
Uri uri = uriBuilder.Uri;
Assert.AreEqual("file:///c:/temp/log.txt", uri.ToString());
}
}
网友评论