创建Uri 类的实例,并用它来创建WebRequest 实例
下面的示例创建Uri 类的实例,并用它来创建WebRequest 实例。
Uri siteUri = new Uri("http://www.ajaxcn.net/");
WebRequest wr = WebRequest.Create(siteUri);
URI 是 Intranet 或 Internet 上可由应用程序使用的资源的一种简洁表示形式。Uri 类定义了属性和方法来处理 URI,包括分析、比较和组合。Uri 类属性是只读的;若要创建可修改的对象,请使用 UriBuilder 类。
http://msdn.microsoft.com/zh-cn/library/system.uri(VS.85).aspx一个例子如下:
using System;
using System.Net;
using System.Text;
namespace WebTest
{
class Program
{
static void Main(string[] args)
{
ParsingUri();
}
#region "3 Uri解析"
public static void ParsingUri()
{
Program.ParseUri(new Uri("http://user:password@localhost:8080/www.abc.com/home%20page.htm?item=1233#stuff"));
}
public static void ParseUri(Uri uri)
{
try
{
// System.Net.Uri类解析
// new Uri("http://user:password@localhost:8080/www.abc.com/home%20page.htm?item=1233#stuff")
//查看一些信息...
StringBuilder uriParts = new StringBuilder();
uriParts.AppendFormat("AbsoluteURI: {0}{1}",
uri.AbsoluteUri, Environment.NewLine);//uri.AbsoluteUri = "http://user:password@localhost:8080/www.abc.com/home%20page.htm?item=1233#stuff"
uriParts.AppendFormat("AbsolutePath: {0}{1}",
uri.AbsolutePath, Environment.NewLine);//uri.AbsolutePath = "/www.abc.com/home%20page.htm"
uriParts.AppendFormat("Scheme: {0}{1}",
uri.Scheme, Environment.NewLine);//表头 uri.Scheme = "http"
uriParts.AppendFormat("UserInfo: {0}{1}",
uri.UserInfo, Environment.NewLine);//uri.UserInfo = "user:password"
uriParts.AppendFormat("Authority: {0}{1}",
uri.Authority, Environment.NewLine);//uri.Authority = "localhost:8080"
uriParts.AppendFormat("DnsSafeHost: {0}{1}",
uri.DnsSafeHost, Environment.NewLine);//uri.DnsSafeHost = "localhost"
uriParts.AppendFormat("Host: {0}{1}",
uri.Host, Environment.NewLine);//uri.Host = "localhost"
uriParts.AppendFormat("HostNameType: {0}{1}",
uri.HostNameType.ToString(), Environment.NewLine);//uri.HostNameType = Dns
uriParts.AppendFormat("Port: {0}{1}", uri.Port, Environment.NewLine);//uri.Port = 8080
uriParts.AppendFormat("Path: {0}{1}", uri.LocalPath, Environment.NewLine);//uri.LocalPath = "/www.abc.com/home page.htm"
uriParts.AppendFormat("QueryString: {0}{1}", uri.Query, Environment.NewLine);//uri.Query = "?item=1233"
uriParts.AppendFormat("Path and QueryString: {0}{1}",
uri.PathAndQuery, Environment.NewLine);//uri.PathAndQuery = "/www.abc.com/home%20page.htm?item=1233"
uriParts.AppendFormat("Fragment: {0}{1}", uri.Fragment, Environment.NewLine);//锚点 uri.Fragment = "#stuff"
uriParts.AppendFormat("Original String: {0}{1}",
uri.OriginalString, Environment.NewLine);//uri.OriginalString = "http://user:password@localhost:8080/www.abc.com/home%20page.htm?item=1233#stuff"
uriParts.AppendFormat("Segments: {0}", Environment.NewLine);
for (int i = 0; i < uri.Segments.Length; i++)
uriParts.AppendFormat(" Segment {0}:{1}{2}",
i, uri.Segments[i], Environment.NewLine);//uri.Segments = {string[3]} [0] = "/" [1] = "www.abc.com/" [2] = "home%20page.htm"
// GetComponents can be used to get commonly used combinations
// of Uri information
uriParts.AppendFormat("GetComponents for specialized combinations: {0}",
Environment.NewLine);
uriParts.AppendFormat("Host and Port (unescaped): {0}{1}",
uri.GetComponents(UriComponents.HostAndPort,
UriFormat.Unescaped), Environment.NewLine);
uriParts.AppendFormat("HttpRequestUrl (unescaped): {0}{1}",
uri.GetComponents(UriComponents.HttpRequestUrl,
UriFormat.Unescaped), Environment.NewLine);
uriParts.AppendFormat("HttpRequestUrl (escaped): {0}{1}",
uri.GetComponents(UriComponents.HttpRequestUrl,
UriFormat.UriEscaped), Environment.NewLine);
uriParts.AppendFormat("HttpRequestUrl (safeunescaped): {0}{1}",
uri.GetComponents(UriComponents.HttpRequestUrl,
UriFormat.SafeUnescaped), Environment.NewLine);
uriParts.AppendFormat("Scheme And Server (unescaped): {0}{1}",
uri.GetComponents(UriComponents.SchemeAndServer,
UriFormat.Unescaped), Environment.NewLine);
uriParts.AppendFormat("SerializationInfo String (unescaped): {0}{1}",
uri.GetComponents(UriComponents.SerializationInfoString,
UriFormat.Unescaped), Environment.NewLine);
uriParts.AppendFormat("StrongAuthority (unescaped): {0}{1}",
uri.GetComponents(UriComponents.StrongAuthority,
UriFormat.Unescaped), Environment.NewLine);
uriParts.AppendFormat("StrongPort (unescaped): {0}{1}",
uri.GetComponents(UriComponents.StrongPort,
UriFormat.Unescaped), Environment.NewLine);
// write out our summary
Console.WriteLine(uriParts.ToString());
}
catch (ArgumentNullException e)
{
// uriString is a null reference (Nothing in Visual Basic).
Console.WriteLine("URI string object is a null reference: {0}", e);
}
catch (UriFormatException e)
{
Console.WriteLine("URI formatting error: {0}", e);
}
}
#endregion
}
}
原创文章转载请注明出处:云飞扬IT的blog





