HttpRequestOptions.cs 4.3 KB

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