WebRequest.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. //
  2. // System.Net.WebRequest
  3. //
  4. // Authors:
  5. // Lawrence Pit ([email protected])
  6. //
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining
  9. // a copy of this software and associated documentation files (the
  10. // "Software"), to deal in the Software without restriction, including
  11. // without limitation the rights to use, copy, modify, merge, publish,
  12. // distribute, sublicense, and/or sell copies of the Software, and to
  13. // permit persons to whom the Software is furnished to do so, subject to
  14. // the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be
  17. // included in all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  23. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  24. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  25. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. //
  27. using System;
  28. using System.Collections;
  29. using System.Collections.Specialized;
  30. using System.Configuration;
  31. using System.IO;
  32. using System.Runtime.Serialization;
  33. namespace System.Net
  34. {
  35. [Serializable]
  36. public abstract class WebRequest : MarshalByRefObject, ISerializable
  37. {
  38. static HybridDictionary prefixes = new HybridDictionary ();
  39. // Constructors
  40. static WebRequest ()
  41. {
  42. ConfigurationSettings.GetConfig ("system.net/webRequestModules");
  43. }
  44. protected WebRequest () { }
  45. protected WebRequest (SerializationInfo serializationInfo, StreamingContext streamingContext)
  46. {
  47. throw new NotSupportedException ();
  48. }
  49. // Properties
  50. public virtual string ConnectionGroupName {
  51. get { throw new NotSupportedException (); }
  52. set { throw new NotSupportedException (); }
  53. }
  54. public virtual long ContentLength {
  55. get { throw new NotSupportedException (); }
  56. set { throw new NotSupportedException (); }
  57. }
  58. public virtual string ContentType {
  59. get { throw new NotSupportedException (); }
  60. set { throw new NotSupportedException (); }
  61. }
  62. public virtual ICredentials Credentials {
  63. get { throw new NotSupportedException (); }
  64. set { throw new NotSupportedException (); }
  65. }
  66. public virtual WebHeaderCollection Headers {
  67. get { throw new NotSupportedException (); }
  68. set { throw new NotSupportedException (); }
  69. }
  70. public virtual string Method {
  71. get { throw new NotSupportedException (); }
  72. set { throw new NotSupportedException (); }
  73. }
  74. public virtual bool PreAuthenticate {
  75. get { throw new NotSupportedException (); }
  76. set { throw new NotSupportedException (); }
  77. }
  78. public virtual IWebProxy Proxy {
  79. get { throw new NotSupportedException (); }
  80. set { throw new NotSupportedException (); }
  81. }
  82. public virtual Uri RequestUri {
  83. get { throw new NotSupportedException (); }
  84. }
  85. public virtual int Timeout {
  86. get { throw new NotSupportedException (); }
  87. set { throw new NotSupportedException (); }
  88. }
  89. // Methods
  90. public virtual void Abort()
  91. {
  92. throw new NotSupportedException ();
  93. }
  94. public virtual IAsyncResult BeginGetRequestStream (AsyncCallback callback, object state)
  95. {
  96. throw new NotSupportedException ();
  97. }
  98. public virtual IAsyncResult BeginGetResponse (AsyncCallback callback, object state)
  99. {
  100. throw new NotSupportedException ();
  101. }
  102. public static WebRequest Create (string requestUriString)
  103. {
  104. if (requestUriString == null)
  105. throw new ArgumentNullException ("requestUriString");
  106. return Create (new Uri (requestUriString));
  107. }
  108. public static WebRequest Create (Uri requestUri)
  109. {
  110. if (requestUri == null)
  111. throw new ArgumentNullException ("requestUri");
  112. return GetCreator (requestUri.AbsoluteUri).Create (requestUri);
  113. }
  114. public static WebRequest CreateDefault (Uri requestUri)
  115. {
  116. if (requestUri == null)
  117. throw new ArgumentNullException ("requestUri");
  118. return GetCreator (requestUri.Scheme).Create (requestUri);
  119. }
  120. public virtual Stream EndGetRequestStream (IAsyncResult asyncResult)
  121. {
  122. throw new NotSupportedException ();
  123. }
  124. public virtual WebResponse EndGetResponse (IAsyncResult asyncResult)
  125. {
  126. throw new NotSupportedException ();
  127. }
  128. public virtual Stream GetRequestStream()
  129. {
  130. throw new NotSupportedException ();
  131. }
  132. public virtual WebResponse GetResponse()
  133. {
  134. throw new NotSupportedException ();
  135. }
  136. void ISerializable.GetObjectData (SerializationInfo serializationInfo,
  137. StreamingContext streamingContext)
  138. {
  139. throw new NotSupportedException ();
  140. }
  141. public static bool RegisterPrefix (string prefix, IWebRequestCreate creator)
  142. {
  143. if (prefix == null)
  144. throw new ArgumentNullException("prefix");
  145. if (creator == null)
  146. throw new ArgumentNullException("creator");
  147. lock (prefixes.SyncRoot) {
  148. string lowerCasePrefix = prefix.ToLower ();
  149. if (prefixes.Contains (lowerCasePrefix))
  150. return false;
  151. prefixes.Add (lowerCasePrefix, creator);
  152. }
  153. return true;
  154. }
  155. private static IWebRequestCreate GetCreator (string prefix)
  156. {
  157. int longestPrefix = -1;
  158. IWebRequestCreate creator = null;
  159. prefix = prefix.ToLower ();
  160. IDictionaryEnumerator e = prefixes.GetEnumerator ();
  161. while (e.MoveNext ()) {
  162. string key = e.Key as string;
  163. if (key.Length <= longestPrefix)
  164. continue;
  165. if (!prefix.StartsWith (key))
  166. continue;
  167. longestPrefix = key.Length;
  168. creator = (IWebRequestCreate) e.Value;
  169. }
  170. if (creator == null)
  171. throw new NotSupportedException (prefix);
  172. return creator;
  173. }
  174. internal static void ClearPrefixes ()
  175. {
  176. prefixes.Clear ();
  177. }
  178. internal static void RemovePrefix (string prefix)
  179. {
  180. prefixes.Remove (prefix);
  181. }
  182. internal static void AddPrefix (string prefix, string typeName)
  183. {
  184. Type type = Type.GetType (typeName);
  185. if (type == null)
  186. throw new ConfigurationException (String.Format ("Type {0} not found", typeName));
  187. object o = Activator.CreateInstance (type, true);
  188. prefixes [prefix] = o;
  189. }
  190. }
  191. }