HttpClientManager.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  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. private 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 (!string.IsNullOrEmpty(options.RequestContent) || string.Equals(httpMethod, "post", StringComparison.OrdinalIgnoreCase))
  201. {
  202. var content = options.RequestContent ?? string.Empty;
  203. var bytes = Encoding.UTF8.GetBytes(content);
  204. httpWebRequest.ContentType = options.RequestContentType ?? "application/x-www-form-urlencoded";
  205. httpWebRequest.ContentLength = bytes.Length;
  206. httpWebRequest.GetRequestStream().Write(bytes, 0, bytes.Length);
  207. }
  208. if (options.ResourcePool != null)
  209. {
  210. await options.ResourcePool.WaitAsync(options.CancellationToken).ConfigureAwait(false);
  211. }
  212. if ((DateTime.UtcNow - client.LastTimeout).TotalSeconds < TimeoutSeconds)
  213. {
  214. if (options.ResourcePool != null)
  215. {
  216. options.ResourcePool.Release();
  217. }
  218. throw new HttpException(string.Format("Connection to {0} timed out", options.Url)) { IsTimedOut = true };
  219. }
  220. if (options.LogRequest)
  221. {
  222. _logger.Info("HttpClientManager {0}: {1}", httpMethod.ToUpper(), options.Url);
  223. }
  224. try
  225. {
  226. options.CancellationToken.ThrowIfCancellationRequested();
  227. if (!options.BufferContent)
  228. {
  229. var response = await httpWebRequest.GetResponseAsync().ConfigureAwait(false);
  230. var httpResponse = (HttpWebResponse)response;
  231. EnsureSuccessStatusCode(httpResponse, options);
  232. options.CancellationToken.ThrowIfCancellationRequested();
  233. return GetResponseInfo(httpResponse, httpResponse.GetResponseStream(), GetContentLength(httpResponse));
  234. }
  235. using (var response = await httpWebRequest.GetResponseAsync().ConfigureAwait(false))
  236. {
  237. var httpResponse = (HttpWebResponse)response;
  238. EnsureSuccessStatusCode(httpResponse, options);
  239. options.CancellationToken.ThrowIfCancellationRequested();
  240. using (var stream = httpResponse.GetResponseStream())
  241. {
  242. var memoryStream = new MemoryStream();
  243. await stream.CopyToAsync(memoryStream).ConfigureAwait(false);
  244. memoryStream.Position = 0;
  245. return GetResponseInfo(httpResponse, memoryStream, memoryStream.Length);
  246. }
  247. }
  248. }
  249. catch (OperationCanceledException ex)
  250. {
  251. var exception = GetCancellationException(options.Url, options.CancellationToken, ex);
  252. var httpException = exception as HttpException;
  253. if (httpException != null && httpException.IsTimedOut)
  254. {
  255. client.LastTimeout = DateTime.UtcNow;
  256. }
  257. throw exception;
  258. }
  259. catch (HttpRequestException ex)
  260. {
  261. _logger.ErrorException("Error getting response from " + options.Url, ex);
  262. throw new HttpException(ex.Message, ex);
  263. }
  264. catch (WebException ex)
  265. {
  266. throw GetException(ex, options);
  267. }
  268. catch (Exception ex)
  269. {
  270. _logger.ErrorException("Error getting response from " + options.Url, ex);
  271. throw;
  272. }
  273. finally
  274. {
  275. if (options.ResourcePool != null)
  276. {
  277. options.ResourcePool.Release();
  278. }
  279. }
  280. }
  281. /// <summary>
  282. /// Gets the exception.
  283. /// </summary>
  284. /// <param name="ex">The ex.</param>
  285. /// <param name="options">The options.</param>
  286. /// <returns>HttpException.</returns>
  287. private HttpException GetException(WebException ex, HttpRequestOptions options)
  288. {
  289. _logger.ErrorException("Error getting response from " + options.Url, ex);
  290. return new HttpException(ex.Message, ex);
  291. }
  292. private HttpResponseInfo GetResponseInfo(HttpWebResponse httpResponse, Stream content, long? contentLength)
  293. {
  294. return new HttpResponseInfo
  295. {
  296. Content = content,
  297. StatusCode = httpResponse.StatusCode,
  298. ContentType = httpResponse.ContentType,
  299. Headers = new NameValueCollection(httpResponse.Headers),
  300. ContentLength = contentLength
  301. };
  302. }
  303. private HttpResponseInfo GetResponseInfo(HttpWebResponse httpResponse, string tempFile, long? contentLength)
  304. {
  305. return new HttpResponseInfo
  306. {
  307. TempFilePath = tempFile,
  308. StatusCode = httpResponse.StatusCode,
  309. ContentType = httpResponse.ContentType,
  310. Headers = httpResponse.Headers,
  311. ContentLength = contentLength
  312. };
  313. }
  314. public Task<HttpResponseInfo> Post(HttpRequestOptions options)
  315. {
  316. return SendAsync(options, "POST");
  317. }
  318. /// <summary>
  319. /// Performs a POST request
  320. /// </summary>
  321. /// <param name="options">The options.</param>
  322. /// <param name="postData">Params to add to the POST data.</param>
  323. /// <returns>stream on success, null on failure</returns>
  324. public async Task<Stream> Post(HttpRequestOptions options, Dictionary<string, string> postData)
  325. {
  326. var strings = postData.Keys.Select(key => string.Format("{0}={1}", key, postData[key]));
  327. var postContent = string.Join("&", strings.ToArray());
  328. options.RequestContent = postContent;
  329. options.RequestContentType = "application/x-www-form-urlencoded";
  330. var response = await Post(options).ConfigureAwait(false);
  331. return response.Content;
  332. }
  333. /// <summary>
  334. /// Performs a POST request
  335. /// </summary>
  336. /// <param name="url">The URL.</param>
  337. /// <param name="postData">Params to add to the POST data.</param>
  338. /// <param name="resourcePool">The resource pool.</param>
  339. /// <param name="cancellationToken">The cancellation token.</param>
  340. /// <returns>stream on success, null on failure</returns>
  341. public Task<Stream> Post(string url, Dictionary<string, string> postData, SemaphoreSlim resourcePool, CancellationToken cancellationToken)
  342. {
  343. return Post(new HttpRequestOptions
  344. {
  345. Url = url,
  346. ResourcePool = resourcePool,
  347. CancellationToken = cancellationToken
  348. }, postData);
  349. }
  350. /// <summary>
  351. /// Downloads the contents of a given url into a temporary location
  352. /// </summary>
  353. /// <param name="options">The options.</param>
  354. /// <returns>Task{System.String}.</returns>
  355. /// <exception cref="System.ArgumentNullException">progress</exception>
  356. public async Task<string> GetTempFile(HttpRequestOptions options)
  357. {
  358. var response = await GetTempFileResponse(options).ConfigureAwait(false);
  359. return response.TempFilePath;
  360. }
  361. public async Task<HttpResponseInfo> GetTempFileResponse(HttpRequestOptions options)
  362. {
  363. ValidateParams(options);
  364. Directory.CreateDirectory(_appPaths.TempDirectory);
  365. var tempFile = Path.Combine(_appPaths.TempDirectory, Guid.NewGuid() + ".tmp");
  366. if (options.Progress == null)
  367. {
  368. throw new ArgumentNullException("progress");
  369. }
  370. options.CancellationToken.ThrowIfCancellationRequested();
  371. var httpWebRequest = GetRequest(options, "GET", options.EnableHttpCompression);
  372. if (options.ResourcePool != null)
  373. {
  374. await options.ResourcePool.WaitAsync(options.CancellationToken).ConfigureAwait(false);
  375. }
  376. options.Progress.Report(0);
  377. if (options.LogRequest)
  378. {
  379. _logger.Info("HttpClientManager.GetTempFileResponse url: {0}", options.Url);
  380. }
  381. try
  382. {
  383. options.CancellationToken.ThrowIfCancellationRequested();
  384. using (var response = await httpWebRequest.GetResponseAsync().ConfigureAwait(false))
  385. {
  386. var httpResponse = (HttpWebResponse)response;
  387. EnsureSuccessStatusCode(httpResponse, options);
  388. options.CancellationToken.ThrowIfCancellationRequested();
  389. var contentLength = GetContentLength(httpResponse);
  390. if (!contentLength.HasValue)
  391. {
  392. // We're not able to track progress
  393. using (var stream = httpResponse.GetResponseStream())
  394. {
  395. using (var fs = _fileSystem.GetFileStream(tempFile, FileMode.Create, FileAccess.Write, FileShare.Read, true))
  396. {
  397. await stream.CopyToAsync(fs, StreamDefaults.DefaultCopyToBufferSize, options.CancellationToken).ConfigureAwait(false);
  398. }
  399. }
  400. }
  401. else
  402. {
  403. using (var stream = ProgressStream.CreateReadProgressStream(httpResponse.GetResponseStream(), options.Progress.Report, contentLength.Value))
  404. {
  405. using (var fs = _fileSystem.GetFileStream(tempFile, FileMode.Create, FileAccess.Write, FileShare.Read, true))
  406. {
  407. await stream.CopyToAsync(fs, StreamDefaults.DefaultCopyToBufferSize, options.CancellationToken).ConfigureAwait(false);
  408. }
  409. }
  410. }
  411. options.Progress.Report(100);
  412. return GetResponseInfo(httpResponse, tempFile, contentLength);
  413. }
  414. }
  415. catch (OperationCanceledException ex)
  416. {
  417. throw GetTempFileException(ex, options, tempFile);
  418. }
  419. catch (HttpRequestException ex)
  420. {
  421. throw GetTempFileException(ex, options, tempFile);
  422. }
  423. catch (WebException ex)
  424. {
  425. throw GetTempFileException(ex, options, tempFile);
  426. }
  427. catch (Exception ex)
  428. {
  429. throw GetTempFileException(ex, options, tempFile);
  430. }
  431. finally
  432. {
  433. if (options.ResourcePool != null)
  434. {
  435. options.ResourcePool.Release();
  436. }
  437. }
  438. }
  439. private long? GetContentLength(HttpWebResponse response)
  440. {
  441. var length = response.ContentLength;
  442. if (length == 0)
  443. {
  444. return null;
  445. }
  446. return length;
  447. }
  448. protected static readonly CultureInfo UsCulture = new CultureInfo("en-US");
  449. /// <summary>
  450. /// Handles the temp file exception.
  451. /// </summary>
  452. /// <param name="ex">The ex.</param>
  453. /// <param name="options">The options.</param>
  454. /// <param name="tempFile">The temp file.</param>
  455. /// <returns>Task.</returns>
  456. /// <exception cref="HttpException"></exception>
  457. private Exception GetTempFileException(Exception ex, HttpRequestOptions options, string tempFile)
  458. {
  459. var operationCanceledException = ex as OperationCanceledException;
  460. if (operationCanceledException != null)
  461. {
  462. // Cleanup
  463. DeleteTempFile(tempFile);
  464. return GetCancellationException(options.Url, options.CancellationToken, operationCanceledException);
  465. }
  466. _logger.ErrorException("Error getting response from " + options.Url, ex);
  467. // Cleanup
  468. DeleteTempFile(tempFile);
  469. var httpRequestException = ex as HttpRequestException;
  470. if (httpRequestException != null)
  471. {
  472. return new HttpException(ex.Message, ex);
  473. }
  474. var webException = ex as WebException;
  475. if (webException != null)
  476. {
  477. throw GetException(webException, options);
  478. }
  479. return ex;
  480. }
  481. private void DeleteTempFile(string file)
  482. {
  483. try
  484. {
  485. File.Delete(file);
  486. }
  487. catch (IOException)
  488. {
  489. // Might not have been created at all. No need to worry.
  490. }
  491. }
  492. private void ValidateParams(HttpRequestOptions options)
  493. {
  494. if (string.IsNullOrEmpty(options.Url))
  495. {
  496. throw new ArgumentNullException("options");
  497. }
  498. }
  499. /// <summary>
  500. /// Gets the host from URL.
  501. /// </summary>
  502. /// <param name="url">The URL.</param>
  503. /// <returns>System.String.</returns>
  504. private string GetHostFromUrl(string url)
  505. {
  506. var start = url.IndexOf("://", StringComparison.OrdinalIgnoreCase) + 3;
  507. var len = url.IndexOf('/', start) - start;
  508. return url.Substring(start, len);
  509. }
  510. /// <summary>
  511. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  512. /// </summary>
  513. public void Dispose()
  514. {
  515. Dispose(true);
  516. GC.SuppressFinalize(this);
  517. }
  518. /// <summary>
  519. /// Releases unmanaged and - optionally - managed resources.
  520. /// </summary>
  521. /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  522. protected virtual void Dispose(bool dispose)
  523. {
  524. if (dispose)
  525. {
  526. _httpClients.Clear();
  527. }
  528. }
  529. /// <summary>
  530. /// Throws the cancellation exception.
  531. /// </summary>
  532. /// <param name="url">The URL.</param>
  533. /// <param name="cancellationToken">The cancellation token.</param>
  534. /// <param name="exception">The exception.</param>
  535. /// <returns>Exception.</returns>
  536. private Exception GetCancellationException(string url, CancellationToken cancellationToken, OperationCanceledException exception)
  537. {
  538. // If the HttpClient's timeout is reached, it will cancel the Task internally
  539. if (!cancellationToken.IsCancellationRequested)
  540. {
  541. var msg = string.Format("Connection to {0} timed out", url);
  542. _logger.Error(msg);
  543. // Throw an HttpException so that the caller doesn't think it was cancelled by user code
  544. return new HttpException(msg, exception) { IsTimedOut = true };
  545. }
  546. return exception;
  547. }
  548. private void EnsureSuccessStatusCode(HttpWebResponse response, HttpRequestOptions options)
  549. {
  550. var statusCode = response.StatusCode;
  551. var isSuccessful = statusCode >= HttpStatusCode.OK && statusCode <= (HttpStatusCode)299;
  552. if (!isSuccessful)
  553. {
  554. if (options.LogErrorResponseBody)
  555. {
  556. try
  557. {
  558. using (var stream = response.GetResponseStream())
  559. {
  560. if (stream != null)
  561. {
  562. using (var reader = new StreamReader(stream))
  563. {
  564. var msg = reader.ReadToEnd();
  565. _logger.Error(msg);
  566. }
  567. }
  568. }
  569. }
  570. catch
  571. {
  572. }
  573. }
  574. throw new HttpException(response.StatusDescription) { StatusCode = response.StatusCode };
  575. }
  576. }
  577. /// <summary>
  578. /// Posts the specified URL.
  579. /// </summary>
  580. /// <param name="url">The URL.</param>
  581. /// <param name="postData">The post data.</param>
  582. /// <param name="cancellationToken">The cancellation token.</param>
  583. /// <returns>Task{Stream}.</returns>
  584. public Task<Stream> Post(string url, Dictionary<string, string> postData, CancellationToken cancellationToken)
  585. {
  586. return Post(url, postData, null, cancellationToken);
  587. }
  588. }
  589. }