HttpClientManager.cs 25 KB

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