WebRequest.cs 5.3 KB

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