HttpWebClientProtocol.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. //
  2. // System.Web.Services.Protocols.HttpWebClientProtocol.cs
  3. //
  4. // Author:
  5. // Tim Coleman ([email protected])
  6. //
  7. // Copyright (C) Tim Coleman, 2002
  8. //
  9. using System;
  10. using System.ComponentModel;
  11. using System.Net;
  12. using System.Security.Cryptography.X509Certificates;
  13. using System.Threading;
  14. using System.Web.Services;
  15. namespace System.Web.Services.Protocols {
  16. public abstract class HttpWebClientProtocol : WebClientProtocol {
  17. #region Fields
  18. bool allowAutoRedirect;
  19. X509CertificateCollection clientCertificates;
  20. CookieContainer cookieContainer;
  21. IWebProxy proxy;
  22. string userAgent;
  23. CookieCollection prevCookies;
  24. #if NET_1_1
  25. bool _unsafeAuthenticated;
  26. #endif
  27. #endregion
  28. #region Constructors
  29. protected HttpWebClientProtocol ()
  30. {
  31. allowAutoRedirect = false;
  32. clientCertificates = null;
  33. cookieContainer = null;
  34. proxy = null; // FIXME
  35. userAgent = String.Format ("Mono Web Services Client Protocol {0}", Environment.Version);
  36. }
  37. #endregion // Constructors
  38. #region Properties
  39. [DefaultValue (false)]
  40. [WebServicesDescription ("Enable automatic handling of server redirects.")]
  41. public bool AllowAutoRedirect {
  42. get { return allowAutoRedirect; }
  43. set { allowAutoRedirect = value; }
  44. }
  45. [Browsable (false)]
  46. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  47. [WebServicesDescription ("The client certificates that will be sent to the server, if the server requests them.")]
  48. public X509CertificateCollection ClientCertificates {
  49. get {
  50. if (clientCertificates == null)
  51. clientCertificates = new X509CertificateCollection ();
  52. return clientCertificates;
  53. }
  54. }
  55. [DefaultValue (null)]
  56. [WebServicesDescription ("A container for all cookies received from servers in the current session.")]
  57. public CookieContainer CookieContainer {
  58. get { return cookieContainer; }
  59. set { cookieContainer = value; }
  60. }
  61. [Browsable (false)]
  62. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  63. public IWebProxy Proxy {
  64. get { return proxy; }
  65. set { proxy = value; }
  66. }
  67. [WebServicesDescription ("Sets the user agent http header for the request.")]
  68. public string UserAgent {
  69. get { return userAgent; }
  70. set { userAgent = value; }
  71. }
  72. #if NET_1_1
  73. public bool UnsafeAuthenticatedConnectionSharing
  74. {
  75. get { return _unsafeAuthenticated; }
  76. set { _unsafeAuthenticated = value; }
  77. }
  78. #endif
  79. #endregion // Properties
  80. #region Methods
  81. internal virtual void AddCookies (Uri uri)
  82. {
  83. if (cookieContainer == null)
  84. cookieContainer = new CookieContainer ();
  85. if (prevCookies == null || prevCookies.Count == 0)
  86. return;
  87. CookieCollection coll = cookieContainer.GetCookies (uri);
  88. foreach (Cookie prev in prevCookies) {
  89. bool dont = false;
  90. foreach (Cookie c in coll) {
  91. if (c.Equals (prev)) {
  92. dont = true;
  93. break;
  94. }
  95. }
  96. if (dont == false)
  97. cookieContainer.Add (prev);
  98. }
  99. }
  100. internal virtual void CheckForCookies (HttpWebResponse response)
  101. {
  102. CookieCollection cookies = response.Cookies;
  103. if (cookies.Count == 0)
  104. return;
  105. if (prevCookies == null)
  106. prevCookies = new CookieCollection ();
  107. foreach (Cookie c in cookies)
  108. prevCookies.Add (c);
  109. }
  110. protected override WebRequest GetWebRequest (Uri uri)
  111. {
  112. WebRequest req = base.GetWebRequest (uri);
  113. HttpWebRequest request = req as HttpWebRequest;
  114. if (request == null)
  115. return req;
  116. request.AllowAutoRedirect = allowAutoRedirect;
  117. if (clientCertificates != null)
  118. request.ClientCertificates.AddRange (clientCertificates);
  119. AddCookies (uri);
  120. request.CookieContainer = cookieContainer;
  121. if (proxy != null)
  122. request.Proxy = proxy;
  123. request.UserAgent = userAgent;
  124. #if NET_1_1
  125. // request.UnsafeAuthenticatedConnectionSharing = _unsafeAuthenticated;
  126. #endif
  127. return request;
  128. }
  129. protected override WebResponse GetWebResponse (WebRequest request)
  130. {
  131. WebResponse response = base.GetWebResponse (request);
  132. HttpWebResponse wr = response as HttpWebResponse;
  133. if (wr != null)
  134. CheckForCookies (wr);
  135. return response;
  136. }
  137. protected override WebResponse GetWebResponse (WebRequest request, IAsyncResult result)
  138. {
  139. WebResponse response = base.GetWebResponse (request, result);
  140. HttpWebResponse wr = response as HttpWebResponse;
  141. if (wr != null)
  142. CheckForCookies (wr);
  143. return response;
  144. }
  145. #endregion // Methods
  146. }
  147. }