HttpRequestOptions.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Threading;
  6. using System.Text;
  7. namespace MediaBrowser.Common.Net
  8. {
  9. /// <summary>
  10. /// Class HttpRequestOptions
  11. /// </summary>
  12. public class HttpRequestOptions
  13. {
  14. /// <summary>
  15. /// Gets or sets the URL.
  16. /// </summary>
  17. /// <value>The URL.</value>
  18. public string Url { get; set; }
  19. public CompressionMethod? DecompressionMethod { get; set; }
  20. /// <summary>
  21. /// Gets or sets the accept header.
  22. /// </summary>
  23. /// <value>The accept header.</value>
  24. public string AcceptHeader
  25. {
  26. get { return GetHeaderValue("Accept"); }
  27. set
  28. {
  29. RequestHeaders["Accept"] = value;
  30. }
  31. }
  32. /// <summary>
  33. /// Gets or sets the cancellation token.
  34. /// </summary>
  35. /// <value>The cancellation token.</value>
  36. public CancellationToken CancellationToken { get; set; }
  37. /// <summary>
  38. /// Gets or sets the resource pool.
  39. /// </summary>
  40. /// <value>The resource pool.</value>
  41. public SemaphoreSlim ResourcePool { get; set; }
  42. /// <summary>
  43. /// Gets or sets the user agent.
  44. /// </summary>
  45. /// <value>The user agent.</value>
  46. public string UserAgent
  47. {
  48. get { return GetHeaderValue("User-Agent"); }
  49. set
  50. {
  51. RequestHeaders["User-Agent"] = value;
  52. }
  53. }
  54. /// <summary>
  55. /// Gets or sets the referrer.
  56. /// </summary>
  57. /// <value>The referrer.</value>
  58. public string Referer { get; set; }
  59. /// <summary>
  60. /// Gets or sets the host.
  61. /// </summary>
  62. /// <value>The host.</value>
  63. public string Host { get; set; }
  64. /// <summary>
  65. /// Gets or sets the progress.
  66. /// </summary>
  67. /// <value>The progress.</value>
  68. public IProgress<double> Progress { get; set; }
  69. /// <summary>
  70. /// Gets or sets a value indicating whether [enable HTTP compression].
  71. /// </summary>
  72. /// <value><c>true</c> if [enable HTTP compression]; otherwise, <c>false</c>.</value>
  73. public bool EnableHttpCompression { get; set; }
  74. public Dictionary<string, string> RequestHeaders { get; private set; }
  75. public string RequestContentType { get; set; }
  76. public string RequestContent { get; set; }
  77. public byte[] RequestContentBytes { get; set; }
  78. public bool BufferContent { get; set; }
  79. public bool LogRequest { get; set; }
  80. public bool LogRequestAsDebug { get; set; }
  81. public bool LogErrors { get; set; }
  82. public bool LogResponse { get; set; }
  83. public bool LogResponseHeaders { get; set; }
  84. public bool LogErrorResponseBody { get; set; }
  85. public bool EnableKeepAlive { get; set; }
  86. public CacheMode CacheMode { get; set; }
  87. public TimeSpan CacheLength { get; set; }
  88. public int TimeoutMs { get; set; }
  89. public bool EnableDefaultUserAgent { get; set; }
  90. public bool AppendCharsetToMimeType { get; set; }
  91. public string DownloadFilePath { get; set; }
  92. private string GetHeaderValue(string name)
  93. {
  94. string value;
  95. RequestHeaders.TryGetValue(name, out value);
  96. return value;
  97. }
  98. /// <summary>
  99. /// Initializes a new instance of the <see cref="HttpRequestOptions"/> class.
  100. /// </summary>
  101. public HttpRequestOptions()
  102. {
  103. EnableHttpCompression = true;
  104. RequestHeaders = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
  105. LogRequest = true;
  106. LogErrors = true;
  107. CacheMode = CacheMode.None;
  108. TimeoutMs = 20000;
  109. }
  110. public void SetPostData(IDictionary<string,string> values)
  111. {
  112. var strings = values.Keys.Select(key => string.Format("{0}={1}", key, values[key]));
  113. var postContent = string.Join("&", strings.ToArray());
  114. RequestContent = postContent;
  115. RequestContentType = "application/x-www-form-urlencoded";
  116. }
  117. }
  118. public enum CacheMode
  119. {
  120. None = 0,
  121. Unconditional = 1
  122. }
  123. public enum CompressionMethod
  124. {
  125. Deflate,
  126. Gzip
  127. }
  128. }