/
HttpClient.cs
256 lines (239 loc) · 10.8 KB
/
HttpClient.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
using System.Threading;
using System.Threading.Tasks;
namespace System.Net.Http
{
/// <summary>
/// Provides a base class for sending HTTP requests and receiving HTTP responses
/// from a resource identified by a URI.
/// </summary>
public class HttpClient : HttpMessageInvoker
{
/// <summary>
/// Initializes a new instance of the <see cref="HttpClient" /> class.
/// </summary>
public HttpClient() : this(null)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="HttpClient" /> class
/// with a specific handler.
/// </summary>
/// <param name="handler">The HTTP handler stack to use for sending requests.</param>
/// <remarks>
/// If null is specified for handler parameter, the <see cref="HttpClientHandler"/>
/// is used as transport handler.
/// </remarks>
public HttpClient(HttpMessageHandler handler)
: base(handler ?? new HttpClientHandler(), false)
{
}
public Task<HttpResponseMessage> GetAsync(string requestUri)
{
return GetAsync(requestUri, System.Threading.CancellationToken.None);
}
public Task<HttpResponseMessage> GetAsync(Uri requestUri)
{
return GetAsync(requestUri, System.Threading.CancellationToken.None);
}
public Task<HttpResponseMessage> GetAsync(string requestUri, System.Threading.CancellationToken token)
{
if (requestUri == null)
throw new ArgumentNullException("requestUri");
return GetAsync(new Uri(requestUri), token);
}
public Task<HttpResponseMessage> GetAsync(Uri requestUri, System.Threading.CancellationToken token)
{
return GetAsync(requestUri, HttpCompletionOption.ResponseContentRead, token);
}
/// <summary>
/// Send a GET request to the specified Uri with an HTTP completion option and
/// a cancellation token as an asynchronous operation.
/// </summary>
/// <param name="requestUri">The Uri the request is sent to.</param>
/// <param name="completionOption">
/// An HTTP completion option value that indicates when the operation should
/// be considered completed.
/// </param>
/// <param name="cancellationToken">
/// A cancellation token that can be used by other objects or threads to receive
/// notice of cancellation.
/// </param>
/// <returns>
/// Returns System.Threading.Tasks.Task<TResult>.The task object representing
/// the asynchronous operation.
/// </returns>
/// <exception cref="System.ArgumentNullException">The requestUri was null.</exception>
public Task<HttpResponseMessage> GetAsync(Uri requestUri, HttpCompletionOption completionOption, CancellationToken cancellationToken)
{
var req = new HttpRequestMessage() { RequestUri = requestUri, Method = HttpMethod.Get };
return SendAsync(req, completionOption, cancellationToken);
}
/// <summary>
/// Send a GET request to the specified Uri and return the response body as a
/// string in an asynchronous operation.
/// </summary>
/// <param name="requestUri">The Uri the request is sent to.</param>
/// <returns>
/// Returns System.Threading.Tasks.Task<TResult>.The task object representing
/// the asynchronous operation.
/// </returns>
/// <exception cref="System.ArgumentNullException">The requestUri was null.</exception>
public Task<string> GetStringAsync(string requestUri)
{
if (requestUri == null)
throw new ArgumentNullException("requestUri", "The requestUri was null.");
return GetStringAsync(new Uri(requestUri));
}
/// <summary>
/// Send a GET request to the specified Uri and return the response body as a
/// string in an asynchronous operation.
/// </summary>
/// <param name="requestUri">The Uri the request is sent to.</param>
/// <returns>
/// Returns System.Threading.Tasks.Task<TResult>.The task object representing
/// the asynchronous operation.
/// </returns>
/// <exception cref="System.ArgumentNullException">The requestUri was null.</exception>
public async Task<string> GetStringAsync(Uri requestUri)
{
//TODO: There's a lot of context switch here that could be avoided
var response = await GetAsync(requestUri);
return await response.Content.ReadAsStringAsync();
}
/// <summary>
/// Send a POST request to the specified Uri as an asynchronous operation.
/// </summary>
/// <param name="requestUri">T The Uri the request is sent to.</param>
/// <param name="content">The HTTP request content sent to the server.</param>
/// <returns>
/// Returns System.Threading.Tasks.Task<TResult>.The task object representing
/// the asynchronous operation.
/// </returns>
/// <exception cref="System.ArgumentNullException">The requestUri was null.</exception>
public Task<HttpResponseMessage> PostAsync(string requestUri, HttpContent content)
{
return PostAsync(requestUri, content, CancellationToken.None);
}
/// <summary>
/// Send a POST request with a cancellation token as an asynchronous operation.
/// </summary>
/// <param name="requestUri">he Uri the request is sent to.</param>
/// <param name="content">The HTTP request content sent to the server.</param>
/// <returns>
/// Returns System.Threading.Tasks.Task<TResult>.The task object representing
/// the asynchronous operation.
/// </returns>
/// <exception cref="System.ArgumentNullException">The requestUri was null.</exception>
public Task<HttpResponseMessage> PostAsync(Uri requestUri, HttpContent content)
{
return PostAsync(requestUri, content, CancellationToken.None);
}
/// <summary>
/// Send a POST request with a cancellation token as an asynchronous operation.
/// </summary>
/// <param name="requestUri">he Uri the request is sent to.</param>
/// <param name="content">The HTTP request content sent to the server.</param>
/// <param name="cancellationToken">
/// A cancellation token that can be used by other objects or threads to receiv
/// notice of cancellation.
/// </param>
/// <returns>
/// Returns System.Threading.Tasks.Task<TResult>.The task object representing
/// the asynchronous operation.
/// </returns>
/// <exception cref="System.ArgumentNullException">The requestUri was null.</exception>
public Task<HttpResponseMessage> PostAsync(string requestUri, HttpContent content, CancellationToken cancellationToken)
{
if (requestUri == null)
throw new ArgumentNullException("requestUri", "The requestUri was null.");
return PostAsync(new Uri(requestUri), content, cancellationToken);
}
/// <summary>
/// Send a POST request with a cancellation token as an asynchronous operation.
/// </summary>
/// <param name="requestUri">he Uri the request is sent to.</param>
/// <param name="content">The HTTP request content sent to the server.</param>
/// <param name="cancellationToken">
/// A cancellation token that can be used by other objects or threads to receiv
/// notice of cancellation.
/// </param>
/// <returns>
/// Returns System.Threading.Tasks.Task<TResult>.The task object representing
/// the asynchronous operation.
/// </returns>
/// <exception cref="System.ArgumentNullException">The requestUri was null.</exception>
public Task<HttpResponseMessage> PostAsync(Uri requestUri, HttpContent content, CancellationToken cancellationToken)
{
if (requestUri == null)
throw new ArgumentNullException("requestUri", "The requestUri was null.");
var req = new HttpRequestMessage() { RequestUri = requestUri, Method = HttpMethod.Post, Content = content };
return SendAsync(req, cancellationToken);
}
/// <summary>
/// Send an HTTP request as an asynchronous operation.
/// </summary>
/// <param name="request">The HTTP request message to send.</param>
/// <returns>
/// Returns System.Threading.Tasks.Task<TResult>.The task object representing
/// the asynchronous operation.
/// </returns>
/// <exception cref="System.ArgumentNullException">The request was null.</exception>
/// <exception cref="System.InvalidOperationException">The request message was already sent by the
/// <see cref="HttpClient"/> instance.</exception>
public Task<HttpResponseMessage> SendAsync(HttpRequestMessage request)
{
return SendAsync(request, HttpCompletionOption.ResponseContentRead, CancellationToken.None);
}
/// <summary>
/// Send an HTTP request as an asynchronous operation.
/// </summary>
/// <param name="request">The HTTP request message to send.</param>
/// <param name="cancellationToken">The cancellation token to cancel operation.</param>
/// <returns>
/// Returns System.Threading.Tasks.Task<TResult>.The task object representing
/// the asynchronous operation.
/// </returns>
/// <exception cref="System.ArgumentNullException">The request was null.</exception>
/// <exception cref="System.InvalidOperationException">The request message was already sent by the
/// <see cref="HttpClient"/> instance.</exception>
public override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
return base.SendAsync(request, cancellationToken);
}
/// <summary>
/// Send an HTTP request as an asynchronous operation.
/// </summary>
/// <param name="request">The HTTP request message to send.</param>
/// <param name="completionOption">When the operation should complete
/// (as soon as a response is available or after reading the whole response content).</param>
/// <returns>
/// Returns System.Threading.Tasks.Task<TResult>.The task object representing
/// the asynchronous operation.
/// </returns>
/// <exception cref="System.ArgumentNullException">The request was null.</exception>
/// <exception cref="System.InvalidOperationException">The request message was already sent by the
/// <see cref="HttpClient"/> instance.</exception>
public Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, HttpCompletionOption completionOption)
{
return SendAsync(request, completionOption, CancellationToken.None);
}
/// <summary>
/// Send an HTTP request as an asynchronous operation.
/// </summary>
/// <param name="request">The HTTP request message to send.</param>
/// <param name="completionOption">When the operation should complete
/// (as soon as a response is available or after reading the whole response content).</param>
/// <param name="cancellationToken">The cancellation token to cancel operation.</param>
/// <returns>
/// Returns System.Threading.Tasks.Task<TResult>.The task object representing
/// the asynchronous operation.
/// </returns>
/// <exception cref="System.ArgumentNullException">The request was null.</exception>
/// <exception cref="System.InvalidOperationException">The request message was already sent by the
/// <see cref="HttpClient"/> instance.</exception>
public Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationToken cancellationToken)
{
return SendAsync(request, cancellationToken);
}
}
}