URI

Uri和UriBuilder是System命名空间下的两个类。UriBuilder可以通过给定的字符串,从而构建一个URI,而Uri类可以分析,组合和比较URI。

1.创建Uri类

Uri uri = new Uri("http://www.microsoft.com/somefolder/somefile.htm?order=true");

2.分析Uri取得一些属性

string query = uri.Query;// ?order=true 得到参数部分
string absPath = uri.AbsolutePath; //   /somefoler/somefile.htm 得到路径部分
string scheme = uri.Scheme; // http 得到协议
int port = uri.Port ; // 80 访问端口
string host = uri.Host;    // www.microsoft.com
bool isDefaultPort = uri.IsDefaultPort;

UriBuidler

UriBuilder builder = new UriBuilder("http","www.microsoft.com",80,"somefolder/somefile.htm");

//通过builder构建uri
Uri completeUri = builder.Uri;

也可以单独给每个属性部分赋值

builder.Scheme = "http";
builder.Host="www.microsoft.com";
builder.Port = 80;
builder.Path="somefolder/somefile.htm";

results matching ""

    No results matching ""