PageRenderTime 114ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/src/System.Net.Http.Formatting/HttpClientExtensions.cs

http://aspnetwebstack.codeplex.com
C# | 699 lines | 202 code | 45 blank | 452 comment | 8 complexity | 8468b1240c34c3123e20c3cb21555578 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
  2. using System.ComponentModel;
  3. using System.Diagnostics.CodeAnalysis;
  4. using System.Net.Http.Formatting;
  5. using System.Net.Http.Headers;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using System.Web.Http;
  9. namespace System.Net.Http
  10. {
  11. /// <summary>
  12. /// Extension methods that aid in making formatted requests using <see cref="HttpClient"/>.
  13. /// </summary>
  14. [EditorBrowsable(EditorBrowsableState.Never)]
  15. public static class HttpClientExtensions
  16. {
  17. /// <summary>
  18. /// Sends a POST request as an asynchronous operation to the specified Uri with the given <paramref name="value"/> serialized
  19. /// as JSON.
  20. /// </summary>
  21. /// <remarks>
  22. /// This method uses the default instance of <see cref="JsonMediaTypeFormatter"/>.
  23. /// </remarks>
  24. /// <typeparam name="T">The type of <paramref name="value"/>.</typeparam>
  25. /// <param name="client">The client used to make the request.</param>
  26. /// <param name="requestUri">The Uri the request is sent to.</param>
  27. /// <param name="value">The value that will be placed in the request's entity body.</param>
  28. /// <returns>A task object representing the asynchronous operation.</returns>
  29. [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "1#", Justification = "We want to support URIs as strings")]
  30. public static Task<HttpResponseMessage> PostAsJsonAsync<T>(this HttpClient client, string requestUri, T value)
  31. {
  32. return client.PostAsJsonAsync(requestUri, value, CancellationToken.None);
  33. }
  34. /// <summary>
  35. /// Sends a POST request as an asynchronous operation to the specified Uri with the given <paramref name="value"/> serialized
  36. /// as JSON.
  37. /// </summary>
  38. /// <remarks>
  39. /// This method uses the default instance of <see cref="JsonMediaTypeFormatter"/>.
  40. /// </remarks>
  41. /// <typeparam name="T">The type of <paramref name="value"/>.</typeparam>
  42. /// <param name="client">The client used to make the request.</param>
  43. /// <param name="requestUri">The Uri the request is sent to.</param>
  44. /// <param name="value">The value that will be placed in the request's entity body.</param>
  45. /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
  46. /// <returns>A task object representing the asynchronous operation.</returns>
  47. [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "1#", Justification = "We want to support URIs as strings")]
  48. public static Task<HttpResponseMessage> PostAsJsonAsync<T>(this HttpClient client, string requestUri, T value, CancellationToken cancellationToken)
  49. {
  50. return client.PostAsync(requestUri, value, new JsonMediaTypeFormatter(), cancellationToken);
  51. }
  52. /// <summary>
  53. /// Sends a POST request as an asynchronous operation to the specified Uri with the given <paramref name="value"/> serialized
  54. /// as JSON.
  55. /// </summary>
  56. /// <remarks>
  57. /// This method uses the default instance of <see cref="JsonMediaTypeFormatter"/>.
  58. /// </remarks>
  59. /// <typeparam name="T">The type of <paramref name="value"/>.</typeparam>
  60. /// <param name="client">The client used to make the request.</param>
  61. /// <param name="requestUri">The Uri the request is sent to.</param>
  62. /// <param name="value">The value that will be placed in the request's entity body.</param>
  63. /// <returns>A task object representing the asynchronous operation.</returns>
  64. public static Task<HttpResponseMessage> PostAsJsonAsync<T>(this HttpClient client, Uri requestUri, T value)
  65. {
  66. return client.PostAsJsonAsync(requestUri, value, CancellationToken.None);
  67. }
  68. /// <summary>
  69. /// Sends a POST request as an asynchronous operation to the specified Uri with the given <paramref name="value"/> serialized
  70. /// as JSON.
  71. /// </summary>
  72. /// <remarks>
  73. /// This method uses the default instance of <see cref="JsonMediaTypeFormatter"/>.
  74. /// </remarks>
  75. /// <typeparam name="T">The type of <paramref name="value"/>.</typeparam>
  76. /// <param name="client">The client used to make the request.</param>
  77. /// <param name="requestUri">The Uri the request is sent to.</param>
  78. /// <param name="value">The value that will be placed in the request's entity body.</param>
  79. /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
  80. /// <returns>A task object representing the asynchronous operation.</returns>
  81. public static Task<HttpResponseMessage> PostAsJsonAsync<T>(this HttpClient client, Uri requestUri, T value, CancellationToken cancellationToken)
  82. {
  83. return client.PostAsync(requestUri, value, new JsonMediaTypeFormatter(), cancellationToken);
  84. }
  85. /// <summary>
  86. /// Sends a POST request as an asynchronous operation to the specified Uri with the given <paramref name="value"/> serialized
  87. /// as XML.
  88. /// </summary>
  89. /// <remarks>
  90. /// This method uses the default instance of <see cref="XmlMediaTypeFormatter"/>.
  91. /// </remarks>
  92. /// <typeparam name="T">The type of <paramref name="value"/>.</typeparam>
  93. /// <param name="client">The client used to make the request.</param>
  94. /// <param name="requestUri">The Uri the request is sent to.</param>
  95. /// <param name="value">The value that will be placed in the request's entity body.</param>
  96. /// <returns>A task object representing the asynchronous operation.</returns>
  97. [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "1#", Justification = "We want to support URIs as strings")]
  98. public static Task<HttpResponseMessage> PostAsXmlAsync<T>(this HttpClient client, string requestUri, T value)
  99. {
  100. return client.PostAsXmlAsync(requestUri, value, CancellationToken.None);
  101. }
  102. /// <summary>
  103. /// Sends a POST request as an asynchronous operation to the specified Uri with the given <paramref name="value"/> serialized
  104. /// as XML.
  105. /// </summary>
  106. /// <remarks>
  107. /// This method uses the default instance of <see cref="XmlMediaTypeFormatter"/>.
  108. /// </remarks>
  109. /// <typeparam name="T">The type of <paramref name="value"/>.</typeparam>
  110. /// <param name="client">The client used to make the request.</param>
  111. /// <param name="requestUri">The Uri the request is sent to.</param>
  112. /// <param name="value">The value that will be placed in the request's entity body.</param>
  113. /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
  114. /// <returns>A task object representing the asynchronous operation.</returns>
  115. [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "1#", Justification = "We want to support URIs as strings")]
  116. public static Task<HttpResponseMessage> PostAsXmlAsync<T>(this HttpClient client, string requestUri, T value, CancellationToken cancellationToken)
  117. {
  118. return client.PostAsync(requestUri, value, new XmlMediaTypeFormatter(), cancellationToken);
  119. }
  120. /// <summary>
  121. /// Sends a POST request as an asynchronous operation to the specified Uri with the given <paramref name="value"/> serialized
  122. /// as XML.
  123. /// </summary>
  124. /// <remarks>
  125. /// This method uses the default instance of <see cref="XmlMediaTypeFormatter"/>.
  126. /// </remarks>
  127. /// <typeparam name="T">The type of <paramref name="value"/>.</typeparam>
  128. /// <param name="client">The client used to make the request.</param>
  129. /// <param name="requestUri">The Uri the request is sent to.</param>
  130. /// <param name="value">The value that will be placed in the request's entity body.</param>
  131. /// <returns>A task object representing the asynchronous operation.</returns>
  132. public static Task<HttpResponseMessage> PostAsXmlAsync<T>(this HttpClient client, Uri requestUri, T value)
  133. {
  134. return client.PostAsXmlAsync(requestUri, value, CancellationToken.None);
  135. }
  136. /// <summary>
  137. /// Sends a POST request as an asynchronous operation to the specified Uri with the given <paramref name="value"/> serialized
  138. /// as XML.
  139. /// </summary>
  140. /// <remarks>
  141. /// This method uses the default instance of <see cref="XmlMediaTypeFormatter"/>.
  142. /// </remarks>
  143. /// <typeparam name="T">The type of <paramref name="value"/>.</typeparam>
  144. /// <param name="client">The client used to make the request.</param>
  145. /// <param name="requestUri">The Uri the request is sent to.</param>
  146. /// <param name="value">The value that will be placed in the request's entity body.</param>
  147. /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
  148. /// <returns>A task object representing the asynchronous operation.</returns>
  149. public static Task<HttpResponseMessage> PostAsXmlAsync<T>(this HttpClient client, Uri requestUri, T value, CancellationToken cancellationToken)
  150. {
  151. return client.PostAsync(requestUri, value, new XmlMediaTypeFormatter(), cancellationToken);
  152. }
  153. /// <summary>
  154. /// Sends a POST request as an asynchronous operation to the specified Uri with <paramref name="value"/>
  155. /// serialized using the given <paramref name="formatter"/>.
  156. /// </summary>
  157. /// <seealso cref="PostAsync{T}(HttpClient, string, T, MediaTypeFormatter, string, CancellationToken)"/>
  158. /// <typeparam name="T">The type of <paramref name="value"/>.</typeparam>
  159. /// <param name="client">The client used to make the request.</param>
  160. /// <param name="requestUri">The Uri the request is sent to.</param>
  161. /// <param name="value">The value that will be placed in the request's entity body.</param>
  162. /// <param name="formatter">The formatter used to serialize the <paramref name="value"/>.</param>
  163. /// <returns>A task object representing the asynchronous operation.</returns>
  164. [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "1#", Justification = "We want to support URIs as strings")]
  165. public static Task<HttpResponseMessage> PostAsync<T>(this HttpClient client, string requestUri, T value, MediaTypeFormatter formatter)
  166. {
  167. return client.PostAsync(requestUri, value, formatter, CancellationToken.None);
  168. }
  169. /// <summary>
  170. /// Sends a POST request as an asynchronous operation to the specified Uri with <paramref name="value"/>
  171. /// serialized using the given <paramref name="formatter"/>.
  172. /// </summary>
  173. /// <seealso cref="PostAsync{T}(HttpClient, string, T, MediaTypeFormatter, string, CancellationToken)"/>
  174. /// <typeparam name="T">The type of <paramref name="value"/>.</typeparam>
  175. /// <param name="client">The client used to make the request.</param>
  176. /// <param name="requestUri">The Uri the request is sent to.</param>
  177. /// <param name="value">The value that will be placed in the request's entity body.</param>
  178. /// <param name="formatter">The formatter used to serialize the <paramref name="value"/>.</param>
  179. /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
  180. /// <returns>A task object representing the asynchronous operation.</returns>
  181. [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "1#", Justification = "We want to support URIs as strings")]
  182. public static Task<HttpResponseMessage> PostAsync<T>(this HttpClient client, string requestUri, T value, MediaTypeFormatter formatter, CancellationToken cancellationToken)
  183. {
  184. return client.PostAsync(requestUri, value, formatter, mediaType: (MediaTypeHeaderValue)null, cancellationToken: cancellationToken);
  185. }
  186. /// <summary>
  187. /// Sends a POST request as an asynchronous operation to the specified Uri with <paramref name="value"/>
  188. /// serialized using the given <paramref name="formatter"/>.
  189. /// </summary>
  190. /// <seealso cref="PostAsync{T}(HttpClient, string, T, MediaTypeFormatter, string, CancellationToken)"/>
  191. /// <typeparam name="T">The type of <paramref name="value"/>.</typeparam>
  192. /// <param name="client">The client used to make the request.</param>
  193. /// <param name="requestUri">The Uri the request is sent to.</param>
  194. /// <param name="value">The value that will be placed in the request's entity body.</param>
  195. /// <param name="formatter">The formatter used to serialize the <paramref name="value"/>.</param>
  196. /// <param name="mediaType">The authoritative value of the request's content's Content-Type header. Can be <c>null</c> in which case the
  197. /// <paramref name="formatter">formatter's</paramref> default content type will be used.</param>
  198. /// <returns>A task object representing the asynchronous operation.</returns>
  199. [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "1#", Justification = "We want to support URIs as strings")]
  200. public static Task<HttpResponseMessage> PostAsync<T>(this HttpClient client, string requestUri, T value, MediaTypeFormatter formatter, string mediaType)
  201. {
  202. return client.PostAsync(requestUri, value, formatter, mediaType, CancellationToken.None);
  203. }
  204. /// <summary>
  205. /// Sends a POST request as an asynchronous operation to the specified Uri with <paramref name="value"/>
  206. /// serialized using the given <paramref name="formatter"/>.
  207. /// </summary>
  208. /// <typeparam name="T">The type of <paramref name="value"/>.</typeparam>
  209. /// <param name="client">The client used to make the request.</param>
  210. /// <param name="requestUri">The Uri the request is sent to.</param>
  211. /// <param name="value">The value that will be placed in the request's entity body.</param>
  212. /// <param name="formatter">The formatter used to serialize the <paramref name="value"/>.</param>
  213. /// <param name="mediaType">The authoritative value of the request's content's Content-Type header. Can be <c>null</c> in which case the
  214. /// <paramref name="formatter">formatter's</paramref> default content type will be used.</param>
  215. /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
  216. /// <returns>A task object representing the asynchronous operation.</returns>
  217. [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "1#", Justification = "We want to support URIs as strings")]
  218. public static Task<HttpResponseMessage> PostAsync<T>(this HttpClient client, string requestUri, T value, MediaTypeFormatter formatter, string mediaType, CancellationToken cancellationToken)
  219. {
  220. return client.PostAsync(requestUri, value, formatter, ObjectContent.BuildHeaderValue(mediaType), cancellationToken);
  221. }
  222. /// <summary>
  223. /// Sends a POST request as an asynchronous operation to the specified Uri with <paramref name="value"/>
  224. /// serialized using the given <paramref name="formatter"/>.
  225. /// </summary>
  226. /// <typeparam name="T">The type of <paramref name="value"/>.</typeparam>
  227. /// <param name="client">The client used to make the request.</param>
  228. /// <param name="requestUri">The Uri the request is sent to.</param>
  229. /// <param name="value">The value that will be placed in the request's entity body.</param>
  230. /// <param name="formatter">The formatter used to serialize the <paramref name="value"/>.</param>
  231. /// <param name="mediaType">The authoritative value of the request's content's Content-Type header. Can be <c>null</c> in which case the
  232. /// <paramref name="formatter">formatter's</paramref> default content type will be used.</param>
  233. /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
  234. /// <returns>A task object representing the asynchronous operation.</returns>
  235. [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "1#", Justification = "We want to support URIs as strings")]
  236. [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "The caller is responsible for disposing the object")]
  237. [SuppressMessage("Microsoft.Usage", "CA2234:PassSystemUriObjectsInsteadOfStrings", Justification = "The called method will convert to Uri instance.")]
  238. public static Task<HttpResponseMessage> PostAsync<T>(this HttpClient client, string requestUri, T value, MediaTypeFormatter formatter, MediaTypeHeaderValue mediaType, CancellationToken cancellationToken)
  239. {
  240. if (client == null)
  241. {
  242. throw Error.ArgumentNull("client");
  243. }
  244. var content = new ObjectContent<T>(value, formatter, mediaType);
  245. return client.PostAsync(requestUri, content, cancellationToken);
  246. }
  247. /// <summary>
  248. /// Sends a POST request as an asynchronous operation to the specified Uri with <paramref name="value"/>
  249. /// serialized using the given <paramref name="formatter"/>.
  250. /// </summary>
  251. /// <seealso cref="PostAsync{T}(HttpClient, string, T, MediaTypeFormatter, string, CancellationToken)"/>
  252. /// <typeparam name="T">The type of <paramref name="value"/>.</typeparam>
  253. /// <param name="client">The client used to make the request.</param>
  254. /// <param name="requestUri">The Uri the request is sent to.</param>
  255. /// <param name="value">The value that will be placed in the request's entity body.</param>
  256. /// <param name="formatter">The formatter used to serialize the <paramref name="value"/>.</param>
  257. /// <returns>A task object representing the asynchronous operation.</returns>
  258. public static Task<HttpResponseMessage> PostAsync<T>(this HttpClient client, Uri requestUri, T value, MediaTypeFormatter formatter)
  259. {
  260. return client.PostAsync(requestUri, value, formatter, CancellationToken.None);
  261. }
  262. /// <summary>
  263. /// Sends a POST request as an asynchronous operation to the specified Uri with <paramref name="value"/>
  264. /// serialized using the given <paramref name="formatter"/>.
  265. /// </summary>
  266. /// <seealso cref="PostAsync{T}(HttpClient, string, T, MediaTypeFormatter, string, CancellationToken)"/>
  267. /// <typeparam name="T">The type of <paramref name="value"/>.</typeparam>
  268. /// <param name="client">The client used to make the request.</param>
  269. /// <param name="requestUri">The Uri the request is sent to.</param>
  270. /// <param name="value">The value that will be placed in the request's entity body.</param>
  271. /// <param name="formatter">The formatter used to serialize the <paramref name="value"/>.</param>
  272. /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
  273. /// <returns>A task object representing the asynchronous operation.</returns>
  274. public static Task<HttpResponseMessage> PostAsync<T>(this HttpClient client, Uri requestUri, T value, MediaTypeFormatter formatter, CancellationToken cancellationToken)
  275. {
  276. return client.PostAsync(requestUri, value, formatter, mediaType: (MediaTypeHeaderValue)null, cancellationToken: cancellationToken);
  277. }
  278. /// <summary>
  279. /// Sends a POST request as an asynchronous operation to the specified Uri with <paramref name="value"/>
  280. /// serialized using the given <paramref name="formatter"/>.
  281. /// </summary>
  282. /// <seealso cref="PostAsync{T}(HttpClient, string, T, MediaTypeFormatter, string, CancellationToken)"/>
  283. /// <typeparam name="T">The type of <paramref name="value"/>.</typeparam>
  284. /// <param name="client">The client used to make the request.</param>
  285. /// <param name="requestUri">The Uri the request is sent to.</param>
  286. /// <param name="value">The value that will be placed in the request's entity body.</param>
  287. /// <param name="formatter">The formatter used to serialize the <paramref name="value"/>.</param>
  288. /// <param name="mediaType">The authoritative value of the request's content's Content-Type header. Can be <c>null</c> in which case the
  289. /// <paramref name="formatter">formatter's</paramref> default content type will be used.</param>
  290. /// <returns>A task object representing the asynchronous operation.</returns>
  291. public static Task<HttpResponseMessage> PostAsync<T>(this HttpClient client, Uri requestUri, T value, MediaTypeFormatter formatter, string mediaType)
  292. {
  293. return client.PostAsync(requestUri, value, formatter, mediaType, CancellationToken.None);
  294. }
  295. /// <summary>
  296. /// Sends a POST request as an asynchronous operation to the specified Uri with <paramref name="value"/>
  297. /// serialized using the given <paramref name="formatter"/>.
  298. /// </summary>
  299. /// <typeparam name="T">The type of <paramref name="value"/>.</typeparam>
  300. /// <param name="client">The client used to make the request.</param>
  301. /// <param name="requestUri">The Uri the request is sent to.</param>
  302. /// <param name="value">The value that will be placed in the request's entity body.</param>
  303. /// <param name="formatter">The formatter used to serialize the <paramref name="value"/>.</param>
  304. /// <param name="mediaType">The authoritative value of the request's content's Content-Type header. Can be <c>null</c> in which case the
  305. /// <paramref name="formatter">formatter's</paramref> default content type will be used.</param>
  306. /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
  307. /// <returns>A task object representing the asynchronous operation.</returns>
  308. public static Task<HttpResponseMessage> PostAsync<T>(this HttpClient client, Uri requestUri, T value, MediaTypeFormatter formatter, string mediaType, CancellationToken cancellationToken)
  309. {
  310. return client.PostAsync(requestUri, value, formatter, ObjectContent.BuildHeaderValue(mediaType), cancellationToken);
  311. }
  312. /// <summary>
  313. /// Sends a POST request as an asynchronous operation to the specified Uri with <paramref name="value"/>
  314. /// serialized using the given <paramref name="formatter"/>.
  315. /// </summary>
  316. /// <typeparam name="T">The type of <paramref name="value"/>.</typeparam>
  317. /// <param name="client">The client used to make the request.</param>
  318. /// <param name="requestUri">The Uri the request is sent to.</param>
  319. /// <param name="value">The value that will be placed in the request's entity body.</param>
  320. /// <param name="formatter">The formatter used to serialize the <paramref name="value"/>.</param>
  321. /// <param name="mediaType">The authoritative value of the request's content's Content-Type header. Can be <c>null</c> in which case the
  322. /// <paramref name="formatter">formatter's</paramref> default content type will be used.</param>
  323. /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
  324. /// <returns>A task object representing the asynchronous operation.</returns>
  325. [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "The caller is responsible for disposing the object")]
  326. public static Task<HttpResponseMessage> PostAsync<T>(this HttpClient client, Uri requestUri, T value, MediaTypeFormatter formatter, MediaTypeHeaderValue mediaType, CancellationToken cancellationToken)
  327. {
  328. if (client == null)
  329. {
  330. throw Error.ArgumentNull("client");
  331. }
  332. var content = new ObjectContent<T>(value, formatter, mediaType);
  333. return client.PostAsync(requestUri, content, cancellationToken);
  334. }
  335. /// <summary>
  336. /// Sends a PUT request as an asynchronous operation to the specified Uri with the given <paramref name="value"/> serialized
  337. /// as JSON.
  338. /// </summary>
  339. /// <remarks>
  340. /// This method uses the default instance of <see cref="JsonMediaTypeFormatter"/>.
  341. /// </remarks>
  342. /// <typeparam name="T">The type of <paramref name="value"/>.</typeparam>
  343. /// <param name="client">The client used to make the request.</param>
  344. /// <param name="requestUri">The Uri the request is sent to.</param>
  345. /// <param name="value">The value that will be placed in the request's entity body.</param>
  346. /// <returns>A task object representing the asynchronous operation.</returns>
  347. [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "1#", Justification = "We want to support URIs as strings")]
  348. public static Task<HttpResponseMessage> PutAsJsonAsync<T>(this HttpClient client, string requestUri, T value)
  349. {
  350. return client.PutAsJsonAsync(requestUri, value, CancellationToken.None);
  351. }
  352. /// <summary>
  353. /// Sends a PUT request as an asynchronous operation to the specified Uri with the given <paramref name="value"/> serialized
  354. /// as JSON.
  355. /// </summary>
  356. /// <remarks>
  357. /// This method uses the default instance of <see cref="JsonMediaTypeFormatter"/>.
  358. /// </remarks>
  359. /// <typeparam name="T">The type of <paramref name="value"/>.</typeparam>
  360. /// <param name="client">The client used to make the request.</param>
  361. /// <param name="requestUri">The Uri the request is sent to.</param>
  362. /// <param name="value">The value that will be placed in the request's entity body.</param>
  363. /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
  364. /// <returns>A task object representing the asynchronous operation.</returns>
  365. [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "1#", Justification = "We want to support URIs as strings")]
  366. public static Task<HttpResponseMessage> PutAsJsonAsync<T>(this HttpClient client, string requestUri, T value, CancellationToken cancellationToken)
  367. {
  368. return client.PutAsync(requestUri, value, new JsonMediaTypeFormatter(), cancellationToken);
  369. }
  370. /// <summary>
  371. /// Sends a PUT request as an asynchronous operation to the specified Uri with the given <paramref name="value"/> serialized
  372. /// as JSON.
  373. /// </summary>
  374. /// <remarks>
  375. /// This method uses the default instance of <see cref="JsonMediaTypeFormatter"/>.
  376. /// </remarks>
  377. /// <typeparam name="T">The type of <paramref name="value"/>.</typeparam>
  378. /// <param name="client">The client used to make the request.</param>
  379. /// <param name="requestUri">The Uri the request is sent to.</param>
  380. /// <param name="value">The value that will be placed in the request's entity body.</param>
  381. /// <returns>A task object representing the asynchronous operation.</returns>
  382. public static Task<HttpResponseMessage> PutAsJsonAsync<T>(this HttpClient client, Uri requestUri, T value)
  383. {
  384. return client.PutAsJsonAsync(requestUri, value, CancellationToken.None);
  385. }
  386. /// <summary>
  387. /// Sends a PUT request as an asynchronous operation to the specified Uri with the given <paramref name="value"/> serialized
  388. /// as JSON.
  389. /// </summary>
  390. /// <remarks>
  391. /// This method uses the default instance of <see cref="JsonMediaTypeFormatter"/>.
  392. /// </remarks>
  393. /// <typeparam name="T">The type of <paramref name="value"/>.</typeparam>
  394. /// <param name="client">The client used to make the request.</param>
  395. /// <param name="requestUri">The Uri the request is sent to.</param>
  396. /// <param name="value">The value that will be placed in the request's entity body.</param>
  397. /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
  398. /// <returns>A task object representing the asynchronous operation.</returns>
  399. public static Task<HttpResponseMessage> PutAsJsonAsync<T>(this HttpClient client, Uri requestUri, T value, CancellationToken cancellationToken)
  400. {
  401. return client.PutAsync(requestUri, value, new JsonMediaTypeFormatter(), cancellationToken);
  402. }
  403. /// <summary>
  404. /// Sends a PUT request as an asynchronous operation to the specified Uri with the given <paramref name="value"/> serialized
  405. /// as XML.
  406. /// </summary>
  407. /// <remarks>
  408. /// This method uses the default instance of <see cref="XmlMediaTypeFormatter"/>.
  409. /// </remarks>
  410. /// <typeparam name="T">The type of <paramref name="value"/>.</typeparam>
  411. /// <param name="client">The client used to make the request.</param>
  412. /// <param name="requestUri">The Uri the request is sent to.</param>
  413. /// <param name="value">The value that will be placed in the request's entity body.</param>
  414. /// <returns>A task object representing the asynchronous operation.</returns>
  415. [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "1#", Justification = "We want to support URIs as strings")]
  416. public static Task<HttpResponseMessage> PutAsXmlAsync<T>(this HttpClient client, string requestUri, T value)
  417. {
  418. return client.PutAsXmlAsync(requestUri, value, CancellationToken.None);
  419. }
  420. /// <summary>
  421. /// Sends a PUT request as an asynchronous operation to the specified Uri with the given <paramref name="value"/> serialized
  422. /// as XML.
  423. /// </summary>
  424. /// <remarks>
  425. /// This method uses the default instance of <see cref="XmlMediaTypeFormatter"/>.
  426. /// </remarks>
  427. /// <typeparam name="T">The type of <paramref name="value"/>.</typeparam>
  428. /// <param name="client">The client used to make the request.</param>
  429. /// <param name="requestUri">The Uri the request is sent to.</param>
  430. /// <param name="value">The value that will be placed in the request's entity body.</param>
  431. /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
  432. /// <returns>A task object representing the asynchronous operation.</returns>
  433. [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "1#", Justification = "We want to support URIs as strings")]
  434. public static Task<HttpResponseMessage> PutAsXmlAsync<T>(this HttpClient client, string requestUri, T value, CancellationToken cancellationToken)
  435. {
  436. return client.PutAsync(requestUri, value, new XmlMediaTypeFormatter(), cancellationToken);
  437. }
  438. /// <summary>
  439. /// Sends a PUT request as an asynchronous operation to the specified Uri with the given <paramref name="value"/> serialized
  440. /// as XML.
  441. /// </summary>
  442. /// <remarks>
  443. /// This method uses the default instance of <see cref="XmlMediaTypeFormatter"/>.
  444. /// </remarks>
  445. /// <typeparam name="T">The type of <paramref name="value"/>.</typeparam>
  446. /// <param name="client">The client used to make the request.</param>
  447. /// <param name="requestUri">The Uri the request is sent to.</param>
  448. /// <param name="value">The value that will be placed in the request's entity body.</param>
  449. /// <returns>A task object representing the asynchronous operation.</returns>
  450. public static Task<HttpResponseMessage> PutAsXmlAsync<T>(this HttpClient client, Uri requestUri, T value)
  451. {
  452. return client.PutAsXmlAsync(requestUri, value, CancellationToken.None);
  453. }
  454. /// <summary>
  455. /// Sends a PUT request as an asynchronous operation to the specified Uri with the given <paramref name="value"/> serialized
  456. /// as XML.
  457. /// </summary>
  458. /// <remarks>
  459. /// This method uses the default instance of <see cref="XmlMediaTypeFormatter"/>.
  460. /// </remarks>
  461. /// <typeparam name="T">The type of <paramref name="value"/>.</typeparam>
  462. /// <param name="client">The client used to make the request.</param>
  463. /// <param name="requestUri">The Uri the request is sent to.</param>
  464. /// <param name="value">The value that will be placed in the request's entity body.</param>
  465. /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
  466. /// <returns>A task object representing the asynchronous operation.</returns>
  467. public static Task<HttpResponseMessage> PutAsXmlAsync<T>(this HttpClient client, Uri requestUri, T value, CancellationToken cancellationToken)
  468. {
  469. return client.PutAsync(requestUri, value, new XmlMediaTypeFormatter(), cancellationToken);
  470. }
  471. /// <summary>
  472. /// Sends a PUT request as an asynchronous operation to the specified Uri with <paramref name="value"/>
  473. /// serialized using the given <paramref name="formatter"/>.
  474. /// </summary>
  475. /// <seealso cref="PutAsync{T}(HttpClient, string, T, MediaTypeFormatter, string, CancellationToken)"/>
  476. /// <typeparam name="T">The type of <paramref name="value"/>.</typeparam>
  477. /// <param name="client">The client used to make the request.</param>
  478. /// <param name="requestUri">The Uri the request is sent to.</param>
  479. /// <param name="value">The value that will be placed in the request's entity body.</param>
  480. /// <param name="formatter">The formatter used to serialize the <paramref name="value"/>.</param>
  481. /// <returns>A task object representing the asynchronous operation.</returns>
  482. [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "1#", Justification = "We want to support URIs as strings")]
  483. public static Task<HttpResponseMessage> PutAsync<T>(this HttpClient client, string requestUri, T value, MediaTypeFormatter formatter)
  484. {
  485. return client.PutAsync(requestUri, value, formatter, CancellationToken.None);
  486. }
  487. /// <summary>
  488. /// Sends a PUT request as an asynchronous operation to the specified Uri with <paramref name="value"/>
  489. /// serialized using the given <paramref name="formatter"/>.
  490. /// </summary>
  491. /// <seealso cref="PutAsync{T}(HttpClient, string, T, MediaTypeFormatter, string, CancellationToken)"/>
  492. /// <typeparam name="T">The type of <paramref name="value"/>.</typeparam>
  493. /// <param name="client">The client used to make the request.</param>
  494. /// <param name="requestUri">The Uri the request is sent to.</param>
  495. /// <param name="value">The value that will be placed in the request's entity body.</param>
  496. /// <param name="formatter">The formatter used to serialize the <paramref name="value"/>.</param>
  497. /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
  498. /// <returns>A task object representing the asynchronous operation.</returns>
  499. [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "1#", Justification = "We want to support URIs as strings")]
  500. public static Task<HttpResponseMessage> PutAsync<T>(this HttpClient client, string requestUri, T value, MediaTypeFormatter formatter, CancellationToken cancellationToken)
  501. {
  502. return client.PutAsync(requestUri, value, formatter, mediaType: (MediaTypeHeaderValue)null, cancellationToken: cancellationToken);
  503. }
  504. /// <summary>
  505. /// Sends a PUT request as an asynchronous operation to the specified Uri with <paramref name="value"/>
  506. /// serialized using the given <paramref name="formatter"/>.
  507. /// </summary>
  508. /// <seealso cref="PutAsync{T}(HttpClient, string, T, MediaTypeFormatter, string, CancellationToken)"/>
  509. /// <typeparam name="T">The type of <paramref name="value"/>.</typeparam>
  510. /// <param name="client">The client used to make the request.</param>
  511. /// <param name="requestUri">The Uri the request is sent to.</param>
  512. /// <param name="value">The value that will be placed in the request's entity body.</param>
  513. /// <param name="formatter">The formatter used to serialize the <paramref name="value"/>.</param>
  514. /// <param name="mediaType">The authoritative value of the request's content's Content-Type header. Can be <c>null</c> in which case the
  515. /// <paramref name="formatter">formatter's</paramref> default content type will be used.</param>
  516. /// <returns>A task object representing the asynchronous operation.</returns>
  517. [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "1#", Justification = "We want to support URIs as strings")]
  518. public static Task<HttpResponseMessage> PutAsync<T>(this HttpClient client, string requestUri, T value, MediaTypeFormatter formatter, string mediaType)
  519. {
  520. return client.PutAsync(requestUri, value, formatter, mediaType, CancellationToken.None);
  521. }
  522. /// <summary>
  523. /// Sends a PUT request as an asynchronous operation to the specified Uri with <paramref name="value"/>
  524. /// serialized using the given <paramref name="formatter"/>.
  525. /// </summary>
  526. /// <typeparam name="T">The type of <paramref name="value"/>.</typeparam>
  527. /// <param name="client">The client used to make the request.</param>
  528. /// <param name="requestUri">The Uri the request is sent to.</param>
  529. /// <param name="value">The value that will be placed in the request's entity body.</param>
  530. /// <param name="formatter">The formatter used to serialize the <paramref name="value"/>.</param>
  531. /// <param name="mediaType">The authoritative value of the request's content's Content-Type header. Can be <c>null</c> in which case the
  532. /// <paramref name="formatter">formatter's</paramref> default content type will be used.</param>
  533. /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
  534. /// <returns>A task object representing the asynchronous operation.</returns>
  535. [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "1#", Justification = "We want to support URIs as strings")]
  536. public static Task<HttpResponseMessage> PutAsync<T>(this HttpClient client, string requestUri, T value, MediaTypeFormatter formatter, string mediaType, CancellationToken cancellationToken)
  537. {
  538. return client.PutAsync(requestUri, value, formatter, ObjectContent.BuildHeaderValue(mediaType), cancellationToken);
  539. }
  540. /// <summary>
  541. /// Sends a PUT request as an asynchronous operation to the specified Uri with <paramref name="value"/>
  542. /// serialized using the given <paramref name="formatter"/>.
  543. /// </summary>
  544. /// <typeparam name="T">The type of <paramref name="value"/>.</typeparam>
  545. /// <param name="client">The client used to make the request.</param>
  546. /// <param name="requestUri">The Uri the request is sent to.</param>
  547. /// <param name="value">The value that will be placed in the request's entity body.</param>
  548. /// <param name="formatter">The formatter used to serialize the <paramref name="value"/>.</param>
  549. /// <param name="mediaType">The authoritative value of the request's content's Content-Type header. Can be <c>null</c> in which case the
  550. /// <paramref name="formatter">formatter's</paramref> default content type will be used.</param>
  551. /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
  552. /// <returns>A task object representing the asynchronous operation.</returns>
  553. [SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings", MessageId = "1#", Justification = "We want to support URIs as strings")]
  554. [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "The caller is responsible for disposing the object")]
  555. [SuppressMessage("Microsoft.Usage", "CA2234:PassSystemUriObjectsInsteadOfStrings", Justification = "The called method will convert to Uri instance.")]
  556. public static Task<HttpResponseMessage> PutAsync<T>(this HttpClient client, string requestUri, T value, MediaTypeFormatter formatter, MediaTypeHeaderValue mediaType, CancellationToken cancellationToken)
  557. {
  558. if (client == null)
  559. {
  560. throw Error.ArgumentNull("client");
  561. }
  562. var content = new ObjectContent<T>(value, formatter, mediaType);
  563. return client.PutAsync(requestUri, content, cancellationToken);
  564. }
  565. /// <summary>
  566. /// Sends a PUT request as an asynchronous operation to the specified Uri with <paramref name="value"/>
  567. /// serialized using the given <paramref name="formatter"/>.
  568. /// </summary>
  569. /// <seealso cref="PutAsync{T}(HttpClient, string, T, MediaTypeFormatter, string, CancellationToken)"/>
  570. /// <typeparam name="T">The type of <paramref name="value"/>.</typeparam>
  571. /// <param name="client">The client used to make the request.</param>
  572. /// <param name="requestUri">The Uri the request is sent to.</param>
  573. /// <param name="value">The value that will be placed in the request's entity body.</param>
  574. /// <param name="formatter">The formatter used to serialize the <paramref name="value"/>.</param>
  575. /// <returns>A task object representing the asynchronous operation.</returns>
  576. public static Task<HttpResponseMessage> PutAsync<T>(this HttpClient client, Uri requestUri, T value, MediaTypeFormatter formatter)
  577. {
  578. return client.PutAsync(requestUri, value, formatter, CancellationToken.None);
  579. }
  580. /// <summary>
  581. /// Sends a PUT request as an asynchronous operation to the specified Uri with <paramref name="value"/>
  582. /// serialized using the given <paramref name="formatter"/>.
  583. /// </summary>
  584. /// <seealso cref="PutAsync{T}(HttpClient, string, T, MediaTypeFormatter, string, CancellationToken)"/>
  585. /// <typeparam name="T">The type of <paramref name="value"/>.</typeparam>
  586. /// <param name="client">The client used to make the request.</param>
  587. /// <param name="requestUri">The Uri the request is sent to.</param>
  588. /// <param name="value">The value that will be placed in the request's entity body.</param>
  589. /// <param name="formatter">The formatter used to serialize the <paramref name="value"/>.</param>
  590. /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
  591. /// <returns>A task object representing the asynchronous operation.</returns>
  592. public static Task<HttpResponseMessage> PutAsync<T>(this HttpClient client, Uri requestUri, T value, MediaTypeFormatter formatter, CancellationToken cancellationToken)
  593. {
  594. return client.PutAsync(requestUri, value, formatter, mediaType: (MediaTypeHeaderValue)null, cancellationToken: cancellationToken);
  595. }
  596. /// <summary>
  597. /// Sends a PUT request as an asynchronous operation to the specified Uri with <paramref name="value"/>
  598. /// serialized using the given <paramref name="formatter"/>.
  599. /// </summary>
  600. /// <seealso cref="PutAsync{T}(HttpClient, string, T, MediaTypeFormatter, string, CancellationToken)"/>
  601. /// <typeparam name="T">The type of <paramref name="value"/>.</typeparam>
  602. /// <param name="client">The client used to make the request.</param>
  603. /// <param name="requestUri">The Uri the request is sent to.</param>
  604. /// <param name="value">The value that will be placed in the request's entity body.</param>
  605. /// <param name="formatter">The formatter used to serialize the <paramref name="value"/>.</param>
  606. /// <param name="mediaType">The authoritative value of the request's content's Content-Type header. Can be <c>null</c> in which case the
  607. /// <paramref name="formatter">formatter's</paramref> default content type will be used.</param>
  608. /// <returns>A task object representing the asynchronous operation.</returns>
  609. public static Task<HttpResponseMessage> PutAsync<T>(this HttpClient client, Uri requestUri, T value, MediaTypeFormatter formatter, string mediaType)
  610. {
  611. return client.PutAsync(requestUri, value, formatter, mediaType, CancellationToken.None);
  612. }
  613. /// <summary>
  614. /// Sends a PUT request as an asynchronous operation to the specified Uri with <paramref name="value"/>
  615. /// serialized using the given <paramref name="formatter"/>.
  616. /// </summary>
  617. /// <typeparam name="T">The type of <paramref name="value"/>.</typeparam>
  618. /// <param name="client">The client used to make the request.</param>
  619. /// <param name="requestUri">The Uri the request is sent to.</param>
  620. /// <param name="value">The value that will be placed in the request's entity body.</param>
  621. /// <param name="formatter">The formatter used to serialize the <paramref name="value"/>.</param>
  622. /// <param name="mediaType">The authoritative value of the request's content's Content-Type header. Can be <c>null</c> in which case the
  623. /// <paramref name="formatter">formatter's</paramref> default content type will be used.</param>
  624. /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
  625. /// <returns>A task object representing the asynchronous operation.</returns>
  626. public static Task<HttpResponseMessage> PutAsync<T>(this HttpClient client, Uri requestUri, T value, MediaTypeFormatter formatter, string mediaType, CancellationToken cancellationToken)
  627. {
  628. return client.PutAsync(requestUri, value, formatter, ObjectContent.BuildHeaderValue(mediaType), cancellationToken);
  629. }
  630. /// <summary>
  631. /// Sends a PUT request as an asynchronous operation to the specified Uri with <paramref name="value"/>
  632. /// serialized using the given <paramref name="formatter"/>.
  633. /// </summary>
  634. /// <typeparam name="T">The type of <paramref name="value"/>.</typeparam>
  635. /// <param name="client">The client used to make the request.</param>
  636. /// <param name="requestUri">The Uri the request is sent to.</param>
  637. /// <param name="value">The value that will be placed in the request's entity body.</param>
  638. /// <param name="formatter">The formatter used to serialize the <paramref name="value"/>.</param>
  639. /// <param name="mediaType">The authoritative value of the request's content's Content-Type header. Can be <c>null</c> in which case the
  640. /// <paramref name="formatter">formatter's</paramref> default content type will be used.</param>
  641. /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
  642. /// <returns>A task object representing the asynchronous operation.</returns>
  643. [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "The caller is responsible for disposing the object")]
  644. public static Task<HttpResponseMessage> PutAsync<T>(this HttpClient client, Uri requestUri, T value, MediaTypeFormatter formatter, MediaTypeHeaderValue mediaType, CancellationToken cancellationToken)
  645. {
  646. if (client == null)
  647. {
  648. throw Error.ArgumentNull("client");
  649. }
  650. var content = new ObjectContent<T>(value, formatter, mediaType);
  651. return client.PutAsync(requestUri, content, cancellationToken);

Large files files are truncated, but you can click here to view the full file