HttpClientManager.cs 25 KB

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