WebClientProtocol.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. static HybridDictionary cache;
  54. #endregion
  55. #region Constructors
  56. static WebClientProtocol ()
  57. {
  58. cache = new HybridDictionary ();
  59. }
  60. protected WebClientProtocol ()
  61. {
  62. connectionGroupName = String.Empty;
  63. credentials = null;
  64. preAuthenticate = false;
  65. requestEncoding = null;
  66. timeout = 100000;
  67. url = String.Empty;
  68. abort = false;
  69. }
  70. #endregion // Constructors
  71. #region Properties
  72. [DefaultValue ("")]
  73. public string ConnectionGroupName {
  74. get { return connectionGroupName; }
  75. set { connectionGroupName = value; }
  76. }
  77. public ICredentials Credentials {
  78. get { return credentials; }
  79. set { credentials = value; }
  80. }
  81. [DefaultValue (false)]
  82. [WebServicesDescription ("Enables pre authentication of the request.")]
  83. public bool PreAuthenticate {
  84. get { return preAuthenticate; }
  85. set { preAuthenticate = value; }
  86. }
  87. [DefaultValue ("")]
  88. [RecommendedAsConfigurable (true)]
  89. [WebServicesDescription ("The encoding to use for requests.")]
  90. public Encoding RequestEncoding {
  91. get { return requestEncoding; }
  92. set { requestEncoding = value; }
  93. }
  94. [DefaultValue (100000)]
  95. [RecommendedAsConfigurable (true)]
  96. [WebServicesDescription ("Sets the timeout in milliseconds to be used for synchronous calls. The default of -1 means infinite.")]
  97. public int Timeout {
  98. get { return timeout; }
  99. set { timeout = value; }
  100. }
  101. [DefaultValue ("")]
  102. [RecommendedAsConfigurable (true)]
  103. [WebServicesDescription ("The base URL to the server to use for requests.")]
  104. public string Url {
  105. get { return url; }
  106. set {
  107. url = value;
  108. uri = new Uri (url);
  109. }
  110. }
  111. #endregion // Properties
  112. #region Methods
  113. public virtual void Abort ()
  114. {
  115. if (current_request != null){
  116. current_request.Abort ();
  117. current_request = null;
  118. }
  119. abort = true;
  120. }
  121. protected static void AddToCache (Type type, object value)
  122. {
  123. cache [type] = value;
  124. }
  125. protected static object GetFromCache (Type type)
  126. {
  127. return cache [type];
  128. }
  129. protected virtual WebRequest GetWebRequest (Uri uri)
  130. {
  131. if (uri == null)
  132. throw new InvalidOperationException ("uri is null");
  133. current_request = WebRequest.Create (uri);
  134. current_request.Timeout = timeout;
  135. current_request.PreAuthenticate = preAuthenticate;
  136. current_request.ConnectionGroupName = connectionGroupName;
  137. if (credentials != null)
  138. current_request.Credentials = credentials;
  139. return current_request;
  140. }
  141. protected virtual WebResponse GetWebResponse (WebRequest request)
  142. {
  143. if (abort)
  144. throw new WebException ("The operation has been aborted.", WebExceptionStatus.RequestCanceled);
  145. WebResponse response = null;
  146. try {
  147. request.Timeout = timeout;
  148. response = request.GetResponse ();
  149. } catch (WebException e) {
  150. response = e.Response;
  151. if (response == null)
  152. throw;
  153. }
  154. return response;
  155. }
  156. protected virtual WebResponse GetWebResponse (WebRequest request, IAsyncResult result)
  157. {
  158. if (abort)
  159. throw new WebException ("The operation has been aborted.", WebExceptionStatus.RequestCanceled);
  160. return request.EndGetResponse (result);
  161. }
  162. #endregion // Methods
  163. }
  164. }