HttpUrl.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace COSXML.Network
  5. {
  6. public sealed class HttpUrl
  7. {
  8. private string scheme;
  9. private string userName = "";
  10. private string userPwd = "";
  11. private string host;
  12. private int port = -1;
  13. private string path = "/";
  14. private Dictionary<string, string> queryParameters;
  15. private string fragment;
  16. public HttpUrl()
  17. {
  18. this.queryParameters = new Dictionary<string, string>();
  19. }
  20. public string Scheme
  21. {
  22. set
  23. {
  24. if (value == null)
  25. {
  26. throw new ArgumentNullException("scheme == null");
  27. }
  28. if (value.Equals("http", StringComparison.OrdinalIgnoreCase))
  29. {
  30. this.scheme = "http";
  31. }
  32. else
  33. if (value.Equals("https", StringComparison.OrdinalIgnoreCase))
  34. {
  35. this.scheme = "https";
  36. }
  37. else
  38. {
  39. throw new ArgumentException("unexpected scheme: " + scheme);
  40. }
  41. }
  42. get
  43. {
  44. return this.scheme;
  45. }
  46. }
  47. public string UserName
  48. {
  49. set
  50. {
  51. if (value == null)
  52. {
  53. throw new ArgumentNullException("userName == null");
  54. }
  55. this.userName = value;
  56. }
  57. get
  58. {
  59. return this.userName;
  60. }
  61. }
  62. public string UserPassword
  63. {
  64. set
  65. {
  66. if (value == null)
  67. {
  68. throw new ArgumentNullException("userPwd == null");
  69. }
  70. this.userPwd = value;
  71. }
  72. get
  73. {
  74. return this.userPwd;
  75. }
  76. }
  77. public string Host
  78. {
  79. set
  80. {
  81. if (value == null)
  82. {
  83. throw new ArgumentNullException("host == null");
  84. }
  85. this.host = value;
  86. }
  87. get
  88. {
  89. return this.host;
  90. }
  91. }
  92. public int Port
  93. {
  94. set
  95. {
  96. if (value <= 0 || value >= 65535)
  97. {
  98. throw new ArgumentException("unexpected port: " + port);
  99. }
  100. this.port = value;
  101. }
  102. get
  103. {
  104. return this.port;
  105. }
  106. }
  107. public string Path
  108. {
  109. set
  110. {
  111. if (value != null)
  112. {
  113. // need url encode
  114. // need url encode
  115. this.path = value;
  116. }
  117. }
  118. get
  119. {
  120. return path;
  121. }
  122. }
  123. public void SetQueryParameters(Dictionary<string, string> queryParameters)
  124. {
  125. if (queryParameters != null)
  126. {
  127. foreach (KeyValuePair<string, string> pair in queryParameters)
  128. {
  129. this.queryParameters.Add(pair.Key, pair.Value);
  130. }
  131. }
  132. }
  133. public Dictionary<string, string> GetQueryParameters()
  134. {
  135. return queryParameters;
  136. }
  137. public string Fragment
  138. {
  139. set
  140. {
  141. this.fragment = value;
  142. }
  143. get
  144. {
  145. return this.fragment;
  146. }
  147. }
  148. public override string ToString()
  149. {
  150. //if (scheme == null) throw new ArgumentNullException("scheme == null");
  151. //if (host == null) throw new ArgumentNullException("host == null");
  152. StringBuilder url = new StringBuilder();
  153. url.Append(scheme)
  154. .Append("://");
  155. if (userName != String.Empty || userPwd != String.Empty)
  156. {
  157. url.Append(userName);
  158. if (userPwd != String.Empty)
  159. {
  160. url.Append(':')
  161. .Append(userPwd);
  162. }
  163. url.Append('@');
  164. }
  165. if (host.IndexOf(':') != -1)
  166. {
  167. url.Append('[')
  168. .Append(host)
  169. .Append(']');
  170. }
  171. else
  172. {
  173. url.Append(host);
  174. }
  175. int effectivePort = EffecivePort();
  176. if (effectivePort != DefaultPort(scheme))
  177. {
  178. url.Append(':')
  179. .Append(effectivePort);
  180. }
  181. url.Append(path);
  182. StringBuilder query = new StringBuilder();
  183. foreach (KeyValuePair<string, string> pair in queryParameters)
  184. {
  185. query.Append(pair.Key);
  186. if (!String.IsNullOrEmpty(pair.Value))
  187. {
  188. query.Append('=').Append(pair.Value);
  189. }
  190. query.Append('&');
  191. }
  192. string queryString = query.ToString();
  193. if (queryString.EndsWith("&"))
  194. {
  195. queryString = queryString.Remove(queryString.Length - 1);
  196. url.Append('?');
  197. url.Append(queryString);
  198. }
  199. if (fragment != null)
  200. {
  201. url.Append('#')
  202. .Append(fragment);
  203. }
  204. return url.ToString();
  205. }
  206. public int EffecivePort()
  207. {
  208. return port != -1 ? port : DefaultPort(scheme);
  209. }
  210. private int DefaultPort(string scheme)
  211. {
  212. if (scheme.Equals("http", StringComparison.OrdinalIgnoreCase))
  213. {
  214. return 80;
  215. }
  216. else if (scheme.Equals("https", StringComparison.OrdinalIgnoreCase))
  217. {
  218. return 443;
  219. }
  220. else
  221. {
  222. return -1;
  223. }
  224. }
  225. }
  226. }