HttpRequest.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine.Networking;
  4. namespace EZXR.Glass.Network.Http
  5. {
  6. /**
  7. * \~english
  8. * @enum Http Method type
  9. *
  10. * \~chinese
  11. * @enum Http 方法类型
  12. */
  13. public enum HttpMethod {
  14. POST,/**<
  15. * \~english POST method
  16. * \~chinese POST方法
  17. */
  18. GET/**<
  19. * \~english GET method
  20. * \~chinese GET方法
  21. */
  22. }
  23. /**
  24. * \~english
  25. * Http request class
  26. *
  27. * \~chinese
  28. * Http请求类
  29. */
  30. public class HttpRequest
  31. {
  32. /**
  33. * \~english
  34. * Request address
  35. *
  36. * \~chinese
  37. * 请求地址
  38. */
  39. public string url;
  40. /**
  41. * \~english
  42. * Request address
  43. *
  44. * \~chinese
  45. * 请求地址
  46. */
  47. public HttpMethod method;
  48. /**
  49. * \~english
  50. * Request header
  51. *
  52. * \~chinese
  53. * 请求头
  54. */
  55. private Dictionary<string, string> headers;
  56. /**
  57. * \~english
  58. * Request body
  59. *
  60. * \~chinese
  61. * 请求体
  62. */
  63. public RequestBody requestBody;
  64. /**
  65. * \~english
  66. * The timeout period of the request
  67. *
  68. * \~chinese
  69. * 该请求的超时时间
  70. */
  71. public int timeout;
  72. public HttpRequest(HttpMethod method) {
  73. this.method = method;
  74. this.url = "";
  75. this.requestBody = null;
  76. this.timeout = 0;
  77. this.headers = new Dictionary<string, string> ();
  78. }
  79. // public HttpRequest Url(string url) {
  80. // this.url = url;
  81. // return this;
  82. // }
  83. // public HttpRequest Method(HttpMethod method, RequestBody body) {
  84. // // if (method == null) throw new NullReferenceException ("method cannot be null");
  85. // // if (method.Length == 0) throw new InvalidOperationException ("method cannot be empty");
  86. // this.method = method;
  87. // this.body = body;
  88. // return this;
  89. // }
  90. /**
  91. * \~english
  92. * @brief Add request header
  93. *
  94. * @param name Request header name
  95. *
  96. * @param value Request header's value
  97. *
  98. * \~chinese
  99. * @brief 添加请求头
  100. *
  101. * @param name 请求头名字
  102. *
  103. * @param value 请求头的值
  104. */
  105. public void AddHeader(string name, string value) {
  106. this.headers.Add (name, value);
  107. }
  108. /**
  109. * \~english
  110. * @brief Remove request header
  111. *
  112. * @param name Request header name
  113. *
  114. * \~chinese
  115. * @brief 移除请求头
  116. *
  117. * @param name 请求头名字
  118. *
  119. */
  120. public void RemoveHeader(string name) {
  121. this.headers.Remove (name);
  122. }
  123. // public HttpRequest Timeout(int timeout) {
  124. // this.timeout = timeout;
  125. // return this;
  126. // }
  127. // public int Timeout() {
  128. // return timeout;
  129. // }
  130. // public HttpRequest Get() {
  131. // Method (UnityWebRequest.kHttpVerbGET, null);
  132. // return this;
  133. // }
  134. // public HttpRequest Post(RequestBody body) {
  135. // Method (UnityWebRequest.kHttpVerbPOST, body);
  136. // return this;
  137. // }
  138. // public HttpRequest Put(RequestBody body) {
  139. // Method (UnityWebRequest.kHttpVerbPUT, body);
  140. // return this;
  141. // }
  142. // public HttpRequest Delete() {
  143. // Method (UnityWebRequest.kHttpVerbDELETE, null);
  144. // return this;
  145. // }
  146. // public string Url() {
  147. // return url;
  148. // }
  149. // public HttpMethod Method() {
  150. // return method;
  151. // }
  152. // public RequestBody Body() {
  153. // return body;
  154. // }
  155. /**
  156. * \~english
  157. * @brief Get all request headers
  158. *
  159. * \~chinese
  160. * @brief 获取所有请求头
  161. *
  162. */
  163. public Dictionary<string, string> Headers() {
  164. return headers;
  165. }
  166. }
  167. }