WebRequest.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. {
  46. }
  47. protected WebRequest (SerializationInfo serializationInfo, StreamingContext streamingContext)
  48. {
  49. }
  50. // Properties
  51. public virtual string ConnectionGroupName {
  52. get { throw new NotImplementedException (); }
  53. set { throw new NotImplementedException (); }
  54. }
  55. public virtual long ContentLength {
  56. get { throw new NotImplementedException (); }
  57. set { throw new NotImplementedException (); }
  58. }
  59. public virtual string ContentType {
  60. get { throw new NotImplementedException (); }
  61. set { throw new NotImplementedException (); }
  62. }
  63. public virtual ICredentials Credentials {
  64. get { throw new NotImplementedException (); }
  65. set { throw new NotImplementedException (); }
  66. }
  67. public virtual WebHeaderCollection Headers {
  68. get { throw new NotImplementedException (); }
  69. set { throw new NotImplementedException (); }
  70. }
  71. public virtual string Method {
  72. get { throw new NotImplementedException (); }
  73. set { throw new NotImplementedException (); }
  74. }
  75. public virtual bool PreAuthenticate {
  76. get { throw new NotImplementedException (); }
  77. set { throw new NotImplementedException (); }
  78. }
  79. public virtual IWebProxy Proxy {
  80. get { throw new NotImplementedException (); }
  81. set { throw new NotImplementedException (); }
  82. }
  83. public virtual Uri RequestUri {
  84. get { throw new NotImplementedException (); }
  85. }
  86. public virtual int Timeout {
  87. get { throw new NotImplementedException (); }
  88. set { throw new NotImplementedException (); }
  89. }
  90. // Methods
  91. public virtual void Abort()
  92. {
  93. throw new NotImplementedException ();
  94. }
  95. public virtual IAsyncResult BeginGetRequestStream (AsyncCallback callback, object state)
  96. {
  97. throw new NotImplementedException ();
  98. }
  99. public virtual IAsyncResult BeginGetResponse (AsyncCallback callback, object state)
  100. {
  101. throw new NotImplementedException ();
  102. }
  103. public static WebRequest Create (string requestUriString)
  104. {
  105. if (requestUriString == null)
  106. throw new ArgumentNullException ("requestUriString");
  107. return Create (new Uri (requestUriString));
  108. }
  109. public static WebRequest Create (Uri requestUri)
  110. {
  111. if (requestUri == null)
  112. throw new ArgumentNullException ("requestUri");
  113. return GetCreator (requestUri.AbsoluteUri).Create (requestUri);
  114. }
  115. public static WebRequest CreateDefault (Uri requestUri)
  116. {
  117. if (requestUri == null)
  118. throw new ArgumentNullException ("requestUri");
  119. return GetCreator (requestUri.Scheme).Create (requestUri);
  120. }
  121. public virtual Stream EndGetRequestStream (IAsyncResult asyncResult)
  122. {
  123. throw new NotImplementedException ();
  124. }
  125. public virtual WebResponse EndGetResponse (IAsyncResult asyncResult)
  126. {
  127. throw new NotImplementedException ();
  128. }
  129. public virtual Stream GetRequestStream()
  130. {
  131. throw new NotImplementedException ();
  132. }
  133. public virtual WebResponse GetResponse()
  134. {
  135. throw new NotImplementedException ();
  136. }
  137. void ISerializable.GetObjectData (SerializationInfo serializationInfo,
  138. StreamingContext streamingContext)
  139. {
  140. throw new NotSupportedException ();
  141. }
  142. public static bool RegisterPrefix (string prefix, IWebRequestCreate creator)
  143. {
  144. if (prefix == null)
  145. throw new ArgumentNullException("prefix");
  146. if (creator == null)
  147. throw new ArgumentNullException("creator");
  148. lock (prefixes.SyncRoot) {
  149. string lowerCasePrefix = prefix.ToLower ();
  150. if (prefixes.Contains (lowerCasePrefix))
  151. return false;
  152. prefixes.Add (lowerCasePrefix, creator);
  153. }
  154. return true;
  155. }
  156. private static IWebRequestCreate GetCreator (string prefix)
  157. {
  158. int longestPrefix = -1;
  159. IWebRequestCreate creator = null;
  160. prefix = prefix.ToLower ();
  161. IDictionaryEnumerator e = prefixes.GetEnumerator ();
  162. while (e.MoveNext ()) {
  163. string key = e.Key as string;
  164. if (key.Length <= longestPrefix)
  165. continue;
  166. if (!prefix.StartsWith (key))
  167. continue;
  168. longestPrefix = key.Length;
  169. creator = (IWebRequestCreate) e.Value;
  170. }
  171. if (creator == null)
  172. throw new NotSupportedException (prefix);
  173. return creator;
  174. }
  175. internal static void ClearPrefixes ()
  176. {
  177. prefixes.Clear ();
  178. }
  179. internal static void RemovePrefix (string prefix)
  180. {
  181. prefixes.Remove (prefix);
  182. }
  183. internal static void AddPrefix (string prefix, string typeName)
  184. {
  185. Type type = Type.GetType (typeName);
  186. if (type == null)
  187. throw new ConfigurationException (String.Format ("Type {0} not found", typeName));
  188. object o = Activator.CreateInstance (type, true);
  189. prefixes [prefix] = o;
  190. }
  191. }
  192. }