123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace COSXML.Network
- {
-
- public sealed class HttpUrl
- {
- private string scheme;
- private string userName = "";
- private string userPwd = "";
- private string host;
- private int port = -1;
- private string path = "/";
- private Dictionary<string, string> queryParameters;
- private string fragment;
- public HttpUrl()
- {
- this.queryParameters = new Dictionary<string, string>();
- }
- public string Scheme
- {
- set
- {
- if (value == null)
- {
- throw new ArgumentNullException("scheme == null");
- }
- if (value.Equals("http", StringComparison.OrdinalIgnoreCase))
- {
- this.scheme = "http";
- }
- else
- if (value.Equals("https", StringComparison.OrdinalIgnoreCase))
- {
- this.scheme = "https";
- }
- else
- {
- throw new ArgumentException("unexpected scheme: " + scheme);
- }
- }
- get
- {
- return this.scheme;
- }
- }
- public string UserName
- {
- set
- {
- if (value == null)
- {
- throw new ArgumentNullException("userName == null");
- }
- this.userName = value;
- }
- get
- {
- return this.userName;
- }
- }
- public string UserPassword
- {
- set
- {
- if (value == null)
- {
- throw new ArgumentNullException("userPwd == null");
- }
- this.userPwd = value;
- }
- get
- {
- return this.userPwd;
- }
- }
- public string Host
- {
- set
- {
- if (value == null)
- {
- throw new ArgumentNullException("host == null");
- }
- this.host = value;
- }
- get
- {
- return this.host;
- }
- }
- public int Port
- {
- set
- {
- if (value <= 0 || value >= 65535)
- {
- throw new ArgumentException("unexpected port: " + port);
- }
- this.port = value;
- }
- get
- {
- return this.port;
- }
- }
- public string Path
- {
- set
- {
- if (value != null)
- {
- // need url encode
- // need url encode
- this.path = value;
- }
- }
- get
- {
- return path;
- }
- }
- public void SetQueryParameters(Dictionary<string, string> queryParameters)
- {
- if (queryParameters != null)
- {
- foreach (KeyValuePair<string, string> pair in queryParameters)
- {
- this.queryParameters.Add(pair.Key, pair.Value);
- }
- }
- }
- public Dictionary<string, string> GetQueryParameters()
- {
- return queryParameters;
- }
- public string Fragment
- {
- set
- {
- this.fragment = value;
- }
- get
- {
- return this.fragment;
- }
- }
- public override string ToString()
- {
- //if (scheme == null) throw new ArgumentNullException("scheme == null");
- //if (host == null) throw new ArgumentNullException("host == null");
- StringBuilder url = new StringBuilder();
- url.Append(scheme)
- .Append("://");
- if (userName != String.Empty || userPwd != String.Empty)
- {
- url.Append(userName);
- if (userPwd != String.Empty)
- {
- url.Append(':')
- .Append(userPwd);
- }
- url.Append('@');
- }
- if (host.IndexOf(':') != -1)
- {
- url.Append('[')
- .Append(host)
- .Append(']');
- }
- else
- {
- url.Append(host);
- }
- int effectivePort = EffecivePort();
- if (effectivePort != DefaultPort(scheme))
- {
- url.Append(':')
- .Append(effectivePort);
- }
- url.Append(path);
- StringBuilder query = new StringBuilder();
- foreach (KeyValuePair<string, string> pair in queryParameters)
- {
- query.Append(pair.Key);
- if (!String.IsNullOrEmpty(pair.Value))
- {
- query.Append('=').Append(pair.Value);
- }
- query.Append('&');
- }
- string queryString = query.ToString();
- if (queryString.EndsWith("&"))
- {
- queryString = queryString.Remove(queryString.Length - 1);
- url.Append('?');
- url.Append(queryString);
- }
- if (fragment != null)
- {
- url.Append('#')
- .Append(fragment);
- }
- return url.ToString();
- }
- public int EffecivePort()
- {
- return port != -1 ? port : DefaultPort(scheme);
- }
- private int DefaultPort(string scheme)
- {
- if (scheme.Equals("http", StringComparison.OrdinalIgnoreCase))
- {
- return 80;
- }
- else if (scheme.Equals("https", StringComparison.OrdinalIgnoreCase))
- {
- return 443;
- }
- else
- {
- return -1;
- }
- }
- }
- }
|