HttpRequestOptions.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. private string GetHeaderValue(string name)
  82. {
  83. string value;
  84. RequestHeaders.TryGetValue(name, out value);
  85. return value;
  86. }
  87. /// <summary>
  88. /// Initializes a new instance of the <see cref="HttpRequestOptions"/> class.
  89. /// </summary>
  90. public HttpRequestOptions()
  91. {
  92. EnableHttpCompression = true;
  93. BufferContent = true;
  94. RequestHeaders = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
  95. LogRequest = true;
  96. CacheMode = CacheMode.None;
  97. }
  98. public void SetPostData(IDictionary<string,string> values)
  99. {
  100. var strings = values.Keys.Select(key => string.Format("{0}={1}", key, values[key]));
  101. var postContent = string.Join("&", strings.ToArray());
  102. RequestContent = postContent;
  103. RequestContentType = "application/x-www-form-urlencoded";
  104. }
  105. }
  106. public enum CacheMode
  107. {
  108. None = 1,
  109. Unconditional = 2
  110. }
  111. }