2
0

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