WebClientProtocol.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. //
  2. // System.Web.Services.Protocols.WebClientProtocol.cs
  3. //
  4. // Author:
  5. // Tim Coleman ([email protected])
  6. //
  7. // Copyright (C) Tim Coleman, 2002
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System.Collections.Specialized;
  30. using System.ComponentModel;
  31. using System.Net;
  32. using System.Text;
  33. using System.Threading;
  34. using System.Web.Services;
  35. namespace System.Web.Services.Protocols {
  36. public abstract class WebClientProtocol : Component {
  37. #region Fields
  38. string connectionGroupName;
  39. ICredentials credentials;
  40. bool preAuthenticate;
  41. Encoding requestEncoding;
  42. int timeout;
  43. string url;
  44. bool abort;
  45. //
  46. // Used by SoapHttpClientProtocol, use this to avoid creating a new Uri on each invocation.
  47. //
  48. internal Uri uri;
  49. //
  50. // Points to the current request, so we can call Abort() on it
  51. //
  52. WebRequest current_request;
  53. #if !TARGET_JVM
  54. static HybridDictionary cache;
  55. #else
  56. static HybridDictionary cache {
  57. get {
  58. return (HybridDictionary)AppDomain.CurrentDomain.GetData("WebClientProtocol.cache");
  59. }
  60. set {
  61. AppDomain.CurrentDomain.SetData("WebClientProtocol.cache", value);
  62. }
  63. }
  64. #endif
  65. #endregion
  66. #region Constructors
  67. static WebClientProtocol ()
  68. {
  69. cache = new HybridDictionary ();
  70. }
  71. protected WebClientProtocol ()
  72. {
  73. connectionGroupName = String.Empty;
  74. credentials = null;
  75. preAuthenticate = false;
  76. requestEncoding = null;
  77. timeout = 100000;
  78. url = String.Empty;
  79. abort = false;
  80. }
  81. #endregion // Constructors
  82. #region Properties
  83. [DefaultValue ("")]
  84. public string ConnectionGroupName {
  85. get { return connectionGroupName; }
  86. set { connectionGroupName = value; }
  87. }
  88. public ICredentials Credentials {
  89. get { return credentials; }
  90. set { credentials = value; }
  91. }
  92. [DefaultValue (false)]
  93. [WebServicesDescription ("Enables pre authentication of the request.")]
  94. public bool PreAuthenticate {
  95. get { return preAuthenticate; }
  96. set { preAuthenticate = value; }
  97. }
  98. [DefaultValue ("")]
  99. [RecommendedAsConfigurable (true)]
  100. [WebServicesDescription ("The encoding to use for requests.")]
  101. public Encoding RequestEncoding {
  102. get { return requestEncoding; }
  103. set { requestEncoding = value; }
  104. }
  105. [DefaultValue (100000)]
  106. [RecommendedAsConfigurable (true)]
  107. [WebServicesDescription ("Sets the timeout in milliseconds to be used for synchronous calls. The default of -1 means infinite.")]
  108. public int Timeout {
  109. get { return timeout; }
  110. set { timeout = value; }
  111. }
  112. [DefaultValue ("")]
  113. [RecommendedAsConfigurable (true)]
  114. [WebServicesDescription ("The base URL to the server to use for requests.")]
  115. public string Url {
  116. get { return url; }
  117. set {
  118. url = value;
  119. uri = new Uri (url);
  120. }
  121. }
  122. #endregion // Properties
  123. #region Methods
  124. public virtual void Abort ()
  125. {
  126. if (current_request != null){
  127. current_request.Abort ();
  128. current_request = null;
  129. }
  130. abort = true;
  131. }
  132. protected static void AddToCache (Type type, object value)
  133. {
  134. cache [type] = value;
  135. }
  136. protected static object GetFromCache (Type type)
  137. {
  138. return cache [type];
  139. }
  140. protected virtual WebRequest GetWebRequest (Uri uri)
  141. {
  142. if (uri == null)
  143. throw new InvalidOperationException ("uri is null");
  144. current_request = WebRequest.Create (uri);
  145. current_request.Timeout = timeout;
  146. current_request.PreAuthenticate = preAuthenticate;
  147. current_request.ConnectionGroupName = connectionGroupName;
  148. if (credentials != null)
  149. current_request.Credentials = credentials;
  150. return current_request;
  151. }
  152. protected virtual WebResponse GetWebResponse (WebRequest request)
  153. {
  154. if (abort)
  155. throw new WebException ("The operation has been aborted.", WebExceptionStatus.RequestCanceled);
  156. WebResponse response = null;
  157. try {
  158. request.Timeout = timeout;
  159. response = request.GetResponse ();
  160. } catch (WebException e) {
  161. response = e.Response;
  162. if (response == null)
  163. throw;
  164. }
  165. return response;
  166. }
  167. protected virtual WebResponse GetWebResponse (WebRequest request, IAsyncResult result)
  168. {
  169. if (abort)
  170. throw new WebException ("The operation has been aborted.", WebExceptionStatus.RequestCanceled);
  171. return request.EndGetResponse (result);
  172. }
  173. #endregion // Methods
  174. }
  175. }