2
0

WebClientProtocol.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. #if NET_2_0
  37. [System.Runtime.InteropServices.ComVisible (true)]
  38. #endif
  39. public abstract class WebClientProtocol : Component {
  40. #region Fields
  41. string connectionGroupName;
  42. ICredentials credentials;
  43. bool preAuthenticate;
  44. Encoding requestEncoding;
  45. int timeout;
  46. string url;
  47. bool abort;
  48. //
  49. // Used by SoapHttpClientProtocol, use this to avoid creating a new Uri on each invocation.
  50. //
  51. internal Uri uri;
  52. //
  53. // Points to the current request, so we can call Abort() on it
  54. //
  55. WebRequest current_request;
  56. #if !TARGET_JVM
  57. static HybridDictionary cache;
  58. #else
  59. static HybridDictionary cache {
  60. get {
  61. return (HybridDictionary)AppDomain.CurrentDomain.GetData("WebClientProtocol.cache");
  62. }
  63. set {
  64. AppDomain.CurrentDomain.SetData("WebClientProtocol.cache", value);
  65. }
  66. }
  67. #endif
  68. #endregion
  69. #region Constructors
  70. static WebClientProtocol ()
  71. {
  72. cache = new HybridDictionary ();
  73. }
  74. protected WebClientProtocol ()
  75. {
  76. connectionGroupName = String.Empty;
  77. credentials = null;
  78. preAuthenticate = false;
  79. requestEncoding = null;
  80. timeout = 100000;
  81. url = String.Empty;
  82. abort = false;
  83. }
  84. #endregion // Constructors
  85. #region Properties
  86. [DefaultValue ("")]
  87. public string ConnectionGroupName {
  88. get { return connectionGroupName; }
  89. set { connectionGroupName = value; }
  90. }
  91. public ICredentials Credentials {
  92. get { return credentials; }
  93. set { credentials = value; }
  94. }
  95. [DefaultValue (false)]
  96. [WebServicesDescription ("Enables pre authentication of the request.")]
  97. public bool PreAuthenticate {
  98. get { return preAuthenticate; }
  99. set { preAuthenticate = value; }
  100. }
  101. [DefaultValue (null)]
  102. [RecommendedAsConfigurable (true)]
  103. [WebServicesDescription ("The encoding to use for requests.")]
  104. public Encoding RequestEncoding {
  105. get { return requestEncoding; }
  106. set { requestEncoding = value; }
  107. }
  108. [DefaultValue (100000)]
  109. [RecommendedAsConfigurable (true)]
  110. [WebServicesDescription ("Sets the timeout in milliseconds to be used for synchronous calls. The default of -1 means infinite.")]
  111. public int Timeout {
  112. get { return timeout; }
  113. set { timeout = value; }
  114. }
  115. [DefaultValue ("")]
  116. [RecommendedAsConfigurable (true)]
  117. [WebServicesDescription ("The base URL to the server to use for requests.")]
  118. public string Url {
  119. get { return url; }
  120. set {
  121. url = value;
  122. uri = new Uri (url);
  123. }
  124. }
  125. #if NET_2_0
  126. public bool UseDefaultCredentials {
  127. get { return CredentialCache.DefaultCredentials == Credentials; }
  128. set { Credentials = value ? CredentialCache.DefaultCredentials : null; }
  129. }
  130. #endif
  131. #endregion // Properties
  132. #region Methods
  133. public virtual void Abort ()
  134. {
  135. if (current_request != null){
  136. current_request.Abort ();
  137. current_request = null;
  138. }
  139. abort = true;
  140. }
  141. protected static void AddToCache (Type type, object value)
  142. {
  143. cache [type] = value;
  144. }
  145. protected static object GetFromCache (Type type)
  146. {
  147. return cache [type];
  148. }
  149. protected virtual WebRequest GetWebRequest (Uri uri)
  150. {
  151. if (uri == null)
  152. throw new InvalidOperationException ("uri is null");
  153. current_request = WebRequest.Create (uri);
  154. current_request.Timeout = timeout;
  155. current_request.PreAuthenticate = preAuthenticate;
  156. current_request.ConnectionGroupName = connectionGroupName;
  157. if (credentials != null)
  158. current_request.Credentials = credentials;
  159. return current_request;
  160. }
  161. protected virtual WebResponse GetWebResponse (WebRequest request)
  162. {
  163. if (abort)
  164. throw new WebException ("The operation has been aborted.", WebExceptionStatus.RequestCanceled);
  165. WebResponse response = null;
  166. try {
  167. request.Timeout = timeout;
  168. response = request.GetResponse ();
  169. } catch (WebException e) {
  170. response = e.Response;
  171. if (response == null)
  172. throw;
  173. }
  174. return response;
  175. }
  176. protected virtual WebResponse GetWebResponse (WebRequest request, IAsyncResult result)
  177. {
  178. if (abort)
  179. throw new WebException ("The operation has been aborted.", WebExceptionStatus.RequestCanceled);
  180. return request.EndGetResponse (result);
  181. }
  182. #endregion // Methods
  183. }
  184. }