HttpRequestOptions.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. /// <summary>
  18. /// Gets or sets the accept header.
  19. /// </summary>
  20. /// <value>The accept header.</value>
  21. public string AcceptHeader
  22. {
  23. get { return GetHeaderValue("Accept"); }
  24. set
  25. {
  26. RequestHeaders["Accept"] = value;
  27. }
  28. }
  29. /// <summary>
  30. /// Gets or sets the cancellation token.
  31. /// </summary>
  32. /// <value>The cancellation token.</value>
  33. public CancellationToken CancellationToken { get; set; }
  34. /// <summary>
  35. /// Gets or sets the resource pool.
  36. /// </summary>
  37. /// <value>The resource pool.</value>
  38. public SemaphoreSlim ResourcePool { get; set; }
  39. /// <summary>
  40. /// Gets or sets the user agent.
  41. /// </summary>
  42. /// <value>The user agent.</value>
  43. public string UserAgent
  44. {
  45. get { return GetHeaderValue("User-Agent"); }
  46. set
  47. {
  48. RequestHeaders["User-Agent"] = value;
  49. }
  50. }
  51. /// <summary>
  52. /// Gets or sets the referrer.
  53. /// </summary>
  54. /// <value>The referrer.</value>
  55. public string Referer { get; set; }
  56. /// <summary>
  57. /// Gets or sets the host.
  58. /// </summary>
  59. /// <value>The host.</value>
  60. public string Host { get; set; }
  61. /// <summary>
  62. /// Gets or sets the progress.
  63. /// </summary>
  64. /// <value>The progress.</value>
  65. public IProgress<double> Progress { get; set; }
  66. /// <summary>
  67. /// Gets or sets a value indicating whether [enable HTTP compression].
  68. /// </summary>
  69. /// <value><c>true</c> if [enable HTTP compression]; otherwise, <c>false</c>.</value>
  70. public bool EnableHttpCompression { get; set; }
  71. public Dictionary<string, string> RequestHeaders { get; private set; }
  72. public string RequestContentType { get; set; }
  73. public string RequestContent { get; set; }
  74. public byte[] RequestContentBytes { get; set; }
  75. public bool BufferContent { get; set; }
  76. public bool LogRequest { 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. private string GetHeaderValue(string name)
  83. {
  84. string value;
  85. RequestHeaders.TryGetValue(name, out value);
  86. return value;
  87. }
  88. /// <summary>
  89. /// Initializes a new instance of the <see cref="HttpRequestOptions"/> class.
  90. /// </summary>
  91. public HttpRequestOptions()
  92. {
  93. EnableHttpCompression = true;
  94. BufferContent = true;
  95. RequestHeaders = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
  96. LogRequest = true;
  97. CacheMode = CacheMode.None;
  98. TimeoutMs = 20000;
  99. }
  100. public void SetPostData(IDictionary<string,string> values)
  101. {
  102. var strings = values.Keys.Select(key => string.Format("{0}={1}", key, values[key]));
  103. var postContent = string.Join("&", strings.ToArray());
  104. RequestContent = postContent;
  105. RequestContentType = "application/x-www-form-urlencoded";
  106. }
  107. }
  108. public enum CacheMode
  109. {
  110. None = 0,
  111. Unconditional = 1
  112. }
  113. }