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
;
39 public abstract class WebRequest
: MarshalByRefObject
, ISerializable
41 static HybridDictionary prefixes
= new HybridDictionary ();
47 ConfigurationSettings
.GetConfig ("system.net/webRequestModules");
50 protected WebRequest () { }
52 protected WebRequest (SerializationInfo serializationInfo
, StreamingContext streamingContext
)
54 throw new NotSupportedException ();
59 public virtual string ConnectionGroupName
{
60 get { throw new NotSupportedException (); }
61 set { throw new NotSupportedException (); }
64 public virtual long ContentLength
{
65 get { throw new NotSupportedException (); }
66 set { throw new NotSupportedException (); }
69 public virtual string ContentType
{
70 get { throw new NotSupportedException (); }
71 set { throw new NotSupportedException (); }
74 public virtual ICredentials Credentials
{
75 get { throw new NotSupportedException (); }
76 set { throw new NotSupportedException (); }
79 public virtual WebHeaderCollection Headers
{
80 get { throw new NotSupportedException (); }
81 set { throw new NotSupportedException (); }
84 public virtual string Method
{
85 get { throw new NotSupportedException (); }
86 set { throw new NotSupportedException (); }
89 public virtual bool PreAuthenticate
{
90 get { throw new NotSupportedException (); }
91 set { throw new NotSupportedException (); }
94 public virtual IWebProxy Proxy
{
95 get { throw new NotSupportedException (); }
96 set { throw new NotSupportedException (); }
99 public virtual Uri RequestUri
{
100 get { throw new NotSupportedException (); }
103 public virtual int Timeout
{
104 get { throw new NotSupportedException (); }
105 set { throw new NotSupportedException (); }
110 public virtual void Abort()
112 throw new NotSupportedException ();
115 public virtual IAsyncResult
BeginGetRequestStream (AsyncCallback callback
, object state
)
117 throw new NotSupportedException ();
120 public virtual IAsyncResult
BeginGetResponse (AsyncCallback callback
, object state
)
122 throw new NotSupportedException ();
125 public static WebRequest
Create (string requestUriString
)
127 if (requestUriString
== null)
128 throw new ArgumentNullException ("requestUriString");
129 return Create (new Uri (requestUriString
));
132 public static WebRequest
Create (Uri requestUri
)
134 if (requestUri
== null)
135 throw new ArgumentNullException ("requestUri");
136 return GetCreator (requestUri
.AbsoluteUri
).Create (requestUri
);
139 public static WebRequest
CreateDefault (Uri requestUri
)
141 if (requestUri
== null)
142 throw new ArgumentNullException ("requestUri");
143 return GetCreator (requestUri
.Scheme
).Create (requestUri
);
146 public virtual Stream
EndGetRequestStream (IAsyncResult asyncResult
)
148 throw new NotSupportedException ();
151 public virtual WebResponse
EndGetResponse (IAsyncResult asyncResult
)
153 throw new NotSupportedException ();
156 public virtual Stream
GetRequestStream()
158 throw new NotSupportedException ();
161 public virtual WebResponse
GetResponse()
163 throw new NotSupportedException ();
166 void ISerializable
.GetObjectData (SerializationInfo serializationInfo
,
167 StreamingContext streamingContext
)
169 throw new NotSupportedException ();
172 public static bool RegisterPrefix (string prefix
, IWebRequestCreate creator
)
175 throw new ArgumentNullException("prefix");
177 throw new ArgumentNullException("creator");
179 lock (prefixes
.SyncRoot
) {
180 string lowerCasePrefix
= prefix
.ToLower ();
181 if (prefixes
.Contains (lowerCasePrefix
))
183 prefixes
.Add (lowerCasePrefix
, creator
);
188 private static IWebRequestCreate
GetCreator (string prefix
)
190 int longestPrefix
= -1;
191 IWebRequestCreate creator
= null;
193 prefix
= prefix
.ToLower ();
195 IDictionaryEnumerator e
= prefixes
.GetEnumerator ();
196 while (e
.MoveNext ()) {
197 string key
= e
.Key
as string;
199 if (key
.Length
<= longestPrefix
)
202 if (!prefix
.StartsWith (key
))
205 longestPrefix
= key
.Length
;
206 creator
= (IWebRequestCreate
) e
.Value
;
210 throw new NotSupportedException (prefix
);
215 internal static void ClearPrefixes ()
220 internal static void RemovePrefix (string prefix
)
222 prefixes
.Remove (prefix
);
225 internal static void AddPrefix (string prefix
, string typeName
)
227 Type type
= Type
.GetType (typeName
);
229 throw new ConfigurationException (String
.Format ("Type {0} not found", typeName
));
231 object o
= Activator
.CreateInstance (type
, true);
232 prefixes
[prefix
] = o
;