WebRequest.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. //
  2. // System.Net.WebRequest
  3. //
  4. // Authors:
  5. // Lawrence Pit ([email protected])
  6. //
  7. using System;
  8. using System.Collections;
  9. using System.Collections.Specialized;
  10. using System.Configuration;
  11. using System.IO;
  12. using System.Runtime.Serialization;
  13. namespace System.Net
  14. {
  15. [Serializable]
  16. public abstract class WebRequest : MarshalByRefObject, ISerializable
  17. {
  18. static HybridDictionary prefixes = new HybridDictionary ();
  19. // Constructors
  20. static WebRequest ()
  21. {
  22. ConfigurationSettings.GetConfig ("system.net/webRequestModules");
  23. }
  24. protected WebRequest () { }
  25. protected WebRequest (SerializationInfo serializationInfo, StreamingContext streamingContext)
  26. {
  27. throw new NotSupportedException ();
  28. }
  29. // Properties
  30. public virtual string ConnectionGroupName {
  31. get { throw new NotSupportedException (); }
  32. set { throw new NotSupportedException (); }
  33. }
  34. public virtual long ContentLength {
  35. get { throw new NotSupportedException (); }
  36. set { throw new NotSupportedException (); }
  37. }
  38. public virtual string ContentType {
  39. get { throw new NotSupportedException (); }
  40. set { throw new NotSupportedException (); }
  41. }
  42. public virtual ICredentials Credentials {
  43. get { throw new NotSupportedException (); }
  44. set { throw new NotSupportedException (); }
  45. }
  46. public virtual WebHeaderCollection Headers {
  47. get { throw new NotSupportedException (); }
  48. set { throw new NotSupportedException (); }
  49. }
  50. public virtual string Method {
  51. get { throw new NotSupportedException (); }
  52. set { throw new NotSupportedException (); }
  53. }
  54. public virtual bool PreAuthenticate {
  55. get { throw new NotSupportedException (); }
  56. set { throw new NotSupportedException (); }
  57. }
  58. public virtual IWebProxy Proxy {
  59. get { throw new NotSupportedException (); }
  60. set { throw new NotSupportedException (); }
  61. }
  62. public virtual Uri RequestUri {
  63. get { throw new NotSupportedException (); }
  64. set { throw new NotSupportedException (); }
  65. }
  66. public virtual int Timeout {
  67. get { throw new NotSupportedException (); }
  68. set { throw new NotSupportedException (); }
  69. }
  70. // Methods
  71. public virtual void Abort()
  72. {
  73. throw new NotSupportedException ();
  74. }
  75. public virtual IAsyncResult BeginGetRequestStream (AsyncCallback callback, object state)
  76. {
  77. throw new NotSupportedException ();
  78. }
  79. public virtual IAsyncResult BeginGetResponse (AsyncCallback callback, object state)
  80. {
  81. throw new NotSupportedException ();
  82. }
  83. public static WebRequest Create (string requestUriString)
  84. {
  85. if (requestUriString == null)
  86. throw new ArgumentNullException ("requestUriString");
  87. return Create (new Uri (requestUriString));
  88. }
  89. public static WebRequest Create (Uri requestUri)
  90. {
  91. if (requestUri == null)
  92. throw new ArgumentNullException ("requestUri");
  93. return GetCreator (requestUri.AbsoluteUri).Create (requestUri);
  94. }
  95. public static WebRequest CreateDefault (Uri requestUri)
  96. {
  97. if (requestUri == null)
  98. throw new ArgumentNullException ("requestUri");
  99. return GetCreator (requestUri.Scheme).Create (requestUri);
  100. }
  101. public virtual Stream EndGetRequestStream (IAsyncResult asyncResult)
  102. {
  103. throw new NotSupportedException ();
  104. }
  105. public virtual WebResponse EndGetResponse (IAsyncResult asyncResult)
  106. {
  107. throw new NotSupportedException ();
  108. }
  109. public virtual Stream GetRequestStream()
  110. {
  111. throw new NotSupportedException ();
  112. }
  113. public virtual WebResponse GetResponse()
  114. {
  115. throw new NotSupportedException ();
  116. }
  117. void ISerializable.GetObjectData (SerializationInfo serializationInfo,
  118. StreamingContext streamingContext)
  119. {
  120. throw new NotSupportedException ();
  121. }
  122. public static bool RegisterPrefix (string prefix, IWebRequestCreate creator)
  123. {
  124. if (prefix == null)
  125. throw new ArgumentNullException("prefix");
  126. if (creator == null)
  127. throw new ArgumentNullException("creator");
  128. lock (prefixes.SyncRoot) {
  129. if (prefixes.Contains (prefix))
  130. return false;
  131. prefixes.Add (prefix.ToLower (), creator);
  132. }
  133. return true;
  134. }
  135. private static IWebRequestCreate GetCreator (string prefix)
  136. {
  137. int longestPrefix = -1;
  138. IWebRequestCreate creator = null;
  139. prefix = prefix.ToLower ();
  140. IDictionaryEnumerator e = prefixes.GetEnumerator ();
  141. while (e.MoveNext ()) {
  142. string key = e.Key as string;
  143. if (key.Length <= longestPrefix)
  144. continue;
  145. if (!prefix.StartsWith (key))
  146. continue;
  147. longestPrefix = key.Length;
  148. creator = (IWebRequestCreate) e.Value;
  149. }
  150. if (creator == null)
  151. throw new NotSupportedException (prefix);
  152. return creator;
  153. }
  154. internal static void ClearPrefixes ()
  155. {
  156. prefixes.Clear ();
  157. }
  158. internal static void RemovePrefix (string prefix)
  159. {
  160. prefixes.Remove (prefix);
  161. }
  162. internal static void AddPrefix (string prefix, string typeName)
  163. {
  164. Type type = Type.GetType (typeName);
  165. if (type == null)
  166. throw new ConfigurationException (String.Format ("Type {0} not found", typeName));
  167. object o = Activator.CreateInstance (type);
  168. prefixes [prefix] = o;
  169. }
  170. }
  171. }