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. }
  65. public virtual int Timeout {
  66. get { throw new NotSupportedException (); }
  67. set { throw new NotSupportedException (); }
  68. }
  69. // Methods
  70. public virtual void Abort()
  71. {
  72. throw new NotSupportedException ();
  73. }
  74. public virtual IAsyncResult BeginGetRequestStream (AsyncCallback callback, object state)
  75. {
  76. throw new NotSupportedException ();
  77. }
  78. public virtual IAsyncResult BeginGetResponse (AsyncCallback callback, object state)
  79. {
  80. throw new NotSupportedException ();
  81. }
  82. public static WebRequest Create (string requestUriString)
  83. {
  84. if (requestUriString == null)
  85. throw new ArgumentNullException ("requestUriString");
  86. return Create (new Uri (requestUriString));
  87. }
  88. public static WebRequest Create (Uri requestUri)
  89. {
  90. if (requestUri == null)
  91. throw new ArgumentNullException ("requestUri");
  92. return GetCreator (requestUri.AbsoluteUri).Create (requestUri);
  93. }
  94. public static WebRequest CreateDefault (Uri requestUri)
  95. {
  96. if (requestUri == null)
  97. throw new ArgumentNullException ("requestUri");
  98. return GetCreator (requestUri.Scheme).Create (requestUri);
  99. }
  100. public virtual Stream EndGetRequestStream (IAsyncResult asyncResult)
  101. {
  102. throw new NotSupportedException ();
  103. }
  104. public virtual WebResponse EndGetResponse (IAsyncResult asyncResult)
  105. {
  106. throw new NotSupportedException ();
  107. }
  108. public virtual Stream GetRequestStream()
  109. {
  110. throw new NotSupportedException ();
  111. }
  112. public virtual WebResponse GetResponse()
  113. {
  114. throw new NotSupportedException ();
  115. }
  116. void ISerializable.GetObjectData (SerializationInfo serializationInfo,
  117. StreamingContext streamingContext)
  118. {
  119. throw new NotSupportedException ();
  120. }
  121. public static bool RegisterPrefix (string prefix, IWebRequestCreate creator)
  122. {
  123. if (prefix == null)
  124. throw new ArgumentNullException("prefix");
  125. if (creator == null)
  126. throw new ArgumentNullException("creator");
  127. lock (prefixes.SyncRoot) {
  128. string lowerCasePrefix = prefix.ToLower ();
  129. if (prefixes.Contains (lowerCasePrefix))
  130. return false;
  131. prefixes.Add (lowerCasePrefix, 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, true);
  168. prefixes [prefix] = o;
  169. }
  170. }
  171. }