5 // Marek Safar <marek.safar@gmail.com>
7 // Copyright (C) 2011 Xamarin Inc (http://www.xamarin.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 using System.Threading;
30 using System.Net.Http.Headers;
32 namespace System.Net.Http
34 public class HttpClient : IDisposable
36 static readonly TimeSpan TimeoutDefault = TimeSpan.FromSeconds (100);
39 CancellationTokenSource cancellation_token;
41 readonly HttpMessageHandler handler;
42 HttpRequestHeaders headers;
51 public HttpClient (HttpMessageHandler handler)
53 this.handler = handler ?? new HttpClientHandler ();
54 buffer_size = 0x10000;
55 timeout = TimeoutDefault;
58 public Uri BaseAddress {
67 public HttpRequestHeaders DefaultRequestHeaders {
69 return headers ?? (headers = new HttpRequestHeaders ());
73 public int MaxResponseContentBufferSize {
79 throw new ArgumentOutOfRangeException ();
85 public TimeSpan Timeout {
90 if (value != System.Threading.Timeout.InfiniteTimeSpan && value < TimeSpan.Zero)
91 throw new ArgumentOutOfRangeException ();
97 public void CancelPendingRequests ()
99 if (cancellation_token != null)
100 cancellation_token.Cancel ();
102 cancellation_token = new CancellationTokenSource ();
105 public void Dispose ()
110 protected virtual void Dispose (bool disposing)
112 if (disposing && !disposed) {
115 if (cancellation_token != null)
116 cancellation_token.Dispose ();
120 public HttpResponseMessage Delete (string requestUri)
122 return Send (new HttpRequestMessage (HttpMethod.Delete, requestUri));
125 public HttpResponseMessage Delete (Uri requestUri)
127 return Send (new HttpRequestMessage (HttpMethod.Delete, requestUri));
130 public HttpResponseMessage Get (string requestUri)
132 return Send (new HttpRequestMessage (HttpMethod.Get, requestUri));
135 public HttpResponseMessage Get (Uri requestUri)
137 return Send (new HttpRequestMessage (HttpMethod.Get, requestUri));
140 public HttpResponseMessage Post (string requestUri, HttpContent content)
142 return Send (new HttpRequestMessage (HttpMethod.Post, requestUri) { Content = content });
145 public HttpResponseMessage Put (Uri requestUri, HttpContent content)
147 return Send (new HttpRequestMessage (HttpMethod.Put, requestUri) { Content = content });
150 public HttpResponseMessage Put (string requestUri, HttpContent content)
152 return Send (new HttpRequestMessage (HttpMethod.Put, requestUri) { Content = content });
155 public HttpResponseMessage Post (Uri requestUri, HttpContent content)
157 return Send (new HttpRequestMessage (HttpMethod.Post, requestUri) { Content = content });
160 public HttpResponseMessage Send (HttpRequestMessage request)
162 return Send (request, HttpCompletionOption.ResponseContentRead, CancellationToken.None);
165 public HttpResponseMessage Send (HttpRequestMessage request, HttpCompletionOption completionOption)
167 return Send (request, completionOption, CancellationToken.None);
170 public HttpResponseMessage Send (HttpRequestMessage request, CancellationToken cancellationToken)
172 return Send (request, HttpCompletionOption.ResponseContentRead, cancellationToken);
175 public HttpResponseMessage Send (HttpRequestMessage request, HttpCompletionOption completionOption, CancellationToken cancellationToken)
178 throw new ArgumentNullException ("request");
180 if (request.SetIsUsed ())
181 throw new InvalidOperationException ("Cannot send the same request message multiple times");
183 if (request.RequestUri == null) {
184 if (base_address == null)
185 throw new InvalidOperationException ("The request URI must either be an absolute URI or BaseAddress must be set");
187 request.RequestUri = base_address;
191 if (cancellation_token == null)
192 cancellation_token = new CancellationTokenSource ();
194 using (var cts = CancellationTokenSource.CreateLinkedTokenSource (cancellation_token.Token, cancellationToken)) {
195 cts.CancelAfter (timeout);
197 var response = handler.Send (request, cts.Token);
198 if (response == null)
199 throw new InvalidOperationException ("Handler failed to return a response");
204 cancellation_token.Dispose ();
205 cancellation_token = null;