Request.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using COSXML.Auth;
  5. using COSXML.Log;
  6. using COSXML.Common;
  7. using System.Net;
  8. namespace COSXML.Network
  9. {
  10. public sealed class Request
  11. {
  12. private static string TAG = "Request";
  13. // post put get delete head, etc.
  14. private string method;
  15. // shceme://host:port/path?query# etc.
  16. private HttpUrl url;
  17. // key : value
  18. private Dictionary<string, string> headers;
  19. // file or byte, etc.
  20. private RequestBody body;
  21. // package tag for request
  22. private Object tag;
  23. private bool isHttps;
  24. private string userAgent;
  25. private string host;
  26. private string urlString;
  27. private HttpWebRequest realeHttpRequest;
  28. public Request()
  29. {
  30. headers = new Dictionary<string, string>();
  31. this.onNotifyGetResponse = this.HandleGetResponse;
  32. }
  33. public string Method
  34. {
  35. get
  36. {
  37. return method;
  38. }
  39. set { method = value; }
  40. }
  41. public bool IsHttps
  42. {
  43. get
  44. {
  45. return isHttps;
  46. }
  47. set { isHttps = value; }
  48. }
  49. public string UserAgent
  50. {
  51. get
  52. {
  53. return userAgent == null ? CosVersion.GetUserAgent() : userAgent;
  54. }
  55. set { userAgent = value; }
  56. }
  57. public string Host
  58. {
  59. get
  60. {
  61. return host == null ? url.Host : host;
  62. }
  63. set { host = value; }
  64. }
  65. public HttpUrl Url
  66. {
  67. get
  68. {
  69. //if (url == null) throw new ArgumentNullException("httpUrl == null");
  70. return url;
  71. }
  72. set
  73. {
  74. if (value == null)
  75. {
  76. throw new ArgumentNullException("httpUrl == null");
  77. }
  78. url = value;
  79. }
  80. }
  81. public string RequestUrlString
  82. {
  83. get
  84. {
  85. if (urlString == null)
  86. {
  87. urlString = url.ToString();
  88. }
  89. return urlString;
  90. }
  91. set { urlString = value; }
  92. }
  93. public Dictionary<string, string> Headers
  94. {
  95. get
  96. {
  97. return headers;
  98. }
  99. private set { }
  100. }
  101. public void AddHeader(string name, string value)
  102. {
  103. try
  104. {
  105. headers.Add(name, value);
  106. }
  107. catch (ArgumentNullException)
  108. {
  109. QLog.Debug(TAG, "AddHeader: name is null");
  110. }
  111. catch (ArgumentException)
  112. {
  113. if (String.IsNullOrEmpty(value))
  114. {
  115. return;
  116. }
  117. if (!String.IsNullOrEmpty(headers[name]))
  118. {
  119. headers[name] = headers[name] + ',' + value;
  120. }
  121. else
  122. {
  123. headers[name] = value;
  124. }
  125. }
  126. }
  127. public RequestBody Body
  128. {
  129. get
  130. {
  131. return body;
  132. }
  133. set { body = value; }
  134. }
  135. public COSXML.Callback.OnNotifyGetResponse onNotifyGetResponse;
  136. private void HandleGetResponse()
  137. {
  138. if (body != null)
  139. {
  140. body.OnNotifyGetResponse();
  141. }
  142. }
  143. public void BindHttpWebRequest(HttpWebRequest httpWebRequest)
  144. {
  145. this.realeHttpRequest = httpWebRequest;
  146. }
  147. public void Cancel()
  148. {
  149. if (realeHttpRequest != null)
  150. {
  151. realeHttpRequest.Abort();
  152. }
  153. }
  154. }
  155. }