3 using System.Collections;
\r
4 using System.Collections.Specialized;
\r
5 using System.Configuration;
\r
6 using System.Globalization;
\r
8 using System.Runtime;
\r
9 using System.Runtime.CompilerServices;
\r
10 using System.Runtime.Serialization;
\r
11 using System.Security;
\r
12 using System.Security.Cryptography.X509Certificates;
\r
13 using System.Security.Permissions;
\r
15 using System.Threading;
\r
18 namespace System.Net
\r
21 public class HttpWebRequest : WebRequest
\r
26 private static readonly int DEFAULT_MAX_RESP_HEADER_LEN = 64;
\r
28 private static int _defaultMaxResponseHeadersLength = DEFAULT_MAX_RESP_HEADER_LEN;
\r
31 private HttpProvider _provider;
\r
35 #region Constructors
\r
37 internal HttpWebRequest(Uri uri)
\r
39 _provider = HttpProvider.GetHttpProvider(uri);
\r
40 // Console.WriteLine("uri to string: " + uri.ToString());
\r
49 public string Accept
\r
51 get{return Headers["Accept"];}
\r
54 if(_provider.IsRequestStarted ())
\r
55 throw new InvalidOperationException ("request started");
\r
56 _provider.Headers.RemoveAndAdd ("Accept", value);
\r
62 get{return _provider.GetAddress();}
\r
65 public bool AllowAutoRedirect
\r
67 get{return _provider.AllowAutoRedirect;}
\r
68 set{_provider.AllowAutoRedirect = value;}
\r
71 public bool AllowWriteStreamBuffering
\r
73 get{return _provider.AllowWriteStreamBuffering;}
\r
74 set{_provider.AllowWriteStreamBuffering = value;}
\r
77 [MonoTODO] //documentation related
\r
78 public X509CertificateCollection ClientCertificates
\r
81 get{return _provider.GetX509Certificates();}
\r
82 [MonoNotSupported("")]
\r
83 set { throw new NotImplementedException (); }
\r
86 public string Connection
\r
88 get { return Headers["Connection"]; }
\r
91 if(_provider.IsRequestStarted())
\r
92 throw new InvalidOperationException ("request started");
\r
96 val = val.Trim ().ToLower (CultureInfo.InvariantCulture);
\r
98 if (val == null || val.Length == 0)
\r
100 Headers.RemoveInternal ("Connection");
\r
104 if (val == "keep-alive" || val == "close")
\r
105 throw new ArgumentException ("Keep-Alive and Close may not be set with this property");
\r
107 // if (this.KeepAlive && val.IndexOf ("keep-alive") == -1)
\r
108 // value = value + ", Keep-Alive";
\r
110 Headers.RemoveAndAdd ("Connection", value);
\r
114 public override string ConnectionGroupName
\r
116 get{return _provider.ConnectionGroupName;}
\r
117 set{_provider.ConnectionGroupName = value;}
\r
120 public override long ContentLength
\r
122 get{return _provider.ContentLength;}
\r
125 if(_provider.IsRequestStarted())
\r
126 throw new InvalidOperationException("Connection already opened");
\r
127 _provider.ContentLength = value;
\r
131 public override string ContentType
\r
133 get { return Headers["Content-Type"]; }
\r
136 if (value == null || value.Trim().Length == 0)
\r
138 Headers.RemoveInternal ("Content-Type");
\r
141 Headers.RemoveAndAdd ("Content-Type", value);
\r
144 [MonoTODO] //needed for automatic documentation tools,
\r
145 //since currently we don't support this feature
\r
146 public HttpContinueDelegate ContinueDelegate
\r
149 get{return _provider.ContinueDelegate;}
\r
151 set{_provider.ContinueDelegate = value;}
\r
154 public CookieContainer CookieContainer
\r
156 get{return _provider.CookieContainer;}
\r
157 set{_provider.CookieContainer = value;}
\r
160 public override ICredentials Credentials
\r
162 get{return _provider.Credentials;}
\r
163 set{_provider.Credentials = value;}
\r
166 public static int DefaultMaximumResponseHeadersLength
\r
168 get{return HttpProvider.DefaultMaxResponseHeadersLength;}
\r
169 set{HttpProvider.DefaultMaxResponseHeadersLength = value;}
\r
172 public string Expect
\r
174 get{return Headers["Expect"];}
\r
177 if(_provider.IsRequestStarted ())
\r
178 throw new InvalidOperationException("Connection already opened");
\r
179 string val = value;
\r
181 val = val.Trim ().ToLower (CultureInfo.InvariantCulture);
\r
183 if (val == null || val.Length == 0)
\r
185 Headers.RemoveInternal ("Expect");
\r
189 if (val == "100-continue")
\r
190 throw new ArgumentException ("100-Continue cannot be set with this property.",
\r
192 Headers.RemoveAndAdd ("Expect", value);
\r
196 public bool HaveResponse
\r
198 get{return _provider.IsHaveResponse();}
\r
201 public override WebHeaderCollection Headers
\r
203 get{return _provider.Headers;}
\r
204 set{_provider.Headers = value;}
\r
207 public DateTime IfModifiedSince
\r
211 string str = Headers["If-Modified-Since"];
\r
213 return DateTime.Now;
\r
216 return MonoHttpDate.Parse (str);
\r
220 return DateTime.Now;
\r
225 if(_provider.IsRequestStarted ())
\r
226 throw new InvalidOperationException("Connection already started");
\r
227 // rfc-1123 pattern
\r
228 Headers.SetInternal ("If-Modified-Since",
\r
229 value.ToUniversalTime ().ToString ("r", null));
\r
230 // TODO: check last param when using different locale
\r
234 public bool KeepAlive
\r
236 get{return _provider.KeepAlive;}
\r
237 set{_provider.KeepAlive = value;}
\r
240 public int MaximumAutomaticRedirections
\r
242 get{return _provider.MaxAutoRedirections;}
\r
243 set{_provider.MaxAutoRedirections = value;}
\r
246 [MonoTODO] //documentation
\r
247 public int MaximumResponseHeadersLength
\r
250 get{return _provider.MaximumResponseHeadersLength;}
\r
252 set{_provider.MaximumResponseHeadersLength = value;}
\r
255 public string MediaType
\r
257 get{return _provider.MediaType;}
\r
258 set{_provider.MediaType = value;}
\r
261 public override string Method
\r
263 get{return _provider.MethodName;}
\r
264 set{_provider.MethodName = value;}
\r
266 [MonoTODO] //for documentation related - limited.
\r
267 public bool Pipelined
\r
270 get{return _provider.Pipelined;}
\r
272 set{_provider.Pipelined = value;}
\r
275 public override bool PreAuthenticate
\r
277 get{return _provider.PreAuthenticate;}
\r
278 set{_provider.PreAuthenticate = value;}
\r
281 public Version ProtocolVersion
\r
283 get{return _provider.ProtocolVersion;}
\r
284 set{_provider.ProtocolVersion = value;}
\r
287 public override IWebProxy Proxy
\r
289 get{return _provider.Proxy;}
\r
290 set{_provider.Proxy = value;}
\r
293 public int ReadWriteTimeout
\r
295 get{return _provider.ReadWriteTimeout;}
\r
296 set{_provider.ReadWriteTimeout = value;}
\r
299 public string Referer
\r
301 get {return Headers["Referer"];}
\r
304 if(_provider.IsRequestStarted ())
\r
305 throw new InvalidOperationException("Connection already opened");
\r
306 if (value == null || value.Trim().Length == 0)
\r
308 Headers.RemoveInternal ("Referer");
\r
311 Headers.SetInternal ("Referer", value);
\r
314 internal Uri AuthUri
\r
316 get { return RequestUri; }
\r
318 public override Uri RequestUri
\r
320 get{return _provider.GetOriginalAddress();}
\r
323 public bool SendChunked
\r
325 get{return _provider.SendChunked;}
\r
326 set{_provider.SendChunked = value;}
\r
329 public ServicePoint ServicePoint
\r
331 get{return _provider.ServicePoint;}
\r
333 [MonoTODO] //once again - needed since our impl. still
\r
334 //doesn't support this feature we need document it..
\r
335 public override int Timeout
\r
338 get{return _provider.Timeout;}
\r
340 set{_provider.Timeout = value;}
\r
344 public string TransferEncoding
\r
346 get { return Headers ["Transfer-Encoding"]; }
\r
349 if(_provider.IsRequestStarted ())
\r
351 throw new InvalidOperationException("Connection has been already opened");
\r
353 string val = value;
\r
355 val = val.Trim ().ToLower (CultureInfo.InvariantCulture);
\r
357 if (val == null || val.Length == 0)
\r
359 Headers.RemoveInternal ("Transfer-Encoding");
\r
363 if (val == "chunked")
\r
364 throw new ArgumentException ("Chunked encoding must be set with the SendChunked property");
\r
366 if (!this.SendChunked)
\r
367 throw new InvalidOperationException ("SendChunked must be True");
\r
369 Headers.RemoveAndAdd ("Transfer-Encoding", value);
\r
374 public bool UnsafeAuthenticatedConnectionSharing
\r
376 get { throw new NotImplementedException (); }
\r
377 set { throw new NotImplementedException (); }
\r
380 public string UserAgent
\r
382 get { return Headers ["User-Agent"]; }
\r
383 set { Headers.SetInternal ("User-Agent", value); }
\r
394 public override void Abort()
\r
397 // _connection.disconnect();
\r
398 // _haveResponse = true;
\r
399 // //aborted = true;
\r
400 // if (_asyncWrite != null)
\r
402 // GHWebAsyncResult r = _asyncWrite;
\r
403 // WebException wexc = new WebException ("Aborted.", WebExceptionStatus.RequestCanceled);
\r
404 // r.SetCompleted (false, wexc);
\r
405 // r.DoCallback ();
\r
406 // _asyncWrite = null;
\r
409 // if (_asyncRead != null)
\r
411 // GHWebAsyncResult r = _asyncRead;
\r
412 // WebException wexc = new WebException ("Aborted.", WebExceptionStatus.RequestCanceled);
\r
413 // r.SetCompleted (false, wexc);
\r
414 // r.DoCallback ();
\r
415 // _asyncRead = null;
\r
418 //// if (abortHandler != null)
\r
422 //// abortHandler (this, EventArgs.Empty);
\r
425 //// abortHandler = null;
\r
428 // if (_writeStream != null)
\r
432 // _writeStream.Close ();
\r
433 // _writeStream = null;
\r
438 // if (_response != null)
\r
442 // _response.Close ();
\r
443 // _response = null;
\r
449 public void AddRange (int range)
\r
451 AddRange ("bytes", range);
\r
454 public void AddRange (int from, int to)
\r
456 AddRange ("bytes", from, to);
\r
459 public void AddRange (string rangeSpecifier, int range)
\r
461 if (rangeSpecifier == null)
\r
462 throw new ArgumentNullException ("rangeSpecifier");
\r
463 string value = Headers ["Range"];
\r
464 if (value == null || value.Length == 0)
\r
465 value = rangeSpecifier + "=";
\r
466 else if (value.StartsWith (rangeSpecifier.ToLower () + "=", StringComparison.InvariantCultureIgnoreCase))
\r
469 throw new InvalidOperationException ("rangeSpecifier");
\r
470 Headers.RemoveAndAdd ("Range", value + range + "-");
\r
473 public void AddRange (string rangeSpecifier, int from, int to)
\r
475 if (rangeSpecifier == null)
\r
476 throw new ArgumentNullException ("rangeSpecifier");
\r
477 if (from < 0 || to < 0 || from > to)
\r
478 throw new ArgumentOutOfRangeException ();
\r
479 string value = Headers ["Range"];
\r
480 if (value == null || value.Length == 0)
\r
481 value = rangeSpecifier + "=";
\r
482 else if (value.StartsWith (rangeSpecifier.ToLower () + "=", StringComparison.InvariantCultureIgnoreCase))
\r
485 throw new InvalidOperationException ("rangeSpecifier");
\r
486 Headers.RemoveAndAdd ("Range", value + from + "-" + to);
\r
489 public override Stream GetRequestStream()
\r
491 return _provider.GetRequestStream();
\r
494 // Type t = Type.GetType("System.IO.ConsoleWriteStream", true);
\r
495 // _connection.setDoOutput(true);
\r
498 //// Console.WriteLine("Request is sent with following headers:");
\r
499 //// java.util.Map map = _connection.getRequestProperties();
\r
500 //// for(java.util.Iterator iter = map.keySet().iterator(); iter.hasNext();)
\r
502 //// string key = (string) iter.next();
\r
503 //// Console.WriteLine(key + ": " + map.get(key));
\r
506 // foreach(string k in Headers)
\r
508 // string val = Headers[k];
\r
509 // val = (val == null) ? "" : val;
\r
510 // _connection.setRequestProperty(k, val);
\r
513 // _writeStream = (Stream) Activator.CreateInstance(t, new object[]{_connection.getOutputStream()});
\r
514 // _haveRequest = true;
\r
515 // return _writeStream;
\r
519 public override WebResponse GetResponse()
\r
521 return _provider.GetResponse();
\r
524 private void CommonChecks (bool putpost)
\r
526 string method = _connection.getRequestMethod();
\r
528 if (method == null)
\r
529 throw new ProtocolViolationException ("Method is null.");
\r
531 bool keepAlive = _headers["Keep-Alive"] == null;
\r
532 bool allowBuffering = true;
\r
533 bool sendChunked = true;
\r
534 long contentLength = _connection.getContentLength();
\r
536 if (putpost && ((!keepAlive || (contentLength == -1 && !sendChunked)) && !allowBuffering))
\r
537 throw new ProtocolViolationException ("Content-Length not set");
\r
539 string transferEncoding = TransferEncoding;
\r
540 if (!sendChunked && transferEncoding != null && transferEncoding.Trim () != "")
\r
541 throw new ProtocolViolationException ("SendChunked should be true.");
\r
545 public override IAsyncResult BeginGetRequestStream(AsyncCallback callback, object state)
\r
547 return _provider.BeginGetRequestStream(callback, state);
\r
550 public override Stream EndGetRequestStream(IAsyncResult asyncResult)
\r
552 return _provider.EndGetRequestStream(asyncResult);
\r
555 public override IAsyncResult BeginGetResponse(AsyncCallback callback, object state)
\r
557 //todo check input, http headers etc.
\r
559 return _provider.BeginGetResponse(callback, state);
\r
562 public override WebResponse EndGetResponse(IAsyncResult asyncResult)
\r
564 return _provider.EndGetResponse(asyncResult);
\r
572 #region Inner Classes
\r
574 // #region JavaHeaders class
\r
576 // internal sealed class JavaHeaders : WebHeaderCollection
\r
578 // private java.net.HttpURLConnection _connection;
\r
580 // internal JavaHeaders(java.net.HttpURLConnection con)
\r
582 // _connection = con;
\r
585 // public string this[string key]
\r
589 // return _connection.getHeaderField(key);
\r
593 // _connection.addRequestProperty(key, value);
\r
604 public DecompressionMethods AutomaticDecompression
\r
607 throw new NotSupportedException ();
\r
610 throw new NotSupportedException ();
\r