WebClientProtocol.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. static HybridDictionary cache;
  26. #endregion
  27. #region Constructors
  28. static WebClientProtocol ()
  29. {
  30. cache = new HybridDictionary ();
  31. }
  32. protected WebClientProtocol ()
  33. {
  34. connectionGroupName = String.Empty;
  35. credentials = null;
  36. preAuthenticate = false;
  37. requestEncoding = null;
  38. timeout = 100000;
  39. url = String.Empty;
  40. abort = false;
  41. }
  42. #endregion // Constructors
  43. #region Properties
  44. [DefaultValue ("")]
  45. public string ConnectionGroupName {
  46. get { return connectionGroupName; }
  47. set { connectionGroupName = value; }
  48. }
  49. public ICredentials Credentials {
  50. get { return credentials; }
  51. set { credentials = value; }
  52. }
  53. [DefaultValue (false)]
  54. [WebServicesDescription ("Enables pre authentication of the request.")]
  55. public bool PreAuthenticate {
  56. get { return preAuthenticate; }
  57. set { preAuthenticate = value; }
  58. }
  59. [DefaultValue ("")]
  60. [RecommendedAsConfigurable (true)]
  61. [WebServicesDescription ("The encoding to use for requests.")]
  62. public Encoding RequestEncoding {
  63. get { return requestEncoding; }
  64. set { requestEncoding = value; }
  65. }
  66. [DefaultValue (100000)]
  67. [RecommendedAsConfigurable (true)]
  68. [WebServicesDescription ("Sets the timeout in milliseconds to be used for synchronous calls. The default of -1 means infinite.")]
  69. public int Timeout {
  70. get { return timeout; }
  71. set { timeout = value; }
  72. }
  73. [DefaultValue ("")]
  74. [RecommendedAsConfigurable (true)]
  75. [WebServicesDescription ("The base URL to the server to use for requests.")]
  76. public string Url {
  77. get { return url; }
  78. set { url = value; }
  79. }
  80. #endregion // Properties
  81. #region Methods
  82. public virtual void Abort ()
  83. {
  84. abort = true;
  85. }
  86. protected static void AddToCache (Type type, object value)
  87. {
  88. cache [type] = value;
  89. }
  90. protected static object GetFromCache (Type type)
  91. {
  92. return cache [type];
  93. }
  94. protected virtual WebRequest GetWebRequest (Uri uri)
  95. {
  96. return WebRequest.Create (uri);
  97. }
  98. protected virtual WebResponse GetWebResponse (WebRequest request)
  99. {
  100. if (abort)
  101. throw new WebException ("The operation has been aborted.", WebExceptionStatus.RequestCanceled);
  102. return request.GetResponse ();
  103. }
  104. protected virtual WebResponse GetWebResponse (WebRequest request, IAsyncResult result)
  105. {
  106. if (abort)
  107. throw new WebException ("The operation has been aborted.", WebExceptionStatus.RequestCanceled);
  108. IAsyncResult ar = request.BeginGetResponse (null, null);
  109. ar.AsyncWaitHandle.WaitOne ();
  110. return request.EndGetResponse (result);
  111. }
  112. #endregion // Methods
  113. }
  114. }