2 // System.Net.HttpWebRequest
5 // Lawrence Pit (loz@cable.a2000.nl)
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
8 // (c) 2002 Lawrence Pit
9 // (c) 2003 Ximian, Inc. (http://www.ximian.com)
13 using System.Collections;
15 using System.Net.Sockets;
16 using System.Runtime.Remoting.Messaging;
17 using System.Runtime.Serialization;
18 using System.Security.Cryptography.X509Certificates;
20 using System.Threading;
25 public class HttpWebRequest : WebRequest, ISerializable
30 bool allowAutoRedirect = true;
31 bool allowBuffering = true;
32 X509CertificateCollection certificates;
33 string connectionGroup;
34 long contentLength = -1;
35 HttpContinueDelegate continueDelegate;
36 CookieContainer cookieContainer;
37 ICredentials credentials;
41 WebHeaderCollection webHeaders = new WebHeaderCollection (true);
42 bool keepAlive = true;
43 int maxAutoRedirect = 50;
44 string mediaType = String.Empty;
45 string method = "GET";
46 string initialMethod = "GET";
47 bool pipelined = true;
49 Version version = HttpVersion.Version11;
52 ServicePoint servicePoint;
55 WebConnectionStream writeStream;
56 HttpWebResponse webResponse;
57 WebAsyncResult asyncWrite;
58 WebAsyncResult asyncRead;
59 EventHandler abortHandler;
61 bool gotRequestStream;
70 internal HttpWebRequest (Uri uri)
72 this.requestUri = uri;
74 this.proxy = GlobalProxySelection.Select;
77 protected HttpWebRequest (SerializationInfo serializationInfo, StreamingContext streamingContext)
79 SerializationInfo info = serializationInfo;
81 requestUri = (Uri) info.GetValue ("requestUri", typeof (Uri));
82 actualUri = (Uri) info.GetValue ("actualUri", typeof (Uri));
83 allowAutoRedirect = info.GetBoolean ("allowAutoRedirect");
84 allowBuffering = info.GetBoolean ("allowBuffering");
85 certificates = (X509CertificateCollection) info.GetValue ("certificates", typeof (X509CertificateCollection));
86 connectionGroup = info.GetString ("connectionGroup");
87 contentLength = info.GetInt64 ("contentLength");
88 webHeaders = (WebHeaderCollection) info.GetValue ("webHeaders", typeof (WebHeaderCollection));
89 keepAlive = info.GetBoolean ("keepAlive");
90 maxAutoRedirect = info.GetInt32 ("maxAutoRedirect");
91 mediaType = info.GetString ("mediaType");
92 method = info.GetString ("method");
93 initialMethod = info.GetString ("initialMethod");
94 pipelined = info.GetBoolean ("pipelined");
95 version = (Version) info.GetValue ("version", typeof (Version));
96 proxy = (IWebProxy) info.GetValue ("proxy", typeof (IWebProxy));
97 sendChunked = info.GetBoolean ("sendChunked");
98 timeout = info.GetInt32 ("timeout");
99 redirects = info.GetInt32 ("redirects");
104 public string Accept {
105 get { return webHeaders ["Accept"]; }
107 CheckRequestStarted ();
108 webHeaders.SetInternal ("Accept", value);
113 get { return actualUri; }
116 public bool AllowAutoRedirect {
117 get { return allowAutoRedirect; }
118 set { this.allowAutoRedirect = value; }
121 public bool AllowWriteStreamBuffering {
122 get { return allowBuffering; }
123 set { allowBuffering = value; }
126 internal bool InternalAllowBuffering {
128 return (allowBuffering && (method == "PUT" || method == "POST"));
132 public X509CertificateCollection ClientCertificates {
134 if (certificates == null)
135 certificates = new X509CertificateCollection ();
141 public string Connection {
142 get { return webHeaders ["Connection"]; }
144 CheckRequestStarted ();
147 val = val.Trim ().ToLower ();
149 if (val == null || val.Length == 0) {
150 webHeaders.RemoveInternal ("Connection");
154 if (val == "keep-alive" || val == "close")
155 throw new ArgumentException ("Keep-Alive and Close may not be set with this property");
157 if (keepAlive && val.IndexOf ("keep-alive") == -1)
158 value = value + ", Keep-Alive";
160 webHeaders.SetInternal ("Connection", value);
164 public override string ConnectionGroupName {
165 get { return connectionGroup; }
166 set { connectionGroup = value; }
169 public override long ContentLength {
170 get { return contentLength; }
172 CheckRequestStarted ();
174 throw new ArgumentOutOfRangeException ("value", "Content-Length must be >= 0");
176 contentLength = value;
180 internal long InternalContentLength {
181 set { contentLength = value; }
184 public override string ContentType {
185 get { return webHeaders ["Content-Type"]; }
187 CheckRequestStarted ();
188 if (value == null || value.Trim().Length == 0) {
189 webHeaders.RemoveInternal ("Content-Type");
192 webHeaders.SetInternal ("Content-Type", value);
196 public HttpContinueDelegate ContinueDelegate {
197 get { return continueDelegate; }
198 set { continueDelegate = value; }
201 public CookieContainer CookieContainer {
202 get { return cookieContainer; }
203 set { cookieContainer = value; }
206 public override ICredentials Credentials {
207 get { return credentials; }
208 set { credentials = value; }
211 public string Expect {
212 get { return webHeaders ["Expect"]; }
214 CheckRequestStarted ();
217 val = val.Trim ().ToLower ();
219 if (val == null || val.Length == 0) {
220 webHeaders.RemoveInternal ("Expect");
224 if (val == "100-continue")
225 throw new ArgumentException ("100-Continue cannot be set with this property.",
227 webHeaders.SetInternal ("Expect", value);
231 public bool HaveResponse {
232 get { return haveResponse; }
235 public override WebHeaderCollection Headers {
236 get { return webHeaders; }
238 CheckRequestStarted ();
239 WebHeaderCollection newHeaders = new WebHeaderCollection (true);
240 int count = value.Count;
241 for (int i = 0; i < count; i++)
242 newHeaders.Add (value.GetKey (i), value.Get (i));
244 webHeaders = newHeaders;
248 public DateTime IfModifiedSince {
250 string str = webHeaders ["If-Modified-Since"];
254 return MonoHttpDate.Parse (str);
255 } catch (Exception) {
260 CheckRequestStarted ();
262 webHeaders.SetInternal ("If-Modified-Since",
263 value.ToUniversalTime ().ToString ("r", null));
264 // TODO: check last param when using different locale
268 public bool KeepAlive {
277 public int MaximumAutomaticRedirections {
278 get { return maxAutoRedirect; }
281 throw new ArgumentException ("Must be > 0", "value");
283 maxAutoRedirect = value;
287 public string MediaType {
288 get { return mediaType; }
294 public override string Method {
295 get { return this.method; }
297 if (value == null || value.Trim () == "")
298 throw new ArgumentException ("not a valid method");
304 public bool Pipelined {
305 get { return pipelined; }
306 set { pipelined = value; }
309 public override bool PreAuthenticate {
310 get { return preAuthenticate; } //TODO: support preauthentication
311 set { preAuthenticate = value; }
314 public Version ProtocolVersion {
315 get { return version; }
317 if (value != HttpVersion.Version10 && value != HttpVersion.Version11)
318 throw new ArgumentException ("value");
324 public override IWebProxy Proxy {
325 get { return proxy; }
327 CheckRequestStarted ();
329 throw new ArgumentNullException ("value");
332 servicePoint = null; // we may need a new one
336 public string Referer {
337 get { return webHeaders ["Referer"]; }
339 CheckRequestStarted ();
340 if (value == null || value.Trim().Length == 0) {
341 webHeaders.RemoveInternal ("Referer");
344 webHeaders.SetInternal ("Referer", value);
348 public override Uri RequestUri {
349 get { return requestUri; }
352 public bool SendChunked {
353 get { return sendChunked; }
355 CheckRequestStarted ();
360 public ServicePoint ServicePoint {
361 get { return GetServicePoint (); }
364 public override int Timeout {
365 get { return timeout; }
368 throw new ArgumentOutOfRangeException ("value");
374 public string TransferEncoding {
375 get { return webHeaders ["Transfer-Encoding"]; }
377 CheckRequestStarted ();
380 val = val.Trim ().ToLower ();
382 if (val == null || val.Length == 0) {
383 webHeaders.RemoveInternal ("Transfer-Encoding");
387 if (val == "chunked")
388 throw new ArgumentException ("Chunked encoding must be set with the SendChunked property");
391 throw new ArgumentException ("SendChunked must be True", "value");
393 webHeaders.SetInternal ("Transfer-Encoding", value);
397 public string UserAgent {
398 get { return webHeaders ["User-Agent"]; }
399 set { webHeaders.SetInternal ("User-Agent", value); }
404 public bool UnsafeAuthenticatedConnectionSharing
406 get { throw new NotImplementedException (); }
407 set { throw new NotImplementedException (); }
411 internal bool GotRequestStream {
412 get { return gotRequestStream; }
415 internal bool ExpectContinue {
416 get { return expectContinue; }
417 set { expectContinue = value; }
420 internal Uri AuthUri {
421 get { return actualUri; }
424 internal bool ProxyQuery {
425 get { return servicePoint.UsesProxy; }
430 internal ServicePoint GetServicePoint ()
432 if (!hostChanged && servicePoint != null)
436 if (hostChanged || servicePoint == null) {
437 servicePoint = ServicePointManager.FindServicePoint (actualUri, proxy);
445 public void AddRange (int range)
447 AddRange ("bytes", range);
450 public void AddRange (int from, int to)
452 AddRange ("bytes", from, to);
455 public void AddRange (string rangeSpecifier, int range)
457 if (rangeSpecifier == null)
458 throw new ArgumentNullException ("rangeSpecifier");
459 string value = webHeaders ["Range"];
460 if (value == null || value.Length == 0)
461 value = rangeSpecifier + "=";
462 else if (value.ToLower ().StartsWith (rangeSpecifier.ToLower () + "="))
465 throw new InvalidOperationException ("rangeSpecifier");
466 webHeaders.SetInternal ("Range", value + range + "-");
469 public void AddRange (string rangeSpecifier, int from, int to)
471 if (rangeSpecifier == null)
472 throw new ArgumentNullException ("rangeSpecifier");
473 if (from < 0 || to < 0 || from > to)
474 throw new ArgumentOutOfRangeException ();
475 string value = webHeaders ["Range"];
476 if (value == null || value.Length == 0)
477 value = rangeSpecifier + "=";
478 else if (value.ToLower ().StartsWith (rangeSpecifier.ToLower () + "="))
481 throw new InvalidOperationException ("rangeSpecifier");
482 webHeaders.SetInternal ("Range", value + from + "-" + to);
485 public override int GetHashCode ()
487 return base.GetHashCode ();
490 void CommonChecks (bool putpost)
493 throw new ProtocolViolationException ("Method is null.");
495 if (putpost && ((!keepAlive || (contentLength == -1 && !sendChunked)) && !allowBuffering))
496 throw new ProtocolViolationException ("Content-Length not set");
498 string transferEncoding = TransferEncoding;
499 if (!sendChunked && transferEncoding != null && transferEncoding.Trim () != "")
500 throw new ProtocolViolationException ("SendChunked should be true.");
503 public override IAsyncResult BeginGetRequestStream (AsyncCallback callback, object state)
506 throw new WebException ("The request was previosly aborted.");
508 bool send = !(method == "GET" || method == "CONNECT" || method == "HEAD");
509 if (method == null || !send)
510 throw new ProtocolViolationException ("Cannot send data when method is: " + method);
513 Monitor.Enter (this);
514 if (asyncWrite != null) {
516 throw new InvalidOperationException ("Cannot re-call start of asynchronous " +
517 "method while a previous call is still in progress.");
520 asyncWrite = new WebAsyncResult (this, callback, state);
521 initialMethod = method;
523 if (writeStream != null) {
525 asyncWrite.SetCompleted (true, writeStream);
526 asyncWrite.DoCallback ();
531 gotRequestStream = true;
532 WebAsyncResult result = asyncWrite;
535 servicePoint = GetServicePoint ();
536 abortHandler = servicePoint.SendRequest (this, connectionGroup);
542 public override Stream EndGetRequestStream (IAsyncResult asyncResult)
544 if (asyncResult == null)
545 throw new ArgumentNullException ("asyncResult");
547 WebAsyncResult result = asyncResult as WebAsyncResult;
549 throw new ArgumentException ("Invalid IAsyncResult");
552 result.WaitUntilComplete ();
554 Exception e = result.Exception;
558 return result.WriteStream;
561 public override Stream GetRequestStream()
563 IAsyncResult asyncResult = BeginGetRequestStream (null, null);
564 asyncWrite = (WebAsyncResult) asyncResult;
565 if (!asyncResult.AsyncWaitHandle.WaitOne (timeout, false)) {
567 throw new WebException ("The request timed out", WebExceptionStatus.Timeout);
570 return EndGetRequestStream (asyncResult);
573 public override IAsyncResult BeginGetResponse (AsyncCallback callback, object state)
575 bool send = (method == "PUT" || method == "POST");
577 if ((!KeepAlive || (ContentLength == -1 && !SendChunked)) && !AllowWriteStreamBuffering)
578 throw new ProtocolViolationException ("Content-Length not set");
582 Monitor.Enter (this);
583 if (asyncRead != null && !haveResponse) {
585 throw new InvalidOperationException ("Cannot re-call start of asynchronous " +
586 "method while a previous call is still in progress.");
589 asyncRead = new WebAsyncResult (this, callback, state);
590 initialMethod = method;
592 if (webResponse != null) {
594 asyncRead.SetCompleted (true, webResponse);
595 asyncRead.DoCallback ();
602 servicePoint = GetServicePoint ();
603 abortHandler = servicePoint.SendRequest (this, connectionGroup);
610 public override WebResponse EndGetResponse (IAsyncResult asyncResult)
612 if (asyncResult == null)
613 throw new ArgumentNullException ("asyncResult");
615 WebAsyncResult result = asyncResult as WebAsyncResult;
617 throw new ArgumentException ("Invalid IAsyncResult", "asyncResult");
620 bool redirected = false;
624 haveResponse = false;
626 servicePoint = GetServicePoint ();
627 abortHandler = servicePoint.SendRequest (this, connectionGroup);
630 if (!result.WaitUntilComplete (timeout, false)) {
632 throw new WebException("The request timed out", WebExceptionStatus.Timeout);
635 redirected = CheckFinalStatus (result);
636 } while (redirected);
638 return result.Response;
641 public override WebResponse GetResponse()
643 if (haveResponse && webResponse != null)
646 WebAsyncResult result = (WebAsyncResult) BeginGetResponse (null, null);
647 return EndGetResponse (result);
650 public override void Abort ()
654 if (asyncWrite != null) {
655 WebAsyncResult r = asyncWrite;
656 WebException wexc = new WebException ("Aborted.", WebExceptionStatus.RequestCanceled);
657 r.SetCompleted (false, wexc);
662 if (asyncRead != null) {
663 WebAsyncResult r = asyncRead;
664 WebException wexc = new WebException ("Aborted.", WebExceptionStatus.RequestCanceled);
665 r.SetCompleted (false, wexc);
670 if (abortHandler != null) {
672 abortHandler (this, EventArgs.Empty);
677 if (writeStream != null) {
679 writeStream.Close ();
684 if (webResponse != null) {
686 webResponse.Close ();
692 void ISerializable.GetObjectData (SerializationInfo serializationInfo,
693 StreamingContext streamingContext)
695 SerializationInfo info = serializationInfo;
697 info.AddValue ("requestUri", requestUri, typeof (Uri));
698 info.AddValue ("actualUri", actualUri, typeof (Uri));
699 info.AddValue ("allowAutoRedirect", allowAutoRedirect);
700 info.AddValue ("allowBuffering", allowBuffering);
701 info.AddValue ("certificates", certificates, typeof (X509CertificateCollection));
702 info.AddValue ("connectionGroup", connectionGroup);
703 info.AddValue ("contentLength", contentLength);
704 info.AddValue ("webHeaders", webHeaders, typeof (WebHeaderCollection));
705 info.AddValue ("keepAlive", keepAlive);
706 info.AddValue ("maxAutoRedirect", maxAutoRedirect);
707 info.AddValue ("mediaType", mediaType);
708 info.AddValue ("method", method);
709 info.AddValue ("initialMethod", initialMethod);
710 info.AddValue ("pipelined", pipelined);
711 info.AddValue ("version", version, typeof (Version));
712 info.AddValue ("proxy", proxy, typeof (IWebProxy));
713 info.AddValue ("sendChunked", sendChunked);
714 info.AddValue ("timeout", timeout);
715 info.AddValue ("redirects", redirects);
718 void CheckRequestStarted ()
721 throw new InvalidOperationException ("request started");
724 internal void DoContinueDelegate (int statusCode, WebHeaderCollection headers)
726 if (continueDelegate != null)
727 continueDelegate (statusCode, headers);
730 bool Redirect (WebAsyncResult result, HttpStatusCode code)
734 string uriString = null;
737 case HttpStatusCode.Ambiguous: // 300
738 e = new WebException ("Ambiguous redirect.");
740 case HttpStatusCode.MovedPermanently: // 301
741 case HttpStatusCode.Redirect: // 302
742 case HttpStatusCode.TemporaryRedirect: // 307
743 if (method != "GET" && method != "HEAD") // 10.3
746 uriString = webResponse.Headers ["Location"];
748 case HttpStatusCode.SeeOther: //303
750 uriString = webResponse.Headers ["Location"];
752 case HttpStatusCode.NotModified: // 304
754 case HttpStatusCode.UseProxy: // 305
755 e = new NotImplementedException ("Proxy support not available.");
757 case HttpStatusCode.Unused: // 306
759 e = new ProtocolViolationException ("Invalid status code: " + (int) code);
766 string host = actualUri.Host;
767 actualUri = new Uri (actualUri, uriString);
768 hostChanged = (actualUri.Host != host);
774 bool continue100 = false;
775 if (gotRequestStream && contentLength != -1) {
777 webHeaders.SetInternal ("Content-Length", contentLength.ToString ());
778 } else if (sendChunked) {
780 webHeaders.SetInternal ("Transfer-Encoding", "chunked");
783 if (version == HttpVersion.Version11 && continue100 &&
784 servicePoint.SendContinue) { // RFC2616 8.2.3
785 webHeaders.SetInternal ("Expect" , "100-continue");
786 expectContinue = true;
788 expectContinue = false;
791 string connectionHeader = (ProxyQuery) ? "Proxy-Connection" : "Connection";
792 webHeaders.RemoveInternal ((!ProxyQuery) ? "Proxy-Connection" : "Connection");
794 if (keepAlive && version == HttpVersion.Version10) {
795 webHeaders.SetInternal (connectionHeader, "keep-alive");
796 } else if (!keepAlive && version == HttpVersion.Version11) {
797 webHeaders.SetInternal (connectionHeader, "close");
800 webHeaders.SetInternal ("Host", actualUri.Host);
801 if (cookieContainer != null) {
802 string cookieHeader = cookieContainer.GetCookieHeader (requestUri);
803 if (cookieHeader != "")
804 webHeaders.SetInternal ("Cookie", cookieHeader);
807 return webHeaders.ToString ();
810 internal void SetWriteStreamError (WebExceptionStatus status)
815 WebAsyncResult r = asyncWrite;
820 r.SetCompleted (false, new WebException ("Error: " + status, status));
825 internal void SendRequestHeaders ()
827 StringBuilder req = new StringBuilder ();
830 query = actualUri.PathAndQuery;
831 } else if (actualUri.IsDefaultPort) {
832 query = String.Format ("{0}://{1}{2}", actualUri.Scheme,
834 actualUri.PathAndQuery);
836 query = String.Format ("{0}://{1}:{2}{3}", actualUri.Scheme,
839 actualUri.PathAndQuery);
842 req.AppendFormat ("{0} {1} HTTP/{2}.{3}\r\n", method, query, version.Major, version.Minor);
843 req.Append (GetHeaders ());
844 string reqstr = req.ToString ();
845 byte [] bytes = Encoding.UTF8.GetBytes (reqstr);
846 writeStream.SetHeaders (bytes, 0, bytes.Length);
849 internal void SetWriteStream (WebConnectionStream stream)
854 writeStream = stream;
855 if (bodyBuffer != null) {
856 webHeaders.RemoveInternal ("Transfer-Encoding");
857 contentLength = bodyBufferLength;
858 writeStream.SendChunked = false;
861 SendRequestHeaders ();
865 if (bodyBuffer != null) {
866 // The body has been written and buffered. The request "user"
867 // won't write it again, so we must do it.
868 writeStream.Write (bodyBuffer, 0, bodyBufferLength);
870 writeStream.Close ();
873 if (asyncWrite != null) {
874 asyncWrite.SetCompleted (false, stream);
875 asyncWrite.DoCallback ();
880 internal void SetResponseError (WebExceptionStatus status, Exception e)
882 WebAsyncResult r = asyncRead;
887 WebException wexc = new WebException ("Error getting response stream", e, status, null);
888 r.SetCompleted (false, wexc);
895 internal void SetResponseData (WebConnectionData data)
898 if (data.stream != null)
899 data.stream.Close ();
903 webResponse = new HttpWebResponse (actualUri, method, data, (cookieContainer != null));
906 WebAsyncResult r = asyncRead;
908 r.SetCompleted (false, webResponse);
913 bool CheckAuthorization (WebResponse response, HttpStatusCode code)
915 authCompleted = false;
916 if (code == HttpStatusCode.Unauthorized && credentials == null)
919 bool isProxy = (code == HttpStatusCode.ProxyAuthenticationRequired);
920 if (isProxy && (proxy == null || proxy.Credentials == null))
923 string authHeader = response.Headers [(isProxy) ? "Proxy-Authenticate" : "WWW-Authenticate"];
924 if (authHeader == null)
927 ICredentials creds = (!isProxy) ? credentials : proxy.Credentials;
928 Authorization auth = AuthenticationManager.Authenticate (authHeader, this, creds);
932 webHeaders [(isProxy) ? "Proxy-Authorization" : "Authorization"] = auth.Message;
933 authCompleted = auth.Complete;
937 // Returns true if redirected
938 bool CheckFinalStatus (WebAsyncResult result)
940 if (result.GotException)
941 throw result.Exception;
943 Exception throwMe = result.Exception;
946 HttpWebResponse resp = result.Response;
947 WebExceptionStatus protoError = WebExceptionStatus.ProtocolError;
948 HttpStatusCode code = 0;
949 if (throwMe == null && webResponse != null) {
950 code = webResponse.StatusCode;
951 if (!authCompleted && ((code == HttpStatusCode.Unauthorized && credentials != null) ||
952 code == HttpStatusCode.ProxyAuthenticationRequired)) {
953 if (CheckAuthorization (webResponse, code)) {
954 // Keep the written body, so it can be rewritten in the retry
955 if (allowBuffering) {
956 bodyBuffer = writeStream.WriteBuffer;
957 bodyBufferLength = writeStream.WriteBufferLength;
961 writeStream.InternalClose ();
964 throw new WebException ("This request requires buffering " +
965 "of data for authentication or " +
966 "redirection to be sucessful.");
970 if ((int) code >= 400) {
971 string err = String.Format ("The remote server returned an error: ({0}) {1}.",
972 (int) code, webResponse.StatusDescription);
973 throwMe = new WebException (err, null, protoError, webResponse);
974 } else if ((int) code == 304 && allowAutoRedirect) {
975 string err = String.Format ("The remote server returned an error: ({0}) {1}.",
976 (int) code, webResponse.StatusDescription);
977 throwMe = new WebException (err, null, protoError, webResponse);
978 } else if ((int) code >= 300 && allowAutoRedirect && redirects > maxAutoRedirect) {
979 throwMe = new WebException ("Max. redirections exceeded.", null,
980 protoError, webResponse);
984 if (throwMe == null) {
986 if (allowAutoRedirect && (int) code >= 300)
987 b = Redirect (result, code);
992 if (writeStream != null) {
993 writeStream.InternalClose ();
997 if (webResponse != null)