HttpWebClientProtocol.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. #endregion
  25. #region Constructors
  26. protected HttpWebClientProtocol ()
  27. {
  28. allowAutoRedirect = false;
  29. clientCertificates = null;
  30. cookieContainer = null;
  31. proxy = null; // FIXME
  32. userAgent = String.Format ("Mono Web Services Client Protocol {0}", Environment.Version);
  33. }
  34. #endregion // Constructors
  35. #region Properties
  36. [DefaultValue (false)]
  37. [WebServicesDescription ("Enable automatic handling of server redirects.")]
  38. public bool AllowAutoRedirect {
  39. get { return allowAutoRedirect; }
  40. set { allowAutoRedirect = value; }
  41. }
  42. [Browsable (false)]
  43. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  44. [WebServicesDescription ("The client certificates that will be sent to the server, if the server requests them.")]
  45. public X509CertificateCollection ClientCertificates {
  46. get {
  47. if (clientCertificates == null)
  48. clientCertificates = new X509CertificateCollection ();
  49. return clientCertificates;
  50. }
  51. }
  52. [DefaultValue (null)]
  53. [WebServicesDescription ("A container for all cookies received from servers in the current session.")]
  54. public CookieContainer CookieContainer {
  55. get { return cookieContainer; }
  56. set { cookieContainer = value; }
  57. }
  58. [Browsable (false)]
  59. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  60. public IWebProxy Proxy {
  61. get { return proxy; }
  62. set { proxy = value; }
  63. }
  64. [WebServicesDescription ("Sets the user agent http header for the request.")]
  65. public string UserAgent {
  66. get { return userAgent; }
  67. set { userAgent = value; }
  68. }
  69. #endregion // Properties
  70. #region Methods
  71. internal virtual void AddCookies (Uri uri)
  72. {
  73. if (cookieContainer == null)
  74. cookieContainer = new CookieContainer ();
  75. if (prevCookies == null || prevCookies.Count == 0)
  76. return;
  77. CookieCollection coll = cookieContainer.GetCookies (uri);
  78. foreach (Cookie prev in prevCookies) {
  79. bool dont = false;
  80. foreach (Cookie c in coll) {
  81. if (c.Equals (prev)) {
  82. dont = true;
  83. break;
  84. }
  85. }
  86. if (dont == false)
  87. cookieContainer.Add (prev);
  88. }
  89. }
  90. internal virtual void CheckForCookies (HttpWebResponse response)
  91. {
  92. CookieCollection cookies = response.Cookies;
  93. if (cookies.Count == 0)
  94. return;
  95. if (prevCookies == null)
  96. prevCookies = new CookieCollection ();
  97. foreach (Cookie c in cookies)
  98. prevCookies.Add (c);
  99. }
  100. protected override WebRequest GetWebRequest (Uri uri)
  101. {
  102. WebRequest req = base.GetWebRequest (uri);
  103. HttpWebRequest request = req as HttpWebRequest;
  104. if (request == null)
  105. return req;
  106. request.AllowAutoRedirect = allowAutoRedirect;
  107. if (clientCertificates != null)
  108. request.ClientCertificates.AddRange (clientCertificates);
  109. AddCookies (uri);
  110. request.CookieContainer = cookieContainer;
  111. if (proxy != null)
  112. request.Proxy = proxy;
  113. request.UserAgent = userAgent;
  114. return request;
  115. }
  116. protected override WebResponse GetWebResponse (WebRequest request)
  117. {
  118. WebResponse response = base.GetWebResponse (request);
  119. HttpWebResponse wr = response as HttpWebResponse;
  120. if (wr != null)
  121. CheckForCookies (wr);
  122. return response;
  123. }
  124. protected override WebResponse GetWebResponse (WebRequest request, IAsyncResult result)
  125. {
  126. WebResponse response = base.GetWebResponse (request, result);
  127. HttpWebResponse wr = response as HttpWebResponse;
  128. if (wr != null)
  129. CheckForCookies (wr);
  130. return response;
  131. }
  132. #endregion // Methods
  133. }
  134. }