WebClientProtocol.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. using System.Collections.Specialized;
  10. using System.ComponentModel;
  11. using System.Net;
  12. using System.Text;
  13. using System.Threading;
  14. using System.Web.Services;
  15. namespace System.Web.Services.Protocols {
  16. public abstract class WebClientProtocol : Component {
  17. #region Fields
  18. string connectionGroupName;
  19. ICredentials credentials;
  20. bool preAuthenticate;
  21. Encoding requestEncoding;
  22. int timeout;
  23. string url;
  24. bool abort;
  25. //
  26. // Used by SoapHttpClientProtocol, use this to avoid creating a new Uri on each invocation.
  27. //
  28. protected internal Uri uri;
  29. //
  30. // Points to the current request, so we can call Abort() on it
  31. //
  32. WebRequest current_request;
  33. static HybridDictionary cache;
  34. #endregion
  35. #region Constructors
  36. static WebClientProtocol ()
  37. {
  38. cache = new HybridDictionary ();
  39. }
  40. protected WebClientProtocol ()
  41. {
  42. connectionGroupName = String.Empty;
  43. credentials = null;
  44. preAuthenticate = false;
  45. requestEncoding = null;
  46. timeout = 100000;
  47. url = String.Empty;
  48. abort = false;
  49. }
  50. #endregion // Constructors
  51. #region Properties
  52. [DefaultValue ("")]
  53. public string ConnectionGroupName {
  54. get { return connectionGroupName; }
  55. set { connectionGroupName = value; }
  56. }
  57. public ICredentials Credentials {
  58. get { return credentials; }
  59. set { credentials = value; }
  60. }
  61. [DefaultValue (false)]
  62. [WebServicesDescription ("Enables pre authentication of the request.")]
  63. public bool PreAuthenticate {
  64. get { return preAuthenticate; }
  65. set { preAuthenticate = value; }
  66. }
  67. [DefaultValue ("")]
  68. [RecommendedAsConfigurable (true)]
  69. [WebServicesDescription ("The encoding to use for requests.")]
  70. public Encoding RequestEncoding {
  71. get { return requestEncoding; }
  72. set { requestEncoding = value; }
  73. }
  74. [DefaultValue (100000)]
  75. [RecommendedAsConfigurable (true)]
  76. [WebServicesDescription ("Sets the timeout in milliseconds to be used for synchronous calls. The default of -1 means infinite.")]
  77. public int Timeout {
  78. get { return timeout; }
  79. set { timeout = value; }
  80. }
  81. [DefaultValue ("")]
  82. [RecommendedAsConfigurable (true)]
  83. [WebServicesDescription ("The base URL to the server to use for requests.")]
  84. public string Url {
  85. get { return url; }
  86. set {
  87. url = value;
  88. uri = new Uri (url);
  89. }
  90. }
  91. #endregion // Properties
  92. #region Methods
  93. public virtual void Abort ()
  94. {
  95. if (current_request != null){
  96. current_request.Abort ();
  97. current_request = null;
  98. }
  99. abort = true;
  100. }
  101. protected static void AddToCache (Type type, object value)
  102. {
  103. cache [type] = value;
  104. }
  105. protected static object GetFromCache (Type type)
  106. {
  107. return cache [type];
  108. }
  109. protected virtual WebRequest GetWebRequest (Uri uri)
  110. {
  111. if (uri == null)
  112. throw new InvalidOperationException ("uri is null");
  113. current_request = WebRequest.Create (uri);
  114. current_request.Timeout = timeout;
  115. current_request.PreAuthenticate = preAuthenticate;
  116. current_request.ConnectionGroupName = connectionGroupName;
  117. if (credentials != null)
  118. current_request.Credentials = credentials;
  119. return current_request;
  120. }
  121. protected virtual WebResponse GetWebResponse (WebRequest request)
  122. {
  123. if (abort)
  124. throw new WebException ("The operation has been aborted.", WebExceptionStatus.RequestCanceled);
  125. WebResponse response = null;
  126. try {
  127. request.Timeout = timeout;
  128. response = request.GetResponse ();
  129. } catch (WebException e) {
  130. response = e.Response;
  131. if (response == null)
  132. throw;
  133. }
  134. return response;
  135. }
  136. protected virtual WebResponse GetWebResponse (WebRequest request, IAsyncResult result)
  137. {
  138. if (abort)
  139. throw new WebException ("The operation has been aborted.", WebExceptionStatus.RequestCanceled);
  140. return request.EndGetResponse (result);
  141. }
  142. #endregion // Methods
  143. }
  144. }