2 // WebRequestHandler.cs
5 // Martin Baulig <martin.baulig@xamarin.com>
7 // Copyright (c) 2013 Xamarin Inc. (http://www.xamarin.com)
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 using System.Net.Cache;
28 using System.Net.Security;
29 using System.Security.Principal;
30 using System.Security.Cryptography.X509Certificates;
32 namespace System.Net.Http
34 public class WebRequestHandler : HttpClientHandler
37 RequestCachePolicy cachePolicy;
38 AuthenticationLevel authenticationLevel;
39 TimeSpan continueTimeout;
40 TokenImpersonationLevel impersonationLevel;
41 int maxResponseHeadersLength;
43 RemoteCertificateValidationCallback serverCertificateValidationCallback;
44 bool unsafeAuthenticatedConnectionSharing;
45 X509CertificateCollection clientCertificates;
47 public WebRequestHandler ()
49 allowPipelining = true;
50 authenticationLevel = AuthenticationLevel.MutualAuthRequested;
51 cachePolicy = System.Net.WebRequest.DefaultCachePolicy;
52 continueTimeout = TimeSpan.FromMilliseconds (350);
53 impersonationLevel = TokenImpersonationLevel.Delegation;
54 maxResponseHeadersLength = HttpWebRequest.DefaultMaximumResponseHeadersLength;
55 readWriteTimeout = 300000;
56 serverCertificateValidationCallback = null;
57 unsafeAuthenticatedConnectionSharing = false;
58 clientCertificates = new X509CertificateCollection();
61 public bool AllowPipelining {
62 get { return allowPipelining; }
64 EnsureModifiability ();
65 allowPipelining = value;
69 public RequestCachePolicy CachePolicy {
70 get { return cachePolicy; }
72 EnsureModifiability ();
77 public AuthenticationLevel AuthenticationLevel {
78 get { return authenticationLevel; }
80 EnsureModifiability ();
81 authenticationLevel = value;
85 public X509CertificateCollection ClientCertificates {
87 if (this.ClientCertificateOptions != ClientCertificateOption.Manual) {
88 throw new InvalidOperationException("The ClientCertificateOptions property must be set to 'Manual' to use this property.");
91 return clientCertificates;
96 public TimeSpan ContinueTimeout {
97 get { return continueTimeout; }
99 EnsureModifiability ();
100 continueTimeout = value;
104 public TokenImpersonationLevel ImpersonationLevel {
105 get { return impersonationLevel; }
107 EnsureModifiability ();
108 impersonationLevel = value;
112 public int MaxResponseHeadersLength {
113 get { return maxResponseHeadersLength; }
115 EnsureModifiability ();
116 maxResponseHeadersLength = value;
120 public int ReadWriteTimeout {
121 get { return readWriteTimeout; }
123 EnsureModifiability ();
124 readWriteTimeout = value;
128 public RemoteCertificateValidationCallback ServerCertificateValidationCallback {
129 get { return serverCertificateValidationCallback; }
131 EnsureModifiability ();
132 serverCertificateValidationCallback = value;
136 public bool UnsafeAuthenticatedConnectionSharing {
137 get { return unsafeAuthenticatedConnectionSharing; }
139 EnsureModifiability ();
140 unsafeAuthenticatedConnectionSharing = value;
144 internal override HttpWebRequest CreateWebRequest (HttpRequestMessage request)
146 HttpWebRequest wr = base.CreateWebRequest (request);
148 wr.Pipelined = allowPipelining;
149 wr.AuthenticationLevel = authenticationLevel;
150 wr.CachePolicy = cachePolicy;
151 wr.ImpersonationLevel = impersonationLevel;
152 wr.MaximumResponseHeadersLength = maxResponseHeadersLength;
153 wr.ReadWriteTimeout = readWriteTimeout;
154 wr.UnsafeAuthenticatedConnectionSharing = unsafeAuthenticatedConnectionSharing;
155 wr.ServerCertificateValidationCallback = serverCertificateValidationCallback;
157 if (this.ClientCertificateOptions == ClientCertificateOption.Manual) {
158 wr.ClientCertificates = clientCertificates;