HttpClientConfig.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using COSXML.Common;
  5. namespace COSXML.Network
  6. {
  7. public class HttpClientConfig
  8. {
  9. private string userAgent;
  10. private bool allowAutoRedirect;
  11. private int connectionTimeoutMs;
  12. private int readWriteTimeoutMs;
  13. private int maxRetry;
  14. private int connectionLimit;
  15. private string proxyHost;
  16. private int proxyPort;
  17. private string proxyUserName;
  18. private string proxyUserPassword;
  19. private string proxyDomain;
  20. private bool keepAlive;
  21. private HttpClientConfig(Builder builder)
  22. {
  23. this.userAgent = builder.userAgent;
  24. this.allowAutoRedirect = builder.allowAutoRedirect;
  25. this.connectionTimeoutMs = builder.connectionTimeoutMs;
  26. this.readWriteTimeoutMs = builder.readWriteTimeoutMs;
  27. this.maxRetry = builder.maxRetry;
  28. this.connectionLimit = builder.connectionLimit;
  29. this.proxyHost = builder.proxyHost;
  30. this.proxyPort = builder.proxyPort;
  31. this.proxyUserName = builder.proxyUserName;
  32. this.proxyUserPassword = builder.proxyUserPassword;
  33. this.proxyDomain = builder.proxyDomain;
  34. this.keepAlive = builder.keepAlive;
  35. }
  36. public string UserAgnet
  37. {
  38. get
  39. {
  40. return userAgent;
  41. }
  42. }
  43. public bool AllowAutoRedirect
  44. {
  45. get
  46. {
  47. return allowAutoRedirect;
  48. }
  49. }
  50. public int ConnectionTimeoutMs
  51. {
  52. get
  53. {
  54. return connectionTimeoutMs;
  55. }
  56. }
  57. public int ReadWriteTimeoutMs
  58. {
  59. get
  60. {
  61. return readWriteTimeoutMs;
  62. }
  63. }
  64. public int ConnectionLimit
  65. {
  66. get
  67. {
  68. return connectionLimit;
  69. }
  70. }
  71. public string ProxyHost
  72. {
  73. get
  74. {
  75. return proxyHost;
  76. }
  77. }
  78. public int ProxyPort
  79. {
  80. get
  81. {
  82. return proxyPort;
  83. }
  84. }
  85. public string ProxyUserName
  86. {
  87. get
  88. {
  89. return proxyUserName;
  90. }
  91. }
  92. public string ProxyUserPassword
  93. {
  94. get
  95. {
  96. return proxyUserPassword;
  97. }
  98. }
  99. public string ProxyDomain
  100. {
  101. get
  102. {
  103. return proxyDomain;
  104. }
  105. }
  106. public bool KeepAlive
  107. {
  108. get
  109. {
  110. return keepAlive;
  111. }
  112. }
  113. public int MaxRetry
  114. {
  115. get
  116. {
  117. return maxRetry;
  118. }
  119. }
  120. public class Builder
  121. {
  122. internal string userAgent = CosVersion.GetUserAgent();
  123. internal bool allowAutoRedirect = true;
  124. internal int connectionTimeoutMs = 45000;
  125. internal int readWriteTimeoutMs = 45000;
  126. internal int maxRetry = 3;
  127. internal int connectionLimit = 512;
  128. internal int proxyPort = -1;
  129. internal string proxyHost = null;
  130. internal string proxyUserName;
  131. internal string proxyUserPassword;
  132. internal string proxyDomain;
  133. internal bool keepAlive = true;
  134. public Builder()
  135. {
  136. }
  137. public Builder AllowAutoRedirect(bool allow)
  138. {
  139. this.allowAutoRedirect = allow;
  140. return this;
  141. }
  142. public Builder SetConnectionLimit(int connectionLimit)
  143. {
  144. if (connectionLimit > 0)
  145. {
  146. this.connectionLimit = connectionLimit;
  147. }
  148. return this;
  149. }
  150. public Builder SetConnectionTimeoutMs(int connectionTimeoutMs)
  151. {
  152. if (connectionTimeoutMs > 0)
  153. {
  154. this.connectionTimeoutMs = connectionTimeoutMs;
  155. }
  156. return this;
  157. }
  158. public Builder SetReadWriteTimeoutMs(int readWriteTimeoutMs)
  159. {
  160. if (readWriteTimeoutMs > 0)
  161. {
  162. this.readWriteTimeoutMs = readWriteTimeoutMs;
  163. }
  164. return this;
  165. }
  166. public Builder SetProxyHost(string host)
  167. {
  168. this.proxyHost = host;
  169. return this;
  170. }
  171. public Builder SetProxyPort(int port)
  172. {
  173. this.proxyPort = port;
  174. return this;
  175. }
  176. public Builder SetProxyUserName(string userName)
  177. {
  178. this.proxyUserName = userName;
  179. return this;
  180. }
  181. public Builder SetProxyUserPassword(string password)
  182. {
  183. this.proxyUserPassword = password;
  184. return this;
  185. }
  186. public Builder SetProxyDomain(string domain)
  187. {
  188. this.proxyDomain = domain;
  189. return this;
  190. }
  191. public Builder SetHttpKeepAlive(bool keepAlive)
  192. {
  193. this.keepAlive = keepAlive;
  194. return this;
  195. }
  196. public Builder SetMaxRetry(int maxRetry)
  197. {
  198. this.maxRetry = maxRetry;
  199. return this;
  200. }
  201. public HttpClientConfig Build()
  202. {
  203. return new HttpClientConfig(this);
  204. }
  205. }
  206. }
  207. }