HttpRequestOptions.cs 4.2 KB

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