2 // System.Net.WebRequest
5 // Lawrence Pit (loz@cable.a2000.nl)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Collections;
31 using System.Collections.Specialized;
32 using System.Configuration;
34 using System.Runtime.Serialization;
35 using System.Globalization;
37 using System.Net.Configuration;
38 using System.Net.Security;
39 using System.Net.Cache;
40 using System.Security.Principal;
46 public abstract class WebRequest : MarshalByRefObject, ISerializable
48 static HybridDictionary prefixes = new HybridDictionary ();
50 static bool isDefaultWebProxySet;
51 static IWebProxy defaultWebProxy;
58 #if NET_2_0 && CONFIGURATION_DEP
59 object cfg = ConfigurationManager.GetSection ("system.net/webRequestModules");
60 WebRequestModulesSection s = cfg as WebRequestModulesSection;
62 foreach (WebRequestModuleElement el in
64 AddPrefix (el.Prefix, el.Type);
68 ConfigurationSettings.GetConfig ("system.net/webRequestModules");
71 protected WebRequest ()
75 protected WebRequest (SerializationInfo serializationInfo, StreamingContext streamingContext)
78 throw GetMustImplement ();
82 static Exception GetMustImplement ()
84 return new NotImplementedException ("This method must be implemented in derived classes");
90 private AuthenticationLevel authentication_level = AuthenticationLevel.MutualAuthRequested;
92 public AuthenticationLevel AuthenticationLevel
95 return(authentication_level);
98 authentication_level = value;
102 public virtual RequestCachePolicy CachePolicy
105 throw GetMustImplement ();
112 public virtual string ConnectionGroupName {
113 get { throw GetMustImplement (); }
114 set { throw GetMustImplement (); }
117 public virtual long ContentLength {
118 get { throw GetMustImplement (); }
119 set { throw GetMustImplement (); }
122 public virtual string ContentType {
123 get { throw GetMustImplement (); }
124 set { throw GetMustImplement (); }
127 public virtual ICredentials Credentials {
128 get { throw GetMustImplement (); }
129 set { throw GetMustImplement (); }
133 public static RequestCachePolicy DefaultCachePolicy
136 throw GetMustImplement ();
139 throw GetMustImplement ();
144 public virtual WebHeaderCollection Headers {
145 get { throw GetMustImplement (); }
146 set { throw GetMustImplement (); }
150 public TokenImpersonationLevel ImpersonationLevel {
151 get { throw GetMustImplement (); }
152 set { throw GetMustImplement (); }
155 public virtual string Method {
156 get { throw GetMustImplement (); }
157 set { throw GetMustImplement (); }
160 public virtual bool PreAuthenticate {
161 get { throw GetMustImplement (); }
162 set { throw GetMustImplement (); }
165 public virtual IWebProxy Proxy {
166 get { throw GetMustImplement (); }
167 set { throw GetMustImplement (); }
170 public virtual Uri RequestUri {
171 get { throw GetMustImplement (); }
174 public virtual int Timeout {
175 get { throw GetMustImplement (); }
176 set { throw GetMustImplement (); }
180 public virtual bool UseDefaultCredentials
183 throw GetMustImplement ();
186 throw GetMustImplement ();
190 // volatile static IWebProxy proxy;
191 static readonly object lockobj = new object ();
193 public static IWebProxy DefaultWebProxy {
195 if (!isDefaultWebProxySet) {
197 if (defaultWebProxy == null)
198 defaultWebProxy = GetDefaultWebProxy ();
201 return defaultWebProxy;
204 /* MS documentation states that a null value would cause an ArgumentNullException
205 * but that's not the way it behaves:
206 * https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=304724
208 defaultWebProxy = value;
209 isDefaultWebProxySet = true;
213 [MonoTODO("Needs to respect Module, Proxy.AutoDetect, and Proxy.ScriptLocation config settings")]
214 static IWebProxy GetDefaultWebProxy ()
218 #if CONFIGURATION_DEP
219 DefaultProxySection sec = ConfigurationManager.GetSection ("system.net/defaultProxy") as DefaultProxySection;
221 return GetSystemWebProxy ();
223 ProxyElement pe = sec.Proxy;
225 if ((pe.UseSystemDefault != ProxyElement.UseSystemDefaultValues.False) && (pe.ProxyAddress == null))
226 p = (WebProxy) GetSystemWebProxy ();
230 if (pe.ProxyAddress != null)
231 p.Address = pe.ProxyAddress;
233 if (pe.BypassOnLocal != ProxyElement.BypassOnLocalValues.Unspecified)
234 p.BypassProxyOnLocal = (pe.BypassOnLocal == ProxyElement.BypassOnLocalValues.True);
242 public virtual void Abort()
244 throw GetMustImplement ();
247 public virtual IAsyncResult BeginGetRequestStream (AsyncCallback callback, object state)
249 throw GetMustImplement ();
252 public virtual IAsyncResult BeginGetResponse (AsyncCallback callback, object state)
254 throw GetMustImplement ();
257 public static WebRequest Create (string requestUriString)
259 if (requestUriString == null)
260 throw new ArgumentNullException ("requestUriString");
261 return Create (new Uri (requestUriString));
264 public static WebRequest Create (Uri requestUri)
266 if (requestUri == null)
267 throw new ArgumentNullException ("requestUri");
268 return GetCreator (requestUri.AbsoluteUri).Create (requestUri);
271 public static WebRequest CreateDefault (Uri requestUri)
273 if (requestUri == null)
274 throw new ArgumentNullException ("requestUri");
275 return GetCreator (requestUri.Scheme).Create (requestUri);
278 public virtual Stream EndGetRequestStream (IAsyncResult asyncResult)
280 throw GetMustImplement ();
283 public virtual WebResponse EndGetResponse (IAsyncResult asyncResult)
285 throw GetMustImplement ();
288 public virtual Stream GetRequestStream()
290 throw GetMustImplement ();
293 public virtual WebResponse GetResponse()
295 throw GetMustImplement ();
299 [MonoTODO("Look in other places for proxy config info")]
300 public static IWebProxy GetSystemWebProxy ()
302 string address = Environment.GetEnvironmentVariable ("http_proxy");
303 if (address != null) {
305 WebProxy p = new WebProxy (address);
307 } catch (UriFormatException) {}
309 return new WebProxy ();
313 void ISerializable.GetObjectData
314 (SerializationInfo serializationInfo,
315 StreamingContext streamingContext)
317 throw new NotSupportedException ();
322 protected virtual void GetObjectData (SerializationInfo serializationInfo, StreamingContext streamingContext)
324 throw GetMustImplement ();
328 public static bool RegisterPrefix (string prefix, IWebRequestCreate creator)
331 throw new ArgumentNullException ("prefix");
333 throw new ArgumentNullException ("creator");
335 lock (prefixes.SyncRoot) {
336 string lowerCasePrefix = prefix.ToLower (CultureInfo.InvariantCulture);
337 if (prefixes.Contains (lowerCasePrefix))
339 prefixes.Add (lowerCasePrefix, creator);
344 private static IWebRequestCreate GetCreator (string prefix)
346 int longestPrefix = -1;
347 IWebRequestCreate creator = null;
349 prefix = prefix.ToLower (CultureInfo.InvariantCulture);
351 IDictionaryEnumerator e = prefixes.GetEnumerator ();
352 while (e.MoveNext ()) {
353 string key = e.Key as string;
355 if (key.Length <= longestPrefix)
358 if (!prefix.StartsWith (key))
361 longestPrefix = key.Length;
362 creator = (IWebRequestCreate) e.Value;
366 throw new NotSupportedException (prefix);
371 internal static void ClearPrefixes ()
376 internal static void RemovePrefix (string prefix)
378 prefixes.Remove (prefix);
381 internal static void AddPrefix (string prefix, string typeName)
383 Type type = Type.GetType (typeName);
385 throw new ConfigurationException (String.Format ("Type {0} not found", typeName));
386 AddPrefix (prefix, type);
389 internal static void AddPrefix (string prefix, Type type)
391 object o = Activator.CreateInstance (type, true);
392 prefixes [prefix] = o;