[HttpWebRequest] Support 4.0 Host property.
[mono.git] / mcs / class / System / System.Net / HttpWebRequest.cs
1 //
2 // System.Net.HttpWebRequest
3 //
4 // Authors:
5 //      Lawrence Pit (loz@cable.a2000.nl)
6 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 //
8 // (c) 2002 Lawrence Pit
9 // (c) 2003 Ximian, Inc. (http://www.ximian.com)
10 // (c) 2004 Novell, Inc. (http://www.novell.com)
11 //
12
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33
34 using System;
35 using System.Collections;
36 using System.Configuration;
37 using System.IO;
38 using System.Net.Cache;
39 using System.Net.Sockets;
40 using System.Runtime.Remoting.Messaging;
41 using System.Runtime.Serialization;
42 using System.Security.Cryptography.X509Certificates;
43 using System.Text;
44 using System.Threading;
45
46 namespace System.Net 
47 {
48 #if MOONLIGHT
49         internal class HttpWebRequest : WebRequest, ISerializable {
50 #else
51         [Serializable]
52         public class HttpWebRequest : WebRequest, ISerializable {
53 #endif
54                 Uri requestUri;
55                 Uri actualUri;
56                 bool hostChanged;
57                 bool allowAutoRedirect = true;
58                 bool allowBuffering = true;
59                 X509CertificateCollection certificates;
60                 string connectionGroup;
61                 long contentLength = -1;
62                 HttpContinueDelegate continueDelegate;
63                 CookieContainer cookieContainer;
64                 ICredentials credentials;
65                 bool haveResponse;              
66                 bool haveRequest;
67                 bool requestSent;
68                 WebHeaderCollection webHeaders = new WebHeaderCollection (true);
69                 bool keepAlive = true;
70                 int maxAutoRedirect = 50;
71                 string mediaType = String.Empty;
72                 string method = "GET";
73                 string initialMethod = "GET";
74                 bool pipelined = true;
75                 bool preAuthenticate;
76                 bool usedPreAuth;
77                 Version version = HttpVersion.Version11;
78                 Version actualVersion;
79                 IWebProxy proxy;
80                 bool sendChunked;
81                 ServicePoint servicePoint;
82                 int timeout = 100000;
83                 
84                 WebConnectionStream writeStream;
85                 HttpWebResponse webResponse;
86                 WebAsyncResult asyncWrite;
87                 WebAsyncResult asyncRead;
88                 EventHandler abortHandler;
89                 int aborted;
90                 bool gotRequestStream;
91                 int redirects;
92                 bool expectContinue;
93                 bool authCompleted;
94                 byte[] bodyBuffer;
95                 int bodyBufferLength;
96                 bool getResponseCalled;
97                 Exception saved_exc;
98                 object locker = new object ();
99                 bool finished_reading;
100                 internal WebConnection WebConnection;
101                 DecompressionMethods auto_decomp;
102                 int maxResponseHeadersLength;
103                 static int defaultMaxResponseHeadersLength;
104                 int readWriteTimeout = 300000; // ms
105
106                 enum NtlmAuthState {
107                         None,
108                         Challenge,
109                         Response
110                 }
111                 NtlmAuthState ntlm_auth_state;
112                 string host;
113
114                 // Constructors
115                 static HttpWebRequest ()
116                 {
117                         defaultMaxResponseHeadersLength = 64 * 1024;
118 #if !NET_2_1
119                         NetConfig config = ConfigurationSettings.GetConfig ("system.net/settings") as NetConfig;
120                         if (config != null) {
121                                 int x = config.MaxResponseHeadersLength;
122                                 if (x != -1)
123                                         x *= 64;
124
125                                 defaultMaxResponseHeadersLength = x;
126                         }
127 #endif
128                 }
129
130 #if NET_2_1
131                 public
132 #else
133                 internal
134 #endif
135                 HttpWebRequest (Uri uri) 
136                 {
137                         this.requestUri = uri;
138                         this.actualUri = uri;
139                         this.proxy = GlobalProxySelection.Select;
140                 }               
141                 
142                 [Obsolete ("Serialization is obsoleted for this type", false)]
143                 protected HttpWebRequest (SerializationInfo serializationInfo, StreamingContext streamingContext) 
144                 {
145                         SerializationInfo info = serializationInfo;
146
147                         requestUri = (Uri) info.GetValue ("requestUri", typeof (Uri));
148                         actualUri = (Uri) info.GetValue ("actualUri", typeof (Uri));
149                         allowAutoRedirect = info.GetBoolean ("allowAutoRedirect");
150                         allowBuffering = info.GetBoolean ("allowBuffering");
151                         certificates = (X509CertificateCollection) info.GetValue ("certificates", typeof (X509CertificateCollection));
152                         connectionGroup = info.GetString ("connectionGroup");
153                         contentLength = info.GetInt64 ("contentLength");
154                         webHeaders = (WebHeaderCollection) info.GetValue ("webHeaders", typeof (WebHeaderCollection));
155                         keepAlive = info.GetBoolean ("keepAlive");
156                         maxAutoRedirect = info.GetInt32 ("maxAutoRedirect");
157                         mediaType = info.GetString ("mediaType");
158                         method = info.GetString ("method");
159                         initialMethod = info.GetString ("initialMethod");
160                         pipelined = info.GetBoolean ("pipelined");
161                         version = (Version) info.GetValue ("version", typeof (Version));
162                         proxy = (IWebProxy) info.GetValue ("proxy", typeof (IWebProxy));
163                         sendChunked = info.GetBoolean ("sendChunked");
164                         timeout = info.GetInt32 ("timeout");
165                         redirects = info.GetInt32 ("redirects");
166                         host = info.GetString ("host");
167                 }
168                 
169                 // Properties
170
171                 public string Accept {
172                         get { return webHeaders ["Accept"]; }
173                         set {
174                                 CheckRequestStarted ();
175                                 webHeaders.RemoveAndAdd ("Accept", value);
176                         }
177                 }
178                 
179                 public Uri Address {
180                         get { return actualUri; }
181                 }
182                 
183                 public bool AllowAutoRedirect {
184                         get { return allowAutoRedirect; }
185                         set { this.allowAutoRedirect = value; }
186                 }
187                 
188                 public bool AllowWriteStreamBuffering {
189                         get { return allowBuffering; }
190                         set { allowBuffering = value; }
191                 }
192
193                 static Exception GetMustImplement ()
194                 {
195                         return new NotImplementedException ();
196                 }
197                 
198                 public DecompressionMethods AutomaticDecompression
199                 {
200                         get {
201                                 return auto_decomp;
202                         }
203                         set {
204                                 CheckRequestStarted ();
205                                 auto_decomp = value;
206                         }
207                 }
208                 
209                 internal bool InternalAllowBuffering {
210                         get {
211                                 return (allowBuffering && (method != "HEAD" && method != "GET" &&
212                                                         method != "MKCOL" && method != "CONNECT" &&
213                                                         method != "TRACE"));
214                         }
215                 }
216                 
217                 public X509CertificateCollection ClientCertificates {
218                         get {
219                                 if (certificates == null)
220                                         certificates = new X509CertificateCollection ();
221
222                                 return certificates;
223                         }
224                         [MonoTODO]
225                         set {
226                                 throw GetMustImplement ();
227                         }
228                 }
229                 
230                 public string Connection {
231                         get { return webHeaders ["Connection"]; }
232                         set {
233                                 CheckRequestStarted ();
234                                 string val = value;
235                                 if (val != null) 
236                                         val = val.Trim ().ToLower ();
237
238                                 if (val == null || val.Length == 0) {
239                                         webHeaders.RemoveInternal ("Connection");
240                                         return;
241                                 }
242
243                                 if (val == "keep-alive" || val == "close") 
244                                         throw new ArgumentException ("Keep-Alive and Close may not be set with this property");
245
246                                 if (keepAlive && val.IndexOf ("keep-alive", StringComparison.Ordinal) == -1)
247                                         value = value + ", Keep-Alive";
248                                 
249                                 webHeaders.RemoveAndAdd ("Connection", value);
250                         }
251                 }               
252                 
253                 public override string ConnectionGroupName { 
254                         get { return connectionGroup; }
255                         set { connectionGroup = value; }
256                 }
257                 
258                 public override long ContentLength { 
259                         get { return contentLength; }
260                         set { 
261                                 CheckRequestStarted ();
262                                 if (value < 0)
263                                         throw new ArgumentOutOfRangeException ("value", "Content-Length must be >= 0");
264                                         
265                                 contentLength = value;
266                         }
267                 }
268                 
269                 internal long InternalContentLength {
270                         set { contentLength = value; }
271                 }
272                 
273                 public override string ContentType { 
274                         get { return webHeaders ["Content-Type"]; }
275                         set {
276                                 if (value == null || value.Trim().Length == 0) {
277                                         webHeaders.RemoveInternal ("Content-Type");
278                                         return;
279                                 }
280                                 webHeaders.RemoveAndAdd ("Content-Type", value);
281                         }
282                 }
283                 
284                 public HttpContinueDelegate ContinueDelegate {
285                         get { return continueDelegate; }
286                         set { continueDelegate = value; }
287                 }
288                 
289                 public CookieContainer CookieContainer {
290                         get { return cookieContainer; }
291                         set { cookieContainer = value; }
292                 }
293                 
294                 public override ICredentials Credentials { 
295                         get { return credentials; }
296                         set { credentials = value; }
297                 }
298
299                 [MonoTODO]
300                 public static new RequestCachePolicy DefaultCachePolicy
301                 {
302                         get {
303                                 throw GetMustImplement ();
304                         }
305                         set {
306                                 throw GetMustImplement ();
307                         }
308                 }
309                 
310                 [MonoTODO]
311                 public static int DefaultMaximumErrorResponseLength
312                 {
313                         get {
314                                 throw GetMustImplement ();
315                         }
316                         set {
317                                 throw GetMustImplement ();
318                         }
319                 }
320                 
321                 public string Expect {
322                         get { return webHeaders ["Expect"]; }
323                         set {
324                                 CheckRequestStarted ();
325                                 string val = value;
326                                 if (val != null)
327                                         val = val.Trim ().ToLower ();
328
329                                 if (val == null || val.Length == 0) {
330                                         webHeaders.RemoveInternal ("Expect");
331                                         return;
332                                 }
333
334                                 if (val == "100-continue")
335                                         throw new ArgumentException ("100-Continue cannot be set with this property.",
336                                                                      "value");
337                                 webHeaders.RemoveAndAdd ("Expect", value);
338                         }
339                 }
340                 
341                 public bool HaveResponse {
342                         get { return haveResponse; }
343                 }
344                 
345                 public override WebHeaderCollection Headers { 
346                         get { return webHeaders; }
347                         set {
348                                 CheckRequestStarted ();
349                                 WebHeaderCollection newHeaders = new WebHeaderCollection (true);
350                                 int count = value.Count;
351                                 for (int i = 0; i < count; i++) 
352                                         newHeaders.Add (value.GetKey (i), value.Get (i));
353
354                                 webHeaders = newHeaders;
355                         }
356                 }
357 #if NET_4_0
358                 public
359 #else
360                 internal
361 #endif
362                 string Host {
363                         get {
364                                 if (host == null)
365                                         return actualUri.Authority;
366                                 return host;
367                         }
368                         set {
369                                 if (value == null)
370                                         throw new ArgumentNullException ("value");
371
372                                 if (!CheckValidHost (value))
373                                         throw new ArgumentException ("Invalid host: " + value);
374
375                                 host = value;
376                         }
377                 }
378
379                 static char [] colon = { ':' };
380                 static bool CheckValidHost (string val)
381                 {
382                         if (val == null)
383                                 throw new ArgumentNullException ("value");
384
385                         if (val.Length == 0)
386                                 return false;
387
388                         if (val [0] == '.')
389                                 return false;
390
391                         string [] parts = val.Split ('.');
392                         int l = parts.Length;
393                         string [] last = parts [l - 1].Split (colon, 2);
394                         if (last.Length == 2) {
395                                 parts [l - 1] = last [0];
396                                 ushort port;
397                                 if (!UInt16.TryParse (parts [1], out port))
398                                         return false;
399                         }
400
401                         // MS does not enforce a max. 255 length
402                         // MS does not complain about a leading dash or all numbers...
403                         return true;
404                 }
405
406                 public DateTime IfModifiedSince {
407                         get { 
408                                 string str = webHeaders ["If-Modified-Since"];
409                                 if (str == null)
410                                         return DateTime.Now;
411                                 try {
412                                         return MonoHttpDate.Parse (str);
413                                 } catch (Exception) {
414                                         return DateTime.Now;
415                                 }
416                         }
417                         set {
418                                 CheckRequestStarted ();
419                                 // rfc-1123 pattern
420                                 webHeaders.SetInternal ("If-Modified-Since", 
421                                         value.ToUniversalTime ().ToString ("r", null));
422                                 // TODO: check last param when using different locale
423                         }
424                 }
425
426                 public bool KeepAlive {         
427                         get {
428                                 return keepAlive;
429                         }
430                         set {
431                                 keepAlive = value;
432                         }
433                 }
434                 
435                 public int MaximumAutomaticRedirections {
436                         get { return maxAutoRedirect; }
437                         set {
438                                 if (value <= 0)
439                                         throw new ArgumentException ("Must be > 0", "value");
440
441                                 maxAutoRedirect = value;
442                         }                       
443                 }
444
445                 [MonoTODO ("Use this")]
446                 public int MaximumResponseHeadersLength {
447                         get { return maxResponseHeadersLength; }
448                         set { maxResponseHeadersLength = value; }
449                 }
450
451                 [MonoTODO ("Use this")]
452                 public static int DefaultMaximumResponseHeadersLength {
453                         get { return defaultMaxResponseHeadersLength; }
454                         set { defaultMaxResponseHeadersLength = value; }
455                 }
456
457                 public  int ReadWriteTimeout {
458                         get { return readWriteTimeout; }
459                         set {
460                                 if (requestSent)
461                                         throw new InvalidOperationException ("The request has already been sent.");
462
463                                 if (value < -1)
464                                         throw new ArgumentOutOfRangeException ("value", "Must be >= -1");
465
466                                 readWriteTimeout = value;
467                         }
468                 }
469                 
470                 public string MediaType {
471                         get { return mediaType; }
472                         set { 
473                                 mediaType = value;
474                         }
475                 }
476                 
477                 public override string Method { 
478                         get { return this.method; }
479                         set { 
480                                 if (value == null || value.Trim () == "")
481                                         throw new ArgumentException ("not a valid method");
482
483                                 method = value;
484                         }
485                 }
486                 
487                 public bool Pipelined {
488                         get { return pipelined; }
489                         set { pipelined = value; }
490                 }               
491                 
492                 public override bool PreAuthenticate { 
493                         get { return preAuthenticate; }
494                         set { preAuthenticate = value; }
495                 }
496                 
497                 public Version ProtocolVersion {
498                         get { return version; }
499                         set { 
500                                 if (value != HttpVersion.Version10 && value != HttpVersion.Version11)
501                                         throw new ArgumentException ("value");
502
503                                 version = value; 
504                         }
505                 }
506                 
507                 public override IWebProxy Proxy { 
508                         get { return proxy; }
509                         set { 
510                                 CheckRequestStarted ();
511                                 proxy = value;
512                                 servicePoint = null; // we may need a new one
513                         }
514                 }
515                 
516                 public string Referer {
517                         get { return webHeaders ["Referer"]; }
518                         set {
519                                 CheckRequestStarted ();
520                                 if (value == null || value.Trim().Length == 0) {
521                                         webHeaders.RemoveInternal ("Referer");
522                                         return;
523                                 }
524                                 webHeaders.SetInternal ("Referer", value);
525                         }
526                 }
527
528                 public override Uri RequestUri { 
529                         get { return requestUri; }
530                 }
531                 
532                 public bool SendChunked {
533                         get { return sendChunked; }
534                         set {
535                                 CheckRequestStarted ();
536                                 sendChunked = value;
537                         }
538                 }
539                 
540                 public ServicePoint ServicePoint {
541                         get { return GetServicePoint (); }
542                 }
543                 
544                 public override int Timeout { 
545                         get { return timeout; }
546                         set {
547                                 if (value < -1)
548                                         throw new ArgumentOutOfRangeException ("value");
549
550                                 timeout = value;
551                         }
552                 }
553                 
554                 public string TransferEncoding {
555                         get { return webHeaders ["Transfer-Encoding"]; }
556                         set {
557                                 CheckRequestStarted ();
558                                 string val = value;
559                                 if (val != null)
560                                         val = val.Trim ().ToLower ();
561
562                                 if (val == null || val.Length == 0) {
563                                         webHeaders.RemoveInternal ("Transfer-Encoding");
564                                         return;
565                                 }
566
567                                 if (val == "chunked")
568                                         throw new ArgumentException ("Chunked encoding must be set with the SendChunked property");
569
570                                 if (!sendChunked)
571                                         throw new ArgumentException ("SendChunked must be True", "value");
572
573                                 webHeaders.RemoveAndAdd ("Transfer-Encoding", value);
574                         }
575                 }
576
577                 public override bool UseDefaultCredentials
578                 {
579                         get { return CredentialCache.DefaultCredentials == Credentials; }
580                         set { Credentials = value ? CredentialCache.DefaultCredentials : null; }
581                 }
582                 
583                 public string UserAgent {
584                         get { return webHeaders ["User-Agent"]; }
585                         set { webHeaders.SetInternal ("User-Agent", value); }
586                 }
587
588                 bool unsafe_auth_blah;
589                 public bool UnsafeAuthenticatedConnectionSharing
590                 {
591                         get { return unsafe_auth_blah; }
592                         set { unsafe_auth_blah = value; }
593                 }
594
595                 internal bool GotRequestStream {
596                         get { return gotRequestStream; }
597                 }
598
599                 internal bool ExpectContinue {
600                         get { return expectContinue; }
601                         set { expectContinue = value; }
602                 }
603                 
604                 internal Uri AuthUri {
605                         get { return actualUri; }
606                 }
607                 
608                 internal bool ProxyQuery {
609                         get { return servicePoint.UsesProxy && !servicePoint.UseConnect; }
610                 }
611                 
612                 // Methods
613                 
614                 internal ServicePoint GetServicePoint ()
615                 {
616                         lock (locker) {
617                                 if (hostChanged || servicePoint == null) {
618                                         servicePoint = ServicePointManager.FindServicePoint (actualUri, proxy);
619                                         hostChanged = false;
620                                 }
621                         }
622
623                         return servicePoint;
624                 }
625                 
626                 public void AddRange (int range)
627                 {
628                         AddRange ("bytes", range);
629                 }
630                 
631                 public void AddRange (int from, int to)
632                 {
633                         AddRange ("bytes", from, to);
634                 }
635                 
636                 public void AddRange (string rangeSpecifier, int range)
637                 {
638                         if (rangeSpecifier == null)
639                                 throw new ArgumentNullException ("rangeSpecifier");
640                         string value = webHeaders ["Range"];
641                         if (value == null || value.Length == 0) 
642                                 value = rangeSpecifier + "=";
643                         else if (value.ToLower ().StartsWith (rangeSpecifier.ToLower () + "="))
644                                 value += ",";
645                         else
646                                 throw new InvalidOperationException ("rangeSpecifier");
647                         webHeaders.RemoveAndAdd ("Range", value + range + "-"); 
648                 }
649                 
650                 public void AddRange (string rangeSpecifier, int from, int to)
651                 {
652                         if (rangeSpecifier == null)
653                                 throw new ArgumentNullException ("rangeSpecifier");
654                         if (from < 0 || to < 0 || from > to)
655                                 throw new ArgumentOutOfRangeException ();                       
656                         string value = webHeaders ["Range"];
657                         if (value == null || value.Length == 0) 
658                                 value = rangeSpecifier + "=";
659                         else if (value.ToLower ().StartsWith (rangeSpecifier.ToLower () + "="))
660                                 value += ",";
661                         else
662                                 throw new InvalidOperationException ("rangeSpecifier");
663                         webHeaders.RemoveAndAdd ("Range", value + from + "-" + to);     
664                 }
665                 
666                 public override IAsyncResult BeginGetRequestStream (AsyncCallback callback, object state) 
667                 {
668                         if (Aborted)
669                                 throw new WebException ("The request was canceled.", WebExceptionStatus.RequestCanceled);
670
671                         bool send = !(method == "GET" || method == "CONNECT" || method == "HEAD" ||
672                                         method == "TRACE");
673                         if (method == null || !send)
674                                 throw new ProtocolViolationException ("Cannot send data when method is: " + method);
675
676                         if (contentLength == -1 && !sendChunked && !allowBuffering && KeepAlive)
677                                 throw new ProtocolViolationException ("Content-Length not set");
678
679                         string transferEncoding = TransferEncoding;
680                         if (!sendChunked && transferEncoding != null && transferEncoding.Trim () != "")
681                                 throw new ProtocolViolationException ("SendChunked should be true.");
682
683                         lock (locker)
684                         {
685                                 if (asyncWrite != null) {
686                                         throw new InvalidOperationException ("Cannot re-call start of asynchronous " +
687                                                                 "method while a previous call is still in progress.");
688                                 }
689         
690                                 asyncWrite = new WebAsyncResult (this, callback, state);
691                                 initialMethod = method;
692                                 if (haveRequest) {
693                                         if (writeStream != null) {
694                                                 asyncWrite.SetCompleted (true, writeStream);
695                                                 asyncWrite.DoCallback ();
696                                                 return asyncWrite;
697                                         }
698                                 }
699                                 
700                                 gotRequestStream = true;
701                                 WebAsyncResult result = asyncWrite;
702                                 if (!requestSent) {
703                                         requestSent = true;
704                                         redirects = 0;
705                                         servicePoint = GetServicePoint ();
706                                         abortHandler = servicePoint.SendRequest (this, connectionGroup);
707                                 }
708                                 return result;
709                         }
710                 }
711
712                 public override Stream EndGetRequestStream (IAsyncResult asyncResult)
713                 {
714                         if (asyncResult == null)
715                                 throw new ArgumentNullException ("asyncResult");
716
717                         WebAsyncResult result = asyncResult as WebAsyncResult;
718                         if (result == null)
719                                 throw new ArgumentException ("Invalid IAsyncResult");
720
721                         asyncWrite = result;
722                         result.WaitUntilComplete ();
723
724                         Exception e = result.Exception;
725                         if (e != null)
726                                 throw e;
727
728                         return result.WriteStream;
729                 }
730                 
731                 public override Stream GetRequestStream()
732                 {
733                         IAsyncResult asyncResult = asyncWrite;
734                         if (asyncResult == null) {
735                                 asyncResult = BeginGetRequestStream (null, null);
736                                 asyncWrite = (WebAsyncResult) asyncResult;
737                         }
738
739                         if (!asyncResult.AsyncWaitHandle.WaitOne (timeout, false)) {
740                                 Abort ();
741                                 throw new WebException ("The request timed out", WebExceptionStatus.Timeout);
742                         }
743
744                         return EndGetRequestStream (asyncResult);
745                 }
746
747                 void CheckIfForceWrite ()
748                 {
749                         if (writeStream == null || writeStream.RequestWritten|| contentLength < 0 || !InternalAllowBuffering)
750                                 return;
751
752                         // This will write the POST/PUT if the write stream already has the expected
753                         // amount of bytes in it (ContentLength) (bug #77753).
754                         if (writeStream.WriteBufferLength == contentLength)
755                                 writeStream.WriteRequest ();
756                 }
757
758                 public override IAsyncResult BeginGetResponse (AsyncCallback callback, object state)
759                 {
760                         if (Aborted)
761                                 throw new WebException ("The request was canceled.", WebExceptionStatus.RequestCanceled);
762
763                         if (method == null)
764                                 throw new ProtocolViolationException ("Method is null.");
765
766                         string transferEncoding = TransferEncoding;
767                         if (!sendChunked && transferEncoding != null && transferEncoding.Trim () != "")
768                                 throw new ProtocolViolationException ("SendChunked should be true.");
769
770                         Monitor.Enter (locker);
771                         getResponseCalled = true;
772                         if (asyncRead != null && !haveResponse) {
773                                 Monitor.Exit (locker);
774                                 throw new InvalidOperationException ("Cannot re-call start of asynchronous " +
775                                                         "method while a previous call is still in progress.");
776                         }
777
778                         CheckIfForceWrite ();
779                         asyncRead = new WebAsyncResult (this, callback, state);
780                         WebAsyncResult aread = asyncRead;
781                         initialMethod = method;
782                         if (haveResponse) {
783                                 Exception saved = saved_exc;
784                                 if (webResponse != null) {
785                                         Monitor.Exit (locker);
786                                         if (saved == null) {
787                                                 aread.SetCompleted (true, webResponse);
788                                         } else {
789                                                 aread.SetCompleted (true, saved);
790                                         }
791                                         aread.DoCallback ();
792                                         return aread;
793                                 } else if (saved != null) {
794                                         Monitor.Exit (locker);
795                                         aread.SetCompleted (true, saved);
796                                         aread.DoCallback ();
797                                         return aread;
798                                 }
799                         }
800                         
801                         if (!requestSent) {
802                                 requestSent = true;
803                                 redirects = 0;
804                                 servicePoint = GetServicePoint ();
805                                 abortHandler = servicePoint.SendRequest (this, connectionGroup);
806                         }
807
808                         Monitor.Exit (locker);
809                         return aread;
810                 }
811                 
812                 public override WebResponse EndGetResponse (IAsyncResult asyncResult)
813                 {
814                         if (asyncResult == null)
815                                 throw new ArgumentNullException ("asyncResult");
816
817                         WebAsyncResult result = asyncResult as WebAsyncResult;
818                         if (result == null)
819                                 throw new ArgumentException ("Invalid IAsyncResult", "asyncResult");
820
821                         if (!result.WaitUntilComplete (timeout, false)) {
822                                 Abort ();
823                                 throw new WebException("The request timed out", WebExceptionStatus.Timeout);
824                         }
825
826                         if (result.GotException)
827                                 throw result.Exception;
828
829                         return result.Response;
830                 }
831
832                 public override WebResponse GetResponse()
833                 {
834                         WebAsyncResult result = (WebAsyncResult) BeginGetResponse (null, null);
835                         return EndGetResponse (result);
836                 }
837                 
838                 internal bool FinishedReading {
839                         get { return finished_reading; }
840                         set { finished_reading = value; }
841                 }
842
843                 internal bool Aborted {
844                         get { return Interlocked.CompareExchange (ref aborted, 0, 0) == 1; }
845                 }
846
847                 public override void Abort ()
848                 {
849                         if (Interlocked.CompareExchange (ref aborted, 1, 0) == 1)
850                                 return;
851
852                         if (haveResponse && finished_reading)
853                                 return;
854
855                         haveResponse = true;
856                         if (abortHandler != null) {
857                                 try {
858                                         abortHandler (this, EventArgs.Empty);
859                                 } catch (Exception) {}
860                                 abortHandler = null;
861                         }
862
863                         if (asyncWrite != null) {
864                                 WebAsyncResult r = asyncWrite;
865                                 if (!r.IsCompleted) {
866                                         try {
867                                                 WebException wexc = new WebException ("Aborted.", WebExceptionStatus.RequestCanceled); 
868                                                 r.SetCompleted (false, wexc);
869                                                 r.DoCallback ();
870                                         } catch {}
871                                 }
872                                 asyncWrite = null;
873                         }                       
874
875                         if (asyncRead != null) {
876                                 WebAsyncResult r = asyncRead;
877                                 if (!r.IsCompleted) {
878                                         try {
879                                                 WebException wexc = new WebException ("Aborted.", WebExceptionStatus.RequestCanceled); 
880                                                 r.SetCompleted (false, wexc);
881                                                 r.DoCallback ();
882                                         } catch {}
883                                 }
884                                 asyncRead = null;
885                         }                       
886
887                         if (writeStream != null) {
888                                 try {
889                                         writeStream.Close ();
890                                         writeStream = null;
891                                 } catch {}
892                         }
893
894                         if (webResponse != null) {
895                                 try {
896                                         webResponse.Close ();
897                                         webResponse = null;
898                                 } catch {}
899                         }
900                 }               
901                 
902                 void ISerializable.GetObjectData (SerializationInfo serializationInfo,
903                                                   StreamingContext streamingContext)
904                 {
905                         GetObjectData (serializationInfo, streamingContext);
906                 }
907
908                 protected override void GetObjectData (SerializationInfo serializationInfo,
909                         StreamingContext streamingContext)
910                 {
911                         SerializationInfo info = serializationInfo;
912
913                         info.AddValue ("requestUri", requestUri, typeof (Uri));
914                         info.AddValue ("actualUri", actualUri, typeof (Uri));
915                         info.AddValue ("allowAutoRedirect", allowAutoRedirect);
916                         info.AddValue ("allowBuffering", allowBuffering);
917                         info.AddValue ("certificates", certificates, typeof (X509CertificateCollection));
918                         info.AddValue ("connectionGroup", connectionGroup);
919                         info.AddValue ("contentLength", contentLength);
920                         info.AddValue ("webHeaders", webHeaders, typeof (WebHeaderCollection));
921                         info.AddValue ("keepAlive", keepAlive);
922                         info.AddValue ("maxAutoRedirect", maxAutoRedirect);
923                         info.AddValue ("mediaType", mediaType);
924                         info.AddValue ("method", method);
925                         info.AddValue ("initialMethod", initialMethod);
926                         info.AddValue ("pipelined", pipelined);
927                         info.AddValue ("version", version, typeof (Version));
928                         info.AddValue ("proxy", proxy, typeof (IWebProxy));
929                         info.AddValue ("sendChunked", sendChunked);
930                         info.AddValue ("timeout", timeout);
931                         info.AddValue ("redirects", redirects);
932                         info.AddValue ("host", host);
933                 }
934                 
935                 void CheckRequestStarted () 
936                 {
937                         if (requestSent)
938                                 throw new InvalidOperationException ("request started");
939                 }
940
941                 internal void DoContinueDelegate (int statusCode, WebHeaderCollection headers)
942                 {
943                         if (continueDelegate != null)
944                                 continueDelegate (statusCode, headers);
945                 }
946                 
947                 bool Redirect (WebAsyncResult result, HttpStatusCode code)
948                 {
949                         redirects++;
950                         Exception e = null;
951                         string uriString = null;
952
953                         switch (code) {
954                         case HttpStatusCode.Ambiguous: // 300
955                                 e = new WebException ("Ambiguous redirect.");
956                                 break;
957                         case HttpStatusCode.MovedPermanently: // 301
958                         case HttpStatusCode.Redirect: // 302
959                         case HttpStatusCode.TemporaryRedirect: // 307
960                                 /* MS follows the redirect for POST too
961                                 if (method != "GET" && method != "HEAD") // 10.3
962                                         return false;
963                                 */
964
965                                 contentLength = -1;
966                                 bodyBufferLength = 0;
967                                 bodyBuffer = null;
968                                 if (code != HttpStatusCode.TemporaryRedirect)
969                                         method = "GET";
970                                 uriString = webResponse.Headers ["Location"];
971                                 break;
972                         case HttpStatusCode.SeeOther: //303
973                                 method = "GET";
974                                 uriString = webResponse.Headers ["Location"];
975                                 break;
976                         case HttpStatusCode.NotModified: // 304
977                                 return false;
978                         case HttpStatusCode.UseProxy: // 305
979                                 e = new NotImplementedException ("Proxy support not available.");
980                                 break;
981                         case HttpStatusCode.Unused: // 306
982                         default:
983                                 e = new ProtocolViolationException ("Invalid status code: " + (int) code);
984                                 break;
985                         }
986
987                         if (e != null)
988                                 throw e;
989
990                         if (uriString == null)
991                                 throw new WebException ("No Location header found for " + (int) code,
992                                                         WebExceptionStatus.ProtocolError);
993
994                         Uri prev = actualUri;
995                         try {
996                                 actualUri = new Uri (actualUri, uriString);
997                         } catch (Exception) {
998                                 throw new WebException (String.Format ("Invalid URL ({0}) for {1}",
999                                                                         uriString, (int) code),
1000                                                                         WebExceptionStatus.ProtocolError);
1001                         }
1002
1003                         hostChanged = (actualUri.Scheme != prev.Scheme || Host != prev.Authority);
1004                         return true;
1005                 }
1006
1007                 string GetHeaders ()
1008                 {
1009                         bool continue100 = false;
1010                         if (sendChunked) {
1011                                 continue100 = true;
1012                                 webHeaders.RemoveAndAdd ("Transfer-Encoding", "chunked");
1013                                 webHeaders.RemoveInternal ("Content-Length");
1014                         } else if (contentLength != -1) {
1015                                 if (ntlm_auth_state != NtlmAuthState.Challenge) {
1016                                         if (contentLength > 0)
1017                                                 continue100 = true;
1018
1019                                         webHeaders.SetInternal ("Content-Length", contentLength.ToString ());
1020                                 } else {
1021                                         webHeaders.SetInternal ("Content-Length", "0");
1022                                 }
1023                                 webHeaders.RemoveInternal ("Transfer-Encoding");
1024                         } else {
1025                                 webHeaders.RemoveInternal ("Content-Length");
1026                         }
1027
1028                         if (actualVersion == HttpVersion.Version11 && continue100 &&
1029                             servicePoint.SendContinue) { // RFC2616 8.2.3
1030                                 webHeaders.RemoveAndAdd ("Expect" , "100-continue");
1031                                 expectContinue = true;
1032                         } else {
1033                                 webHeaders.RemoveInternal ("Expect");
1034                                 expectContinue = false;
1035                         }
1036
1037                         bool proxy_query = ProxyQuery;
1038                         string connectionHeader = (proxy_query) ? "Proxy-Connection" : "Connection";
1039                         webHeaders.RemoveInternal ((!proxy_query) ? "Proxy-Connection" : "Connection");
1040                         Version proto_version = servicePoint.ProtocolVersion;
1041                         bool spoint10 = (proto_version == null || proto_version == HttpVersion.Version10);
1042
1043                         if (keepAlive && (version == HttpVersion.Version10 || spoint10)) {
1044                                 webHeaders.RemoveAndAdd (connectionHeader, "keep-alive");
1045                         } else if (!keepAlive && version == HttpVersion.Version11) {
1046                                 webHeaders.RemoveAndAdd (connectionHeader, "close");
1047                         }
1048
1049                         webHeaders.SetInternal ("Host", Host);
1050                         if (cookieContainer != null) {
1051                                 string cookieHeader = cookieContainer.GetCookieHeader (actualUri);
1052                                 if (cookieHeader != "")
1053                                         webHeaders.SetInternal ("Cookie", cookieHeader);
1054                         }
1055
1056                         string accept_encoding = null;
1057                         if ((auto_decomp & DecompressionMethods.GZip) != 0)
1058                                 accept_encoding = "gzip";
1059                         if ((auto_decomp & DecompressionMethods.Deflate) != 0)
1060                                 accept_encoding = accept_encoding != null ? "gzip, deflate" : "deflate";
1061                         if (accept_encoding != null)
1062                                 webHeaders.RemoveAndAdd ("Accept-Encoding", accept_encoding);
1063
1064                         if (!usedPreAuth && preAuthenticate)
1065                                 DoPreAuthenticate ();
1066
1067                         return webHeaders.ToString ();
1068                 }
1069
1070                 void DoPreAuthenticate ()
1071                 {
1072                         bool isProxy = (proxy != null && !proxy.IsBypassed (actualUri));
1073                         ICredentials creds = (!isProxy || credentials != null) ? credentials : proxy.Credentials;
1074                         Authorization auth = AuthenticationManager.PreAuthenticate (this, creds);
1075                         if (auth == null)
1076                                 return;
1077
1078                         webHeaders.RemoveInternal ("Proxy-Authorization");
1079                         webHeaders.RemoveInternal ("Authorization");
1080                         string authHeader = (isProxy && credentials == null) ? "Proxy-Authorization" : "Authorization";
1081                         webHeaders [authHeader] = auth.Message;
1082                         usedPreAuth = true;
1083                 }
1084                 
1085                 internal void SetWriteStreamError (WebExceptionStatus status, Exception exc)
1086                 {
1087                         if (Aborted)
1088                                 return;
1089
1090                         WebAsyncResult r = asyncWrite;
1091                         if (r == null)
1092                                 r = asyncRead;
1093
1094                         if (r != null) {
1095                                 string msg;
1096                                 WebException wex;
1097                                 if (exc == null) {
1098                                         msg = "Error: " + status;
1099                                         wex = new WebException (msg, status);
1100                                 } else {
1101                                         msg = String.Format ("Error: {0} ({1})", status, exc.Message);
1102                                         wex = new WebException (msg, exc, status);
1103                                 }
1104                                 r.SetCompleted (false, wex);
1105                                 r.DoCallback ();
1106                         }
1107                 }
1108
1109                 internal void SendRequestHeaders (bool propagate_error)
1110                 {
1111                         StringBuilder req = new StringBuilder ();
1112                         string query;
1113                         if (!ProxyQuery) {
1114                                 query = actualUri.PathAndQuery;
1115                         } else {
1116                                 query = String.Format ("{0}://{1}{2}",  actualUri.Scheme,
1117                                                                         Host,
1118                                                                         actualUri.PathAndQuery);
1119                         }
1120                         
1121                         if (servicePoint.ProtocolVersion != null && servicePoint.ProtocolVersion < version) {
1122                                 actualVersion = servicePoint.ProtocolVersion;
1123                         } else {
1124                                 actualVersion = version;
1125                         }
1126
1127                         req.AppendFormat ("{0} {1} HTTP/{2}.{3}\r\n", method, query,
1128                                                                 actualVersion.Major, actualVersion.Minor);
1129                         req.Append (GetHeaders ());
1130                         string reqstr = req.ToString ();
1131                         byte [] bytes = Encoding.UTF8.GetBytes (reqstr);
1132                         try {
1133                                 writeStream.SetHeaders (bytes);
1134                         } catch (WebException wexc) {
1135                                 SetWriteStreamError (wexc.Status, wexc);
1136                                 if (propagate_error)
1137                                         throw;
1138                         } catch (Exception exc) {
1139                                 SetWriteStreamError (WebExceptionStatus.SendFailure, exc);
1140                                 if (propagate_error)
1141                                         throw;
1142                         }
1143                 }
1144
1145                 internal void SetWriteStream (WebConnectionStream stream)
1146                 {
1147                         if (Aborted)
1148                                 return;
1149                         
1150                         writeStream = stream;
1151                         if (bodyBuffer != null) {
1152                                 webHeaders.RemoveInternal ("Transfer-Encoding");
1153                                 contentLength = bodyBufferLength;
1154                                 writeStream.SendChunked = false;
1155                         }
1156
1157                         SendRequestHeaders (false);
1158
1159                         haveRequest = true;
1160                         
1161                         if (bodyBuffer != null) {
1162                                 // The body has been written and buffered. The request "user"
1163                                 // won't write it again, so we must do it.
1164                                 if (ntlm_auth_state != NtlmAuthState.Challenge) {
1165                                         writeStream.Write (bodyBuffer, 0, bodyBufferLength);
1166                                         bodyBuffer = null;
1167                                         writeStream.Close ();
1168                                 }
1169                         } else if (method != "HEAD" && method != "GET" && method != "MKCOL" && method != "CONNECT" &&
1170                                         method != "TRACE") {
1171                                 if (getResponseCalled && !writeStream.RequestWritten)
1172                                         writeStream.WriteRequest ();
1173                         }
1174
1175                         if (asyncWrite != null) {
1176                                 asyncWrite.SetCompleted (false, stream);
1177                                 asyncWrite.DoCallback ();
1178                                 asyncWrite = null;
1179                         }
1180                 }
1181
1182                 internal void SetResponseError (WebExceptionStatus status, Exception e, string where)
1183                 {
1184                         if (Aborted)
1185                                 return;
1186                         lock (locker) {
1187                         string msg = String.Format ("Error getting response stream ({0}): {1}", where, status);
1188                         WebAsyncResult r = asyncRead;
1189                         if (r == null)
1190                                 r = asyncWrite;
1191
1192                         WebException wexc;
1193                         if (e is WebException) {
1194                                 wexc = (WebException) e;
1195                         } else {
1196                                 wexc = new WebException (msg, e, status, null); 
1197                         }
1198                         if (r != null) {
1199                                 if (!r.IsCompleted) {
1200                                         r.SetCompleted (false, wexc);
1201                                         r.DoCallback ();
1202                                 } else if (r == asyncWrite) {
1203                                         saved_exc = wexc;
1204                                 }
1205                                 haveResponse = true;
1206                                 asyncRead = null;
1207                                 asyncWrite = null;
1208                         } else {
1209                                 haveResponse = true;
1210                                 saved_exc = wexc;
1211                         }
1212                         }
1213                 }
1214
1215                 void CheckSendError (WebConnectionData data)
1216                 {
1217                         // Got here, but no one called GetResponse
1218                         int status = data.StatusCode;
1219                         if (status < 400 || status == 401 || status == 407)
1220                                 return;
1221
1222                         if (writeStream != null && asyncRead == null && !writeStream.CompleteRequestWritten) {
1223                                 // The request has not been completely sent and we got here!
1224                                 // We should probably just close and cause an error in any case,
1225                                 saved_exc = new WebException (data.StatusDescription, null, WebExceptionStatus.ProtocolError, webResponse); 
1226                                 webResponse.ReadAll ();
1227                         }
1228                 }
1229
1230                 void HandleNtlmAuth (WebAsyncResult r)
1231                 {
1232                         WebConnectionStream wce = webResponse.GetResponseStream () as WebConnectionStream;
1233                         if (wce != null) {
1234                                 WebConnection cnc = wce.Connection;
1235                                 cnc.PriorityRequest = this;
1236                                 bool isProxy = (proxy != null && !proxy.IsBypassed (actualUri));
1237                                 ICredentials creds = (!isProxy) ? credentials : proxy.Credentials;
1238                                 if (creds != null) {
1239                                         cnc.NtlmCredential = creds.GetCredential (requestUri, "NTLM");
1240                                         cnc.UnsafeAuthenticatedConnectionSharing = unsafe_auth_blah;
1241                                 }
1242                         }
1243                         r.Reset ();
1244                         finished_reading = false;
1245                         haveResponse = false;
1246                         webResponse.ReadAll ();
1247                         webResponse = null;
1248                 }
1249
1250                 internal void SetResponseData (WebConnectionData data)
1251                 {
1252                         lock (locker) {
1253                         if (Aborted) {
1254                                 if (data.stream != null)
1255                                         data.stream.Close ();
1256                                 return;
1257                         }
1258
1259                         WebException wexc = null;
1260                         try {
1261                                 webResponse = new HttpWebResponse (actualUri, method, data, cookieContainer);
1262                         } catch (Exception e) {
1263                                 wexc = new WebException (e.Message, e, WebExceptionStatus.ProtocolError, null); 
1264                                 if (data.stream != null)
1265                                         data.stream.Close ();
1266                         }
1267
1268                         if (wexc == null && (method == "POST" || method == "PUT")) {
1269                                 lock (locker) {
1270                                         CheckSendError (data);
1271                                         if (saved_exc != null)
1272                                                 wexc = (WebException) saved_exc;
1273                                 }
1274                         }
1275
1276                         WebAsyncResult r = asyncRead;
1277
1278                         bool forced = false;
1279                         if (r == null && webResponse != null) {
1280                                 // This is a forced completion (302, 204)...
1281                                 forced = true;
1282                                 r = new WebAsyncResult (null, null);
1283                                 r.SetCompleted (false, webResponse);
1284                         }
1285
1286                         if (r != null) {
1287                                 if (wexc != null) {
1288                                         haveResponse = true;
1289                                         r.SetCompleted (false, wexc);
1290                                         r.DoCallback ();
1291                                         return;
1292                                 }
1293
1294                                 bool redirected;
1295                                 try {
1296                                         redirected = CheckFinalStatus (r);
1297                                         if (!redirected) {
1298                                                 if (ntlm_auth_state != NtlmAuthState.None && authCompleted && webResponse != null
1299                                                         && (int)webResponse.StatusCode < 400) {
1300                                                         WebConnectionStream wce = webResponse.GetResponseStream () as WebConnectionStream;
1301                                                         if (wce != null) {
1302                                                                 WebConnection cnc = wce.Connection;
1303                                                                 cnc.NtlmAuthenticated = true;
1304                                                         }
1305                                                 }
1306
1307                                                 // clear internal buffer so that it does not
1308                                                 // hold possible big buffer (bug #397627)
1309                                                 if (writeStream != null)
1310                                                         writeStream.KillBuffer ();
1311
1312                                                 haveResponse = true;
1313                                                 r.SetCompleted (false, webResponse);
1314                                                 r.DoCallback ();
1315                                         } else {
1316                                                 if (webResponse != null) {
1317                                                         if (ntlm_auth_state != NtlmAuthState.None) {
1318                                                                 HandleNtlmAuth (r);
1319                                                                 return;
1320                                                         }
1321                                                         webResponse.Close ();
1322                                                 }
1323                                                 finished_reading = false;
1324                                                 haveResponse = false;
1325                                                 webResponse = null;
1326                                                 r.Reset ();
1327                                                 servicePoint = GetServicePoint ();
1328                                                 abortHandler = servicePoint.SendRequest (this, connectionGroup);
1329                                         }
1330                                 } catch (WebException wexc2) {
1331                                         if (forced) {
1332                                                 saved_exc = wexc2;
1333                                                 haveResponse = true;
1334                                         }
1335                                         r.SetCompleted (false, wexc2);
1336                                         r.DoCallback ();
1337                                         return;
1338                                 } catch (Exception ex) {
1339                                         wexc = new WebException (ex.Message, ex, WebExceptionStatus.ProtocolError, null); 
1340                                         if (forced) {
1341                                                 saved_exc = wexc;
1342                                                 haveResponse = true;
1343                                         }
1344                                         r.SetCompleted (false, wexc);
1345                                         r.DoCallback ();
1346                                         return;
1347                                 }
1348                         }
1349                         }
1350                 }
1351
1352                 bool CheckAuthorization (WebResponse response, HttpStatusCode code)
1353                 {
1354                         authCompleted = false;
1355                         if (code == HttpStatusCode.Unauthorized && credentials == null)
1356                                 return false;
1357
1358                         bool isProxy = (code == HttpStatusCode.ProxyAuthenticationRequired);
1359                         if (isProxy && (proxy == null || proxy.Credentials == null))
1360                                 return false;
1361
1362                         string [] authHeaders = response.Headers.GetValues ( (isProxy) ? "Proxy-Authenticate" : "WWW-Authenticate");
1363                         if (authHeaders == null || authHeaders.Length == 0)
1364                                 return false;
1365
1366                         ICredentials creds = (!isProxy) ? credentials : proxy.Credentials;
1367                         Authorization auth = null;
1368                         foreach (string authHeader in authHeaders) {
1369                                 auth = AuthenticationManager.Authenticate (authHeader, this, creds);
1370                                 if (auth != null)
1371                                         break;
1372                         }
1373                         if (auth == null)
1374                                 return false;
1375                         webHeaders [(isProxy) ? "Proxy-Authorization" : "Authorization"] = auth.Message;
1376                         authCompleted = auth.Complete;
1377                         bool is_ntlm = (auth.Module.AuthenticationType == "NTLM");
1378                         if (is_ntlm)
1379                                 ntlm_auth_state = (NtlmAuthState)((int) ntlm_auth_state + 1);
1380                         return true;
1381                 }
1382
1383                 // Returns true if redirected
1384                 bool CheckFinalStatus (WebAsyncResult result)
1385                 {
1386                         if (result.GotException) {
1387                                 bodyBuffer = null;
1388                                 throw result.Exception;
1389                         }
1390
1391                         Exception throwMe = result.Exception;
1392
1393                         HttpWebResponse resp = result.Response;
1394                         WebExceptionStatus protoError = WebExceptionStatus.ProtocolError;
1395                         HttpStatusCode code = 0;
1396                         if (throwMe == null && webResponse != null) {
1397                                 code = webResponse.StatusCode;
1398                                 if (!authCompleted && ((code == HttpStatusCode.Unauthorized && credentials != null) ||
1399                                      (ProxyQuery && code == HttpStatusCode.ProxyAuthenticationRequired))) {
1400                                         if (!usedPreAuth && CheckAuthorization (webResponse, code)) {
1401                                                 // Keep the written body, so it can be rewritten in the retry
1402                                                 if (InternalAllowBuffering) {
1403                                                         // NTLM: This is to avoid sending data in the 'challenge' request
1404                                                         // We save it in the first request (first 401), don't send anything
1405                                                         // in the challenge request and send it in the response request along
1406                                                         // with the buffers kept form the first request.
1407                                                         if (ntlm_auth_state != NtlmAuthState.Response) {
1408                                                                 bodyBuffer = writeStream.WriteBuffer;
1409                                                                 bodyBufferLength = writeStream.WriteBufferLength;
1410                                                         }
1411                                                         return true;
1412                                                 } else if (method != "PUT" && method != "POST") {
1413                                                         bodyBuffer = null;
1414                                                         return true;
1415                                                 }
1416                                                 
1417                                                 writeStream.InternalClose ();
1418                                                 writeStream = null;
1419                                                 webResponse.Close ();
1420                                                 webResponse = null;
1421                                                 bodyBuffer = null;
1422
1423                                                 throw new WebException ("This request requires buffering " +
1424                                                                         "of data for authentication or " +
1425                                                                         "redirection to be sucessful.");
1426                                         }
1427                                 }
1428
1429                                 bodyBuffer = null;
1430                                 if ((int) code >= 400) {
1431                                         string err = String.Format ("The remote server returned an error: ({0}) {1}.",
1432                                                                     (int) code, webResponse.StatusDescription);
1433                                         throwMe = new WebException (err, null, protoError, webResponse);
1434                                         webResponse.ReadAll ();
1435                                 } else if ((int) code == 304 && allowAutoRedirect) {
1436                                         string err = String.Format ("The remote server returned an error: ({0}) {1}.",
1437                                                                     (int) code, webResponse.StatusDescription);
1438                                         throwMe = new WebException (err, null, protoError, webResponse);
1439                                 } else if ((int) code >= 300 && allowAutoRedirect && redirects >= maxAutoRedirect) {
1440                                         throwMe = new WebException ("Max. redirections exceeded.", null,
1441                                                                     protoError, webResponse);
1442                                         webResponse.ReadAll ();
1443                                 }
1444                         }
1445
1446                         bodyBuffer = null;
1447                         if (throwMe == null) {
1448                                 bool b = false;
1449                                 int c = (int) code;
1450                                 if (allowAutoRedirect && c >= 300) {
1451                                         if (InternalAllowBuffering && writeStream.WriteBufferLength > 0) {
1452                                                 bodyBuffer = writeStream.WriteBuffer;
1453                                                 bodyBufferLength = writeStream.WriteBufferLength;
1454                                         }
1455                                         b = Redirect (result, code);
1456                                 }
1457
1458                                 if (resp != null && c >= 300 && c != 304)
1459                                         resp.ReadAll ();
1460
1461                                 return b;
1462                         }
1463
1464                         if (writeStream != null) {
1465                                 writeStream.InternalClose ();
1466                                 writeStream = null;
1467                         }
1468
1469                         webResponse = null;
1470
1471                         throw throwMe;
1472                 }
1473         }
1474 }
1475