HttpClientManager.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. using MediaBrowser.Common.Configuration;
  2. using MediaBrowser.Common.Extensions;
  3. using MediaBrowser.Common.IO;
  4. using MediaBrowser.Common.Net;
  5. using MediaBrowser.Model.Logging;
  6. using MediaBrowser.Model.Net;
  7. using MediaBrowser.Model.Serialization;
  8. using System;
  9. using System.Collections.Concurrent;
  10. using System.Collections.Generic;
  11. using System.Globalization;
  12. using System.IO;
  13. using System.Linq;
  14. using System.Net;
  15. using System.Net.Cache;
  16. using System.Net.Http;
  17. using System.Text;
  18. using System.Threading;
  19. using System.Threading.Tasks;
  20. namespace MediaBrowser.Common.Implementations.HttpClientManager
  21. {
  22. /// <summary>
  23. /// Class HttpClientManager
  24. /// </summary>
  25. public class HttpClientManager : IHttpClient
  26. {
  27. /// <summary>
  28. /// The _logger
  29. /// </summary>
  30. private readonly ILogger _logger;
  31. /// <summary>
  32. /// The _app paths
  33. /// </summary>
  34. private readonly IApplicationPaths _appPaths;
  35. private readonly IJsonSerializer _jsonSerializer;
  36. private readonly FileSystemRepository _cacheRepository;
  37. /// <summary>
  38. /// Initializes a new instance of the <see cref="HttpClientManager" /> class.
  39. /// </summary>
  40. /// <param name="appPaths">The kernel.</param>
  41. /// <param name="logger">The logger.</param>
  42. /// <param name="jsonSerializer">The json serializer.</param>
  43. /// <exception cref="System.ArgumentNullException">
  44. /// appPaths
  45. /// or
  46. /// logger
  47. /// </exception>
  48. public HttpClientManager(IApplicationPaths appPaths, ILogger logger, IJsonSerializer jsonSerializer)
  49. {
  50. if (appPaths == null)
  51. {
  52. throw new ArgumentNullException("appPaths");
  53. }
  54. if (logger == null)
  55. {
  56. throw new ArgumentNullException("logger");
  57. }
  58. _logger = logger;
  59. _jsonSerializer = jsonSerializer;
  60. _appPaths = appPaths;
  61. _cacheRepository = new FileSystemRepository(Path.Combine(_appPaths.CachePath, "downloads"));
  62. }
  63. /// <summary>
  64. /// Holds a dictionary of http clients by host. Use GetHttpClient(host) to retrieve or create a client for web requests.
  65. /// DON'T dispose it after use.
  66. /// </summary>
  67. /// <value>The HTTP clients.</value>
  68. private readonly ConcurrentDictionary<string, HttpClient> _httpClients = new ConcurrentDictionary<string, HttpClient>();
  69. /// <summary>
  70. /// Gets
  71. /// </summary>
  72. /// <param name="host">The host.</param>
  73. /// <returns>HttpClient.</returns>
  74. /// <exception cref="System.ArgumentNullException">host</exception>
  75. private HttpClient GetHttpClient(string host)
  76. {
  77. if (string.IsNullOrEmpty(host))
  78. {
  79. throw new ArgumentNullException("host");
  80. }
  81. HttpClient client;
  82. if (!_httpClients.TryGetValue(host, out client))
  83. {
  84. var handler = new WebRequestHandler
  85. {
  86. CachePolicy = new RequestCachePolicy(RequestCacheLevel.BypassCache),
  87. AutomaticDecompression = DecompressionMethods.None
  88. };
  89. client = new HttpClient(handler);
  90. client.Timeout = TimeSpan.FromSeconds(30);
  91. _httpClients.TryAdd(host, client);
  92. }
  93. return client;
  94. }
  95. /// <summary>
  96. /// Performs a GET request and returns the resulting stream
  97. /// </summary>
  98. /// <param name="options">The options.</param>
  99. /// <returns>Task{Stream}.</returns>
  100. /// <exception cref="HttpException"></exception>
  101. /// <exception cref="MediaBrowser.Model.Net.HttpException"></exception>
  102. public async Task<Stream> Get(HttpRequestOptions options)
  103. {
  104. ValidateParams(options.Url, options.CancellationToken);
  105. HttpResponseInfo cachedInfo = null;
  106. var urlHash = options.Url.GetMD5().ToString();
  107. var cachedInfoPath = _cacheRepository.GetResourcePath(urlHash + ".js");
  108. var cachedReponsePath = _cacheRepository.GetResourcePath(urlHash + ".dat");
  109. if (options.EnableResponseCache)
  110. {
  111. try
  112. {
  113. cachedInfo = _jsonSerializer.DeserializeFromFile<HttpResponseInfo>(cachedInfoPath);
  114. }
  115. catch (FileNotFoundException)
  116. {
  117. }
  118. if (cachedInfo != null)
  119. {
  120. var isCacheValid = (!cachedInfo.MustRevalidate && !string.IsNullOrEmpty(cachedInfo.Etag))
  121. || (cachedInfo.Expires.HasValue && cachedInfo.Expires.Value > DateTime.UtcNow);
  122. if (isCacheValid)
  123. {
  124. try
  125. {
  126. return GetCachedResponse(cachedReponsePath);
  127. }
  128. catch (FileNotFoundException)
  129. {
  130. }
  131. }
  132. }
  133. }
  134. options.CancellationToken.ThrowIfCancellationRequested();
  135. var message = GetHttpRequestMessage(options);
  136. if (options.EnableResponseCache && cachedInfo != null)
  137. {
  138. if (!string.IsNullOrEmpty(cachedInfo.Etag))
  139. {
  140. message.Headers.Add("If-None-Match", cachedInfo.Etag);
  141. }
  142. else if (cachedInfo.LastModified.HasValue)
  143. {
  144. message.Headers.IfModifiedSince = new DateTimeOffset(cachedInfo.LastModified.Value);
  145. }
  146. }
  147. if (options.ResourcePool != null)
  148. {
  149. await options.ResourcePool.WaitAsync(options.CancellationToken).ConfigureAwait(false);
  150. }
  151. _logger.Info("HttpClientManager.Get url: {0}", options.Url);
  152. try
  153. {
  154. options.CancellationToken.ThrowIfCancellationRequested();
  155. var response = await GetHttpClient(GetHostFromUrl(options.Url)).SendAsync(message, HttpCompletionOption.ResponseHeadersRead, options.CancellationToken).ConfigureAwait(false);
  156. EnsureSuccessStatusCode(response);
  157. options.CancellationToken.ThrowIfCancellationRequested();
  158. if (options.EnableResponseCache)
  159. {
  160. cachedInfo = UpdateInfoCache(cachedInfo, options.Url, cachedInfoPath, response);
  161. if (response.StatusCode == HttpStatusCode.NotModified)
  162. {
  163. return GetCachedResponse(cachedReponsePath);
  164. }
  165. if (!string.IsNullOrEmpty(cachedInfo.Etag) || cachedInfo.LastModified.HasValue || (cachedInfo.Expires.HasValue && cachedInfo.Expires.Value > DateTime.UtcNow))
  166. {
  167. await UpdateResponseCache(response, cachedReponsePath).ConfigureAwait(false);
  168. return GetCachedResponse(cachedReponsePath);
  169. }
  170. }
  171. return await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
  172. }
  173. catch (OperationCanceledException ex)
  174. {
  175. throw GetCancellationException(options.Url, options.CancellationToken, ex);
  176. }
  177. catch (HttpRequestException ex)
  178. {
  179. _logger.ErrorException("Error getting response from " + options.Url, ex);
  180. throw new HttpException(ex.Message, ex);
  181. }
  182. catch (Exception ex)
  183. {
  184. _logger.ErrorException("Error getting response from " + options.Url, ex);
  185. throw;
  186. }
  187. finally
  188. {
  189. if (options.ResourcePool != null)
  190. {
  191. options.ResourcePool.Release();
  192. }
  193. }
  194. }
  195. /// <summary>
  196. /// Performs a GET request and returns the resulting stream
  197. /// </summary>
  198. /// <param name="url">The URL.</param>
  199. /// <param name="resourcePool">The resource pool.</param>
  200. /// <param name="cancellationToken">The cancellation token.</param>
  201. /// <returns>Task{Stream}.</returns>
  202. public Task<Stream> Get(string url, SemaphoreSlim resourcePool, CancellationToken cancellationToken)
  203. {
  204. return Get(new HttpRequestOptions
  205. {
  206. Url = url,
  207. ResourcePool = resourcePool,
  208. CancellationToken = cancellationToken,
  209. });
  210. }
  211. /// <summary>
  212. /// Gets the specified URL.
  213. /// </summary>
  214. /// <param name="url">The URL.</param>
  215. /// <param name="cancellationToken">The cancellation token.</param>
  216. /// <returns>Task{Stream}.</returns>
  217. public Task<Stream> Get(string url, CancellationToken cancellationToken)
  218. {
  219. return Get(url, null, cancellationToken);
  220. }
  221. /// <summary>
  222. /// Gets the cached response.
  223. /// </summary>
  224. /// <param name="responsePath">The response path.</param>
  225. /// <returns>Stream.</returns>
  226. private Stream GetCachedResponse(string responsePath)
  227. {
  228. return File.OpenRead(responsePath);
  229. }
  230. /// <summary>
  231. /// Updates the cache.
  232. /// </summary>
  233. /// <param name="cachedInfo">The cached info.</param>
  234. /// <param name="url">The URL.</param>
  235. /// <param name="path">The path.</param>
  236. /// <param name="response">The response.</param>
  237. private HttpResponseInfo UpdateInfoCache(HttpResponseInfo cachedInfo, string url, string path, HttpResponseMessage response)
  238. {
  239. var fileExists = true;
  240. if (cachedInfo == null)
  241. {
  242. cachedInfo = new HttpResponseInfo();
  243. fileExists = false;
  244. }
  245. cachedInfo.Url = url;
  246. var etag = response.Headers.ETag;
  247. if (etag != null)
  248. {
  249. cachedInfo.Etag = etag.Tag;
  250. }
  251. var modified = response.Content.Headers.LastModified;
  252. if (modified.HasValue)
  253. {
  254. cachedInfo.LastModified = modified.Value.UtcDateTime;
  255. }
  256. else if (response.Headers.Age.HasValue)
  257. {
  258. cachedInfo.LastModified = DateTime.UtcNow.Subtract(response.Headers.Age.Value);
  259. }
  260. var expires = response.Content.Headers.Expires;
  261. if (expires.HasValue)
  262. {
  263. cachedInfo.Expires = expires.Value.UtcDateTime;
  264. }
  265. else
  266. {
  267. var cacheControl = response.Headers.CacheControl;
  268. if (cacheControl != null)
  269. {
  270. if (cacheControl.MaxAge.HasValue)
  271. {
  272. var baseline = cachedInfo.LastModified ?? DateTime.UtcNow;
  273. cachedInfo.Expires = baseline.Add(cacheControl.MaxAge.Value);
  274. }
  275. cachedInfo.MustRevalidate = cacheControl.MustRevalidate;
  276. }
  277. }
  278. if (string.IsNullOrEmpty(cachedInfo.Etag) && !cachedInfo.Expires.HasValue && !cachedInfo.LastModified.HasValue)
  279. {
  280. // Nothing to cache
  281. if (fileExists)
  282. {
  283. File.Delete(path);
  284. }
  285. }
  286. else
  287. {
  288. _jsonSerializer.SerializeToFile(cachedInfo, path);
  289. }
  290. return cachedInfo;
  291. }
  292. /// <summary>
  293. /// Updates the response cache.
  294. /// </summary>
  295. /// <param name="response">The response.</param>
  296. /// <param name="path">The path.</param>
  297. /// <returns>Task.</returns>
  298. private async Task UpdateResponseCache(HttpResponseMessage response, string path)
  299. {
  300. using (var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false))
  301. {
  302. using (var fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read, StreamDefaults.DefaultFileStreamBufferSize, FileOptions.Asynchronous))
  303. {
  304. await stream.CopyToAsync(fs).ConfigureAwait(false);
  305. }
  306. }
  307. }
  308. /// <summary>
  309. /// Performs a POST request
  310. /// </summary>
  311. /// <param name="url">The URL.</param>
  312. /// <param name="postData">Params to add to the POST data.</param>
  313. /// <param name="resourcePool">The resource pool.</param>
  314. /// <param name="cancellationToken">The cancellation token.</param>
  315. /// <returns>stream on success, null on failure</returns>
  316. /// <exception cref="System.ArgumentNullException">postData</exception>
  317. /// <exception cref="MediaBrowser.Model.Net.HttpException"></exception>
  318. public async Task<Stream> Post(string url, Dictionary<string, string> postData, SemaphoreSlim resourcePool, CancellationToken cancellationToken)
  319. {
  320. ValidateParams(url, cancellationToken);
  321. if (postData == null)
  322. {
  323. throw new ArgumentNullException("postData");
  324. }
  325. cancellationToken.ThrowIfCancellationRequested();
  326. var strings = postData.Keys.Select(key => string.Format("{0}={1}", key, postData[key]));
  327. var postContent = string.Join("&", strings.ToArray());
  328. var content = new StringContent(postContent, Encoding.UTF8, "application/x-www-form-urlencoded");
  329. if (resourcePool != null)
  330. {
  331. await resourcePool.WaitAsync(cancellationToken).ConfigureAwait(false);
  332. }
  333. _logger.Info("HttpClientManager.Post url: {0}", url);
  334. try
  335. {
  336. cancellationToken.ThrowIfCancellationRequested();
  337. var msg = await GetHttpClient(GetHostFromUrl(url)).PostAsync(url, content, cancellationToken).ConfigureAwait(false);
  338. EnsureSuccessStatusCode(msg);
  339. return await msg.Content.ReadAsStreamAsync().ConfigureAwait(false);
  340. }
  341. catch (OperationCanceledException ex)
  342. {
  343. throw GetCancellationException(url, cancellationToken, ex);
  344. }
  345. catch (HttpRequestException ex)
  346. {
  347. _logger.ErrorException("Error getting response from " + url, ex);
  348. throw new HttpException(ex.Message, ex);
  349. }
  350. finally
  351. {
  352. if (resourcePool != null)
  353. {
  354. resourcePool.Release();
  355. }
  356. }
  357. }
  358. /// <summary>
  359. /// Downloads the contents of a given url into a temporary location
  360. /// </summary>
  361. /// <param name="options">The options.</param>
  362. /// <returns>Task{System.String}.</returns>
  363. /// <exception cref="System.ArgumentNullException">progress</exception>
  364. /// <exception cref="HttpException"></exception>
  365. /// <exception cref="MediaBrowser.Model.Net.HttpException"></exception>
  366. public async Task<string> GetTempFile(HttpRequestOptions options)
  367. {
  368. ValidateParams(options.Url, options.CancellationToken);
  369. var tempFile = Path.Combine(_appPaths.TempDirectory, Guid.NewGuid() + ".tmp");
  370. if (options.Progress == null)
  371. {
  372. throw new ArgumentNullException("progress");
  373. }
  374. options.CancellationToken.ThrowIfCancellationRequested();
  375. if (options.ResourcePool != null)
  376. {
  377. await options.ResourcePool.WaitAsync(options.CancellationToken).ConfigureAwait(false);
  378. }
  379. options.Progress.Report(0);
  380. _logger.Info("HttpClientManager.GetTempFile url: {0}, temp file: {1}", options.Url, tempFile);
  381. try
  382. {
  383. options.CancellationToken.ThrowIfCancellationRequested();
  384. using (var response = await GetHttpClient(GetHostFromUrl(options.Url)).SendAsync(GetHttpRequestMessage(options), HttpCompletionOption.ResponseHeadersRead, options.CancellationToken).ConfigureAwait(false))
  385. {
  386. EnsureSuccessStatusCode(response);
  387. options.CancellationToken.ThrowIfCancellationRequested();
  388. var contentLength = GetContentLength(response);
  389. if (!contentLength.HasValue)
  390. {
  391. // We're not able to track progress
  392. using (var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false))
  393. {
  394. using (var fs = new FileStream(tempFile, FileMode.Create, FileAccess.Write, FileShare.Read, StreamDefaults.DefaultFileStreamBufferSize, FileOptions.Asynchronous))
  395. {
  396. await stream.CopyToAsync(fs, StreamDefaults.DefaultCopyToBufferSize, options.CancellationToken).ConfigureAwait(false);
  397. }
  398. }
  399. }
  400. else
  401. {
  402. using (var stream = ProgressStream.CreateReadProgressStream(await response.Content.ReadAsStreamAsync().ConfigureAwait(false), options.Progress.Report, contentLength.Value))
  403. {
  404. using (var fs = new FileStream(tempFile, FileMode.Create, FileAccess.Write, FileShare.Read, StreamDefaults.DefaultFileStreamBufferSize, FileOptions.Asynchronous))
  405. {
  406. await stream.CopyToAsync(fs, StreamDefaults.DefaultCopyToBufferSize, options.CancellationToken).ConfigureAwait(false);
  407. }
  408. }
  409. }
  410. options.Progress.Report(100);
  411. options.CancellationToken.ThrowIfCancellationRequested();
  412. }
  413. }
  414. catch (Exception ex)
  415. {
  416. HandleTempFileException(ex, options, tempFile);
  417. }
  418. finally
  419. {
  420. if (options.ResourcePool != null)
  421. {
  422. options.ResourcePool.Release();
  423. }
  424. }
  425. return tempFile;
  426. }
  427. /// <summary>
  428. /// Gets the message.
  429. /// </summary>
  430. /// <param name="options">The options.</param>
  431. /// <returns>HttpResponseMessage.</returns>
  432. private HttpRequestMessage GetHttpRequestMessage(HttpRequestOptions options)
  433. {
  434. var message = new HttpRequestMessage(HttpMethod.Get, options.Url);
  435. if (!string.IsNullOrEmpty(options.UserAgent))
  436. {
  437. message.Headers.Add("User-Agent", options.UserAgent);
  438. }
  439. if (!string.IsNullOrEmpty(options.AcceptHeader))
  440. {
  441. message.Headers.Add("Accept", options.AcceptHeader);
  442. }
  443. return message;
  444. }
  445. /// <summary>
  446. /// Gets the length of the content.
  447. /// </summary>
  448. /// <param name="response">The response.</param>
  449. /// <returns>System.Nullable{System.Int64}.</returns>
  450. private long? GetContentLength(HttpResponseMessage response)
  451. {
  452. IEnumerable<string> lengthValues;
  453. if (!response.Headers.TryGetValues("content-length", out lengthValues) && !response.Content.Headers.TryGetValues("content-length", out lengthValues))
  454. {
  455. return null;
  456. }
  457. return long.Parse(string.Join(string.Empty, lengthValues.ToArray()), UsCulture);
  458. }
  459. protected static readonly CultureInfo UsCulture = new CultureInfo("en-US");
  460. /// <summary>
  461. /// Handles the temp file exception.
  462. /// </summary>
  463. /// <param name="ex">The ex.</param>
  464. /// <param name="options">The options.</param>
  465. /// <param name="tempFile">The temp file.</param>
  466. /// <returns>Task.</returns>
  467. /// <exception cref="HttpException"></exception>
  468. private void HandleTempFileException(Exception ex, HttpRequestOptions options, string tempFile)
  469. {
  470. var operationCanceledException = ex as OperationCanceledException;
  471. if (operationCanceledException != null)
  472. {
  473. // Cleanup
  474. if (File.Exists(tempFile))
  475. {
  476. File.Delete(tempFile);
  477. }
  478. throw GetCancellationException(options.Url, options.CancellationToken, operationCanceledException);
  479. }
  480. _logger.ErrorException("Error getting response from " + options.Url, ex);
  481. var httpRequestException = ex as HttpRequestException;
  482. // Cleanup
  483. if (File.Exists(tempFile))
  484. {
  485. File.Delete(tempFile);
  486. }
  487. if (httpRequestException != null)
  488. {
  489. throw new HttpException(ex.Message, ex);
  490. }
  491. throw ex;
  492. }
  493. /// <summary>
  494. /// Validates the params.
  495. /// </summary>
  496. /// <param name="url">The URL.</param>
  497. /// <param name="cancellationToken">The cancellation token.</param>
  498. /// <exception cref="System.ArgumentNullException">url</exception>
  499. private void ValidateParams(string url, CancellationToken cancellationToken)
  500. {
  501. if (string.IsNullOrEmpty(url))
  502. {
  503. throw new ArgumentNullException("url");
  504. }
  505. if (cancellationToken == null)
  506. {
  507. throw new ArgumentNullException("cancellationToken");
  508. }
  509. }
  510. /// <summary>
  511. /// Gets the host from URL.
  512. /// </summary>
  513. /// <param name="url">The URL.</param>
  514. /// <returns>System.String.</returns>
  515. private string GetHostFromUrl(string url)
  516. {
  517. var start = url.IndexOf("://", StringComparison.OrdinalIgnoreCase) + 3;
  518. var len = url.IndexOf('/', start) - start;
  519. return url.Substring(start, len);
  520. }
  521. /// <summary>
  522. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  523. /// </summary>
  524. public void Dispose()
  525. {
  526. Dispose(true);
  527. GC.SuppressFinalize(this);
  528. }
  529. /// <summary>
  530. /// Releases unmanaged and - optionally - managed resources.
  531. /// </summary>
  532. /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  533. protected virtual void Dispose(bool dispose)
  534. {
  535. if (dispose)
  536. {
  537. foreach (var client in _httpClients.Values.ToList())
  538. {
  539. client.Dispose();
  540. }
  541. _httpClients.Clear();
  542. }
  543. }
  544. /// <summary>
  545. /// Throws the cancellation exception.
  546. /// </summary>
  547. /// <param name="url">The URL.</param>
  548. /// <param name="cancellationToken">The cancellation token.</param>
  549. /// <param name="exception">The exception.</param>
  550. /// <returns>Exception.</returns>
  551. private Exception GetCancellationException(string url, CancellationToken cancellationToken, OperationCanceledException exception)
  552. {
  553. // If the HttpClient's timeout is reached, it will cancel the Task internally
  554. if (!cancellationToken.IsCancellationRequested)
  555. {
  556. var msg = string.Format("Connection to {0} timed out", url);
  557. _logger.Error(msg);
  558. // Throw an HttpException so that the caller doesn't think it was cancelled by user code
  559. return new HttpException(msg, exception) { IsTimedOut = true };
  560. }
  561. return exception;
  562. }
  563. /// <summary>
  564. /// Ensures the success status code.
  565. /// </summary>
  566. /// <param name="response">The response.</param>
  567. /// <exception cref="MediaBrowser.Model.Net.HttpException"></exception>
  568. private void EnsureSuccessStatusCode(HttpResponseMessage response)
  569. {
  570. if (!response.IsSuccessStatusCode)
  571. {
  572. throw new HttpException(response.ReasonPhrase) { StatusCode = response.StatusCode };
  573. }
  574. }
  575. /// <summary>
  576. /// Posts the specified URL.
  577. /// </summary>
  578. /// <param name="url">The URL.</param>
  579. /// <param name="postData">The post data.</param>
  580. /// <param name="cancellationToken">The cancellation token.</param>
  581. /// <returns>Task{Stream}.</returns>
  582. public Task<Stream> Post(string url, Dictionary<string, string> postData, CancellationToken cancellationToken)
  583. {
  584. return Post(url, postData, null, cancellationToken);
  585. }
  586. }
  587. }