HttpClientManager.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. using MediaBrowser.Common.Configuration;
  2. using MediaBrowser.Common.IO;
  3. using MediaBrowser.Common.Net;
  4. using MediaBrowser.Model.Logging;
  5. using MediaBrowser.Model.Net;
  6. using System;
  7. using System.Collections.Concurrent;
  8. using System.Collections.Generic;
  9. using System.Globalization;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Net;
  13. using System.Net.Cache;
  14. using System.Net.Http;
  15. using System.Reflection;
  16. using System.Text;
  17. using System.Threading;
  18. using System.Threading.Tasks;
  19. namespace MediaBrowser.Common.Implementations.HttpClientManager
  20. {
  21. /// <summary>
  22. /// Class HttpClientManager
  23. /// </summary>
  24. public class HttpClientManager : IHttpClient
  25. {
  26. /// <summary>
  27. /// When one request to a host times out, we'll ban all other requests for this period of time, to prevent scans from stalling
  28. /// </summary>
  29. private const int TimeoutSeconds = 30;
  30. /// <summary>
  31. /// The _logger
  32. /// </summary>
  33. private readonly ILogger _logger;
  34. /// <summary>
  35. /// The _app paths
  36. /// </summary>
  37. private readonly IApplicationPaths _appPaths;
  38. private readonly IFileSystem _fileSystem;
  39. /// <summary>
  40. /// Initializes a new instance of the <see cref="HttpClientManager" /> class.
  41. /// </summary>
  42. /// <param name="appPaths">The app paths.</param>
  43. /// <param name="logger">The logger.</param>
  44. /// <param name="fileSystem">The file system.</param>
  45. /// <exception cref="System.ArgumentNullException">appPaths
  46. /// or
  47. /// logger</exception>
  48. public HttpClientManager(IApplicationPaths appPaths, ILogger logger, IFileSystem fileSystem)
  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. _fileSystem = fileSystem;
  60. _appPaths = appPaths;
  61. }
  62. /// <summary>
  63. /// Holds a dictionary of http clients by host. Use GetHttpClient(host) to retrieve or create a client for web requests.
  64. /// DON'T dispose it after use.
  65. /// </summary>
  66. /// <value>The HTTP clients.</value>
  67. private readonly ConcurrentDictionary<string, HttpClientInfo> _httpClients = new ConcurrentDictionary<string, HttpClientInfo>();
  68. /// <summary>
  69. /// Gets
  70. /// </summary>
  71. /// <param name="host">The host.</param>
  72. /// <param name="enableHttpCompression">if set to <c>true</c> [enable HTTP compression].</param>
  73. /// <returns>HttpClient.</returns>
  74. /// <exception cref="System.ArgumentNullException">host</exception>
  75. private HttpClientInfo GetHttpClient(string host, bool enableHttpCompression)
  76. {
  77. if (string.IsNullOrEmpty(host))
  78. {
  79. throw new ArgumentNullException("host");
  80. }
  81. HttpClientInfo client;
  82. var key = host + enableHttpCompression;
  83. if (!_httpClients.TryGetValue(key, out client))
  84. {
  85. client = new HttpClientInfo();
  86. _httpClients.TryAdd(key, client);
  87. }
  88. return client;
  89. }
  90. private WebRequest GetMonoRequest(HttpRequestOptions options, string method, bool enableHttpCompression)
  91. {
  92. var request = (HttpWebRequest)WebRequest.Create(options.Url);
  93. if (!string.IsNullOrEmpty(options.AcceptHeader))
  94. {
  95. request.Accept = options.AcceptHeader;
  96. }
  97. request.AutomaticDecompression = enableHttpCompression ? DecompressionMethods.Deflate : DecompressionMethods.None;
  98. request.CachePolicy = new RequestCachePolicy(RequestCacheLevel.Revalidate);
  99. request.ConnectionGroupName = GetHostFromUrl(options.Url);
  100. request.KeepAlive = true;
  101. request.Method = method;
  102. request.Pipelined = true;
  103. request.Timeout = 20000;
  104. if (!string.IsNullOrEmpty(options.UserAgent))
  105. {
  106. request.UserAgent = options.UserAgent;
  107. }
  108. return request;
  109. }
  110. private PropertyInfo _httpBehaviorPropertyInfo;
  111. private WebRequest GetRequest(HttpRequestOptions options, string method, bool enableHttpCompression)
  112. {
  113. #if __MonoCS__
  114. return GetMonoRequest(options, method, enableHttpCompression);
  115. #endif
  116. var request = HttpWebRequest.CreateHttp(options.Url);
  117. if (!string.IsNullOrEmpty(options.AcceptHeader))
  118. {
  119. request.Accept = options.AcceptHeader;
  120. }
  121. request.AutomaticDecompression = enableHttpCompression ? DecompressionMethods.Deflate : DecompressionMethods.None;
  122. request.CachePolicy = new RequestCachePolicy(RequestCacheLevel.Revalidate);
  123. request.ConnectionGroupName = GetHostFromUrl(options.Url);
  124. request.KeepAlive = true;
  125. request.Method = method;
  126. request.Pipelined = true;
  127. request.Timeout = 20000;
  128. if (!string.IsNullOrEmpty(options.UserAgent))
  129. {
  130. request.UserAgent = options.UserAgent;
  131. }
  132. // This is a hack to prevent KeepAlive from getting disabled internally by the HttpWebRequest
  133. // May need to remove this for mono
  134. var sp = request.ServicePoint;
  135. if (_httpBehaviorPropertyInfo == null)
  136. {
  137. _httpBehaviorPropertyInfo = sp.GetType().GetProperty("HttpBehaviour", BindingFlags.Instance | BindingFlags.NonPublic);
  138. }
  139. _httpBehaviorPropertyInfo.SetValue(sp, (byte)0, null);
  140. return request;
  141. }
  142. /// <summary>
  143. /// Gets the response internal.
  144. /// </summary>
  145. /// <param name="options">The options.</param>
  146. /// <returns>Task{HttpResponseInfo}.</returns>
  147. /// <exception cref="HttpException">
  148. /// </exception>
  149. public async Task<HttpResponseInfo> GetResponse(HttpRequestOptions options)
  150. {
  151. ValidateParams(options.Url, options.CancellationToken);
  152. options.CancellationToken.ThrowIfCancellationRequested();
  153. var client = GetHttpClient(GetHostFromUrl(options.Url), options.EnableHttpCompression);
  154. if ((DateTime.UtcNow - client.LastTimeout).TotalSeconds < TimeoutSeconds)
  155. {
  156. throw new HttpException(string.Format("Cancelling connection to {0} due to a previous timeout.", options.Url)) { IsTimedOut = true };
  157. }
  158. var httpWebRequest = GetRequest(options, "GET", options.EnableHttpCompression);
  159. if (options.ResourcePool != null)
  160. {
  161. await options.ResourcePool.WaitAsync(options.CancellationToken).ConfigureAwait(false);
  162. }
  163. if ((DateTime.UtcNow - client.LastTimeout).TotalSeconds < TimeoutSeconds)
  164. {
  165. if (options.ResourcePool != null)
  166. {
  167. options.ResourcePool.Release();
  168. }
  169. throw new HttpException(string.Format("Connection to {0} timed out", options.Url)) { IsTimedOut = true };
  170. }
  171. _logger.Info("HttpClientManager.GET url: {0}", options.Url);
  172. try
  173. {
  174. options.CancellationToken.ThrowIfCancellationRequested();
  175. using (var response = await httpWebRequest.GetResponseAsync().ConfigureAwait(false))
  176. {
  177. var httpResponse = (HttpWebResponse)response;
  178. EnsureSuccessStatusCode(httpResponse);
  179. options.CancellationToken.ThrowIfCancellationRequested();
  180. using (var stream = httpResponse.GetResponseStream())
  181. {
  182. var memoryStream = new MemoryStream();
  183. await stream.CopyToAsync(memoryStream).ConfigureAwait(false);
  184. memoryStream.Position = 0;
  185. return new HttpResponseInfo
  186. {
  187. Content = memoryStream,
  188. StatusCode = httpResponse.StatusCode,
  189. ContentType = httpResponse.ContentType
  190. };
  191. }
  192. }
  193. }
  194. catch (OperationCanceledException ex)
  195. {
  196. var exception = GetCancellationException(options.Url, options.CancellationToken, ex);
  197. var httpException = exception as HttpException;
  198. if (httpException != null && httpException.IsTimedOut)
  199. {
  200. client.LastTimeout = DateTime.UtcNow;
  201. }
  202. throw exception;
  203. }
  204. catch (HttpRequestException ex)
  205. {
  206. _logger.ErrorException("Error getting response from " + options.Url, ex);
  207. throw new HttpException(ex.Message, ex);
  208. }
  209. catch (WebException ex)
  210. {
  211. _logger.ErrorException("Error getting response from " + options.Url, ex);
  212. throw new HttpException(ex.Message, ex);
  213. }
  214. catch (Exception ex)
  215. {
  216. _logger.ErrorException("Error getting response from " + options.Url, ex);
  217. throw;
  218. }
  219. finally
  220. {
  221. if (options.ResourcePool != null)
  222. {
  223. options.ResourcePool.Release();
  224. }
  225. }
  226. }
  227. /// <summary>
  228. /// Performs a GET request and returns the resulting stream
  229. /// </summary>
  230. /// <param name="options">The options.</param>
  231. /// <returns>Task{Stream}.</returns>
  232. /// <exception cref="HttpException"></exception>
  233. /// <exception cref="MediaBrowser.Model.Net.HttpException"></exception>
  234. public async Task<Stream> Get(HttpRequestOptions options)
  235. {
  236. var response = await GetResponse(options).ConfigureAwait(false);
  237. return response.Content;
  238. }
  239. /// <summary>
  240. /// Performs a GET request and returns the resulting stream
  241. /// </summary>
  242. /// <param name="url">The URL.</param>
  243. /// <param name="resourcePool">The resource pool.</param>
  244. /// <param name="cancellationToken">The cancellation token.</param>
  245. /// <returns>Task{Stream}.</returns>
  246. public Task<Stream> Get(string url, SemaphoreSlim resourcePool, CancellationToken cancellationToken)
  247. {
  248. return Get(new HttpRequestOptions
  249. {
  250. Url = url,
  251. ResourcePool = resourcePool,
  252. CancellationToken = cancellationToken,
  253. });
  254. }
  255. /// <summary>
  256. /// Gets the specified URL.
  257. /// </summary>
  258. /// <param name="url">The URL.</param>
  259. /// <param name="cancellationToken">The cancellation token.</param>
  260. /// <returns>Task{Stream}.</returns>
  261. public Task<Stream> Get(string url, CancellationToken cancellationToken)
  262. {
  263. return Get(url, null, cancellationToken);
  264. }
  265. /// <summary>
  266. /// Performs a POST request
  267. /// </summary>
  268. /// <param name="options">The options.</param>
  269. /// <param name="postData">Params to add to the POST data.</param>
  270. /// <returns>stream on success, null on failure</returns>
  271. /// <exception cref="HttpException">
  272. /// </exception>
  273. /// <exception cref="System.ArgumentNullException">postData</exception>
  274. /// <exception cref="MediaBrowser.Model.Net.HttpException"></exception>
  275. public async Task<Stream> Post(HttpRequestOptions options, Dictionary<string, string> postData)
  276. {
  277. ValidateParams(options.Url, options.CancellationToken);
  278. options.CancellationToken.ThrowIfCancellationRequested();
  279. var httpWebRequest = GetRequest(options, "POST", options.EnableHttpCompression);
  280. var strings = postData.Keys.Select(key => string.Format("{0}={1}", key, postData[key]));
  281. var postContent = string.Join("&", strings.ToArray());
  282. var bytes = Encoding.UTF8.GetBytes(postContent);
  283. httpWebRequest.ContentType = "application/x-www-form-urlencoded";
  284. httpWebRequest.ContentLength = bytes.Length;
  285. httpWebRequest.GetRequestStream().Write(bytes, 0, bytes.Length);
  286. if (options.ResourcePool != null)
  287. {
  288. await options.ResourcePool.WaitAsync(options.CancellationToken).ConfigureAwait(false);
  289. }
  290. _logger.Info("HttpClientManager.POST url: {0}", options.Url);
  291. try
  292. {
  293. options.CancellationToken.ThrowIfCancellationRequested();
  294. using (var response = await httpWebRequest.GetResponseAsync().ConfigureAwait(false))
  295. {
  296. var httpResponse = (HttpWebResponse)response;
  297. EnsureSuccessStatusCode(httpResponse);
  298. options.CancellationToken.ThrowIfCancellationRequested();
  299. using (var stream = httpResponse.GetResponseStream())
  300. {
  301. var memoryStream = new MemoryStream();
  302. await stream.CopyToAsync(memoryStream).ConfigureAwait(false);
  303. memoryStream.Position = 0;
  304. return memoryStream;
  305. }
  306. }
  307. }
  308. catch (OperationCanceledException ex)
  309. {
  310. var exception = GetCancellationException(options.Url, options.CancellationToken, ex);
  311. throw exception;
  312. }
  313. catch (HttpRequestException ex)
  314. {
  315. _logger.ErrorException("Error getting response from " + options.Url, ex);
  316. throw new HttpException(ex.Message, ex);
  317. }
  318. catch (WebException ex)
  319. {
  320. _logger.ErrorException("Error getting response from " + options.Url, ex);
  321. throw new HttpException(ex.Message, ex);
  322. }
  323. catch (Exception ex)
  324. {
  325. _logger.ErrorException("Error getting response from " + options.Url, ex);
  326. throw;
  327. }
  328. finally
  329. {
  330. if (options.ResourcePool != null)
  331. {
  332. options.ResourcePool.Release();
  333. }
  334. }
  335. }
  336. /// <summary>
  337. /// Performs a POST request
  338. /// </summary>
  339. /// <param name="url">The URL.</param>
  340. /// <param name="postData">Params to add to the POST data.</param>
  341. /// <param name="resourcePool">The resource pool.</param>
  342. /// <param name="cancellationToken">The cancellation token.</param>
  343. /// <returns>stream on success, null on failure</returns>
  344. public Task<Stream> Post(string url, Dictionary<string, string> postData, SemaphoreSlim resourcePool, CancellationToken cancellationToken)
  345. {
  346. return Post(new HttpRequestOptions
  347. {
  348. Url = url,
  349. ResourcePool = resourcePool,
  350. CancellationToken = cancellationToken
  351. }, postData);
  352. }
  353. /// <summary>
  354. /// Downloads the contents of a given url into a temporary location
  355. /// </summary>
  356. /// <param name="options">The options.</param>
  357. /// <returns>Task{System.String}.</returns>
  358. /// <exception cref="System.ArgumentNullException">progress</exception>
  359. /// <exception cref="HttpException"></exception>
  360. /// <exception cref="MediaBrowser.Model.Net.HttpException"></exception>
  361. public async Task<string> GetTempFile(HttpRequestOptions options)
  362. {
  363. var response = await GetTempFileResponse(options).ConfigureAwait(false);
  364. return response.TempFilePath;
  365. }
  366. public async Task<HttpResponseInfo> GetTempFileResponse(HttpRequestOptions options)
  367. {
  368. ValidateParams(options.Url, options.CancellationToken);
  369. Directory.CreateDirectory(_appPaths.TempDirectory);
  370. var tempFile = Path.Combine(_appPaths.TempDirectory, Guid.NewGuid() + ".tmp");
  371. if (options.Progress == null)
  372. {
  373. throw new ArgumentNullException("progress");
  374. }
  375. options.CancellationToken.ThrowIfCancellationRequested();
  376. var httpWebRequest = GetRequest(options, "GET", options.EnableHttpCompression);
  377. if (options.ResourcePool != null)
  378. {
  379. await options.ResourcePool.WaitAsync(options.CancellationToken).ConfigureAwait(false);
  380. }
  381. options.Progress.Report(0);
  382. _logger.Info("HttpClientManager.GetTempFileResponse url: {0}", options.Url);
  383. try
  384. {
  385. options.CancellationToken.ThrowIfCancellationRequested();
  386. using (var response = await httpWebRequest.GetResponseAsync().ConfigureAwait(false))
  387. {
  388. var httpResponse = (HttpWebResponse)response;
  389. EnsureSuccessStatusCode(httpResponse);
  390. options.CancellationToken.ThrowIfCancellationRequested();
  391. var contentLength = GetContentLength(httpResponse);
  392. if (!contentLength.HasValue)
  393. {
  394. // We're not able to track progress
  395. using (var stream = httpResponse.GetResponseStream())
  396. {
  397. using (var fs = _fileSystem.GetFileStream(tempFile, FileMode.Create, FileAccess.Write, FileShare.Read, true))
  398. {
  399. await stream.CopyToAsync(fs, StreamDefaults.DefaultCopyToBufferSize, options.CancellationToken).ConfigureAwait(false);
  400. }
  401. }
  402. }
  403. else
  404. {
  405. using (var stream = ProgressStream.CreateReadProgressStream(httpResponse.GetResponseStream(), options.Progress.Report, contentLength.Value))
  406. {
  407. using (var fs = _fileSystem.GetFileStream(tempFile, FileMode.Create, FileAccess.Write, FileShare.Read, true))
  408. {
  409. await stream.CopyToAsync(fs, StreamDefaults.DefaultCopyToBufferSize, options.CancellationToken).ConfigureAwait(false);
  410. }
  411. }
  412. }
  413. options.Progress.Report(100);
  414. return new HttpResponseInfo
  415. {
  416. TempFilePath = tempFile,
  417. StatusCode = httpResponse.StatusCode,
  418. ContentType = httpResponse.ContentType
  419. };
  420. }
  421. }
  422. catch (OperationCanceledException ex)
  423. {
  424. throw GetTempFileException(ex, options, tempFile);
  425. }
  426. catch (HttpRequestException ex)
  427. {
  428. throw GetTempFileException(ex, options, tempFile);
  429. }
  430. catch (WebException ex)
  431. {
  432. throw GetTempFileException(ex, options, tempFile);
  433. }
  434. catch (Exception ex)
  435. {
  436. throw GetTempFileException(ex, options, tempFile);
  437. }
  438. finally
  439. {
  440. if (options.ResourcePool != null)
  441. {
  442. options.ResourcePool.Release();
  443. }
  444. }
  445. }
  446. private long? GetContentLength(HttpWebResponse response)
  447. {
  448. var length = response.ContentLength;
  449. if (length == 0)
  450. {
  451. return null;
  452. }
  453. return length;
  454. }
  455. protected static readonly CultureInfo UsCulture = new CultureInfo("en-US");
  456. /// <summary>
  457. /// Handles the temp file exception.
  458. /// </summary>
  459. /// <param name="ex">The ex.</param>
  460. /// <param name="options">The options.</param>
  461. /// <param name="tempFile">The temp file.</param>
  462. /// <returns>Task.</returns>
  463. /// <exception cref="HttpException"></exception>
  464. private Exception GetTempFileException(Exception ex, HttpRequestOptions options, string tempFile)
  465. {
  466. var operationCanceledException = ex as OperationCanceledException;
  467. if (operationCanceledException != null)
  468. {
  469. // Cleanup
  470. DeleteTempFile(tempFile);
  471. return GetCancellationException(options.Url, options.CancellationToken, operationCanceledException);
  472. }
  473. _logger.ErrorException("Error getting response from " + options.Url, ex);
  474. // Cleanup
  475. DeleteTempFile(tempFile);
  476. var httpRequestException = ex as HttpRequestException;
  477. if (httpRequestException != null)
  478. {
  479. return new HttpException(ex.Message, ex);
  480. }
  481. var webException = ex as WebException;
  482. if (webException != null)
  483. {
  484. return new HttpException(ex.Message, ex);
  485. }
  486. return ex;
  487. }
  488. private void DeleteTempFile(string file)
  489. {
  490. try
  491. {
  492. File.Delete(file);
  493. }
  494. catch (IOException)
  495. {
  496. // Might not have been created at all. No need to worry.
  497. }
  498. }
  499. /// <summary>
  500. /// Validates the params.
  501. /// </summary>
  502. /// <param name="url">The URL.</param>
  503. /// <param name="cancellationToken">The cancellation token.</param>
  504. /// <exception cref="System.ArgumentNullException">url</exception>
  505. private void ValidateParams(string url, CancellationToken cancellationToken)
  506. {
  507. if (string.IsNullOrEmpty(url))
  508. {
  509. throw new ArgumentNullException("url");
  510. }
  511. }
  512. /// <summary>
  513. /// Gets the host from URL.
  514. /// </summary>
  515. /// <param name="url">The URL.</param>
  516. /// <returns>System.String.</returns>
  517. private string GetHostFromUrl(string url)
  518. {
  519. var start = url.IndexOf("://", StringComparison.OrdinalIgnoreCase) + 3;
  520. var len = url.IndexOf('/', start) - start;
  521. return url.Substring(start, len);
  522. }
  523. /// <summary>
  524. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  525. /// </summary>
  526. public void Dispose()
  527. {
  528. Dispose(true);
  529. GC.SuppressFinalize(this);
  530. }
  531. /// <summary>
  532. /// Releases unmanaged and - optionally - managed resources.
  533. /// </summary>
  534. /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  535. protected virtual void Dispose(bool dispose)
  536. {
  537. if (dispose)
  538. {
  539. _httpClients.Clear();
  540. }
  541. }
  542. /// <summary>
  543. /// Throws the cancellation exception.
  544. /// </summary>
  545. /// <param name="url">The URL.</param>
  546. /// <param name="cancellationToken">The cancellation token.</param>
  547. /// <param name="exception">The exception.</param>
  548. /// <returns>Exception.</returns>
  549. private Exception GetCancellationException(string url, CancellationToken cancellationToken, OperationCanceledException exception)
  550. {
  551. // If the HttpClient's timeout is reached, it will cancel the Task internally
  552. if (!cancellationToken.IsCancellationRequested)
  553. {
  554. var msg = string.Format("Connection to {0} timed out", url);
  555. _logger.Error(msg);
  556. // Throw an HttpException so that the caller doesn't think it was cancelled by user code
  557. return new HttpException(msg, exception) { IsTimedOut = true };
  558. }
  559. return exception;
  560. }
  561. private void EnsureSuccessStatusCode(HttpWebResponse response)
  562. {
  563. var statusCode = response.StatusCode;
  564. var isSuccessful = statusCode >= HttpStatusCode.OK && statusCode <= (HttpStatusCode)299;
  565. if (!isSuccessful)
  566. {
  567. throw new HttpException(response.StatusDescription) { StatusCode = response.StatusCode };
  568. }
  569. }
  570. /// <summary>
  571. /// Posts the specified URL.
  572. /// </summary>
  573. /// <param name="url">The URL.</param>
  574. /// <param name="postData">The post data.</param>
  575. /// <param name="cancellationToken">The cancellation token.</param>
  576. /// <returns>Task{Stream}.</returns>
  577. public Task<Stream> Post(string url, Dictionary<string, string> postData, CancellationToken cancellationToken)
  578. {
  579. return Post(url, postData, null, cancellationToken);
  580. }
  581. }
  582. }