SoapHttpClientProtocol.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. //
  2. // System.Web.Services.Protocols.SoapHttpClientProtocol.cs
  3. //
  4. // Author:
  5. // Tim Coleman ([email protected])
  6. // Miguel de Icaza ([email protected])
  7. // Lluis Sanchez Gual ([email protected])
  8. //
  9. // Copyright (C) Tim Coleman, 2002
  10. // Copyright (C) Ximian, Inc, 2003
  11. //
  12. using System.IO;
  13. using System.Net;
  14. using System.Web;
  15. using System.Xml;
  16. using System.Text;
  17. using System.Reflection;
  18. using System.Web.Services;
  19. using System.Diagnostics;
  20. using System.Runtime.CompilerServices;
  21. using System.Web.Services.Description;
  22. using System.Xml.Serialization;
  23. using System.Xml.Schema;
  24. using System.Collections;
  25. using System.Threading;
  26. namespace System.Web.Services.Protocols {
  27. public class SoapHttpClientProtocol : HttpWebClientProtocol {
  28. SoapTypeStubInfo type_info;
  29. #region SoapWebClientAsyncResult class
  30. internal class SoapWebClientAsyncResult: WebClientAsyncResult
  31. {
  32. public SoapWebClientAsyncResult (WebRequest request, AsyncCallback callback, object asyncState)
  33. : base (request, callback, asyncState)
  34. {
  35. }
  36. public SoapClientMessage Message;
  37. public SoapExtension[] Extensions;
  38. }
  39. #endregion
  40. #region Constructors
  41. public SoapHttpClientProtocol ()
  42. {
  43. type_info = (SoapTypeStubInfo) TypeStubManager.GetTypeStub (this.GetType (), "Soap");
  44. }
  45. #endregion // Constructors
  46. #region Methods
  47. protected IAsyncResult BeginInvoke (string methodName, object[] parameters, AsyncCallback callback, object asyncState)
  48. {
  49. SoapMethodStubInfo msi = (SoapMethodStubInfo) type_info.GetMethod (methodName);
  50. SoapWebClientAsyncResult ainfo = null;
  51. try
  52. {
  53. SoapClientMessage message = new SoapClientMessage (this, msi, Url, parameters);
  54. message.CollectHeaders (this, message.MethodStubInfo.Headers, SoapHeaderDirection.In);
  55. WebRequest request = GetRequestForMessage (uri, message);
  56. ainfo = new SoapWebClientAsyncResult (request, callback, asyncState);
  57. ainfo.Message = message;
  58. ainfo.Extensions = SoapExtension.CreateExtensionChain (type_info.SoapExtensions[0], msi.SoapExtensions, type_info.SoapExtensions[1]);
  59. ainfo.Request.BeginGetRequestStream (new AsyncCallback (AsyncGetRequestStreamDone), ainfo);
  60. }
  61. catch (Exception ex)
  62. {
  63. if (ainfo != null)
  64. ainfo.SetCompleted (null, ex, false);
  65. }
  66. return ainfo;
  67. }
  68. void AsyncGetRequestStreamDone (IAsyncResult ar)
  69. {
  70. SoapWebClientAsyncResult ainfo = (SoapWebClientAsyncResult) ar.AsyncState;
  71. try
  72. {
  73. SendRequest (ainfo.Request.EndGetRequestStream (ar), ainfo.Message, ainfo.Extensions);
  74. ainfo.Request.BeginGetResponse (new AsyncCallback (AsyncGetResponseDone), ainfo);
  75. }
  76. catch (Exception ex)
  77. {
  78. ainfo.SetCompleted (null, ex, true);
  79. }
  80. }
  81. void AsyncGetResponseDone (IAsyncResult ar)
  82. {
  83. SoapWebClientAsyncResult ainfo = (SoapWebClientAsyncResult) ar.AsyncState;
  84. WebResponse response = null;
  85. try {
  86. response = GetWebResponse (ainfo.Request, ar);
  87. }
  88. catch (WebException ex) {
  89. response = ex.Response;
  90. HttpWebResponse http_response = response as HttpWebResponse;
  91. if (http_response == null || http_response.StatusCode != HttpStatusCode.InternalServerError) {
  92. ainfo.SetCompleted (null, ex, true);
  93. return;
  94. }
  95. }
  96. catch (Exception ex) {
  97. ainfo.SetCompleted (null, ex, true);
  98. return;
  99. }
  100. try {
  101. object[] result = ReceiveResponse (response, ainfo.Message, ainfo.Extensions);
  102. ainfo.SetCompleted (result, null, true);
  103. }
  104. catch (Exception ex) {
  105. ainfo.SetCompleted (null, ex, true);
  106. }
  107. }
  108. protected object[] EndInvoke (IAsyncResult asyncResult)
  109. {
  110. if (!(asyncResult is SoapWebClientAsyncResult)) throw new ArgumentException ("asyncResult is not the return value from BeginInvoke");
  111. SoapWebClientAsyncResult ainfo = (SoapWebClientAsyncResult) asyncResult;
  112. lock (ainfo)
  113. {
  114. if (!ainfo.IsCompleted) ainfo.WaitForComplete ();
  115. if (ainfo.Exception != null) throw ainfo.Exception;
  116. else return (object[]) ainfo.Result;
  117. }
  118. }
  119. [MonoTODO]
  120. public void Discover ()
  121. {
  122. throw new NotImplementedException ();
  123. }
  124. protected override WebRequest GetWebRequest (Uri uri)
  125. {
  126. return base.GetWebRequest (uri);
  127. }
  128. //
  129. // Just for debugging
  130. //
  131. void DumpStackFrames ()
  132. {
  133. StackTrace st = new StackTrace ();
  134. for (int i = 0; i < st.FrameCount; i++){
  135. StackFrame sf = st.GetFrame (i);
  136. Console.WriteLine ("At frame: {0} {1}", i, sf.GetMethod ().Name);
  137. }
  138. }
  139. WebRequest GetRequestForMessage (Uri uri, SoapClientMessage message)
  140. {
  141. WebRequest request = GetWebRequest (uri);
  142. request.Method = "POST";
  143. WebHeaderCollection headers = request.Headers;
  144. headers.Add ("SOAPAction", message.Action);
  145. request.ContentType = message.ContentType + "; charset=utf-8";
  146. return request;
  147. }
  148. void SendRequest (Stream s, SoapClientMessage message, SoapExtension[] extensions)
  149. {
  150. using (s) {
  151. if (extensions != null) {
  152. s = SoapExtension.ExecuteChainStream (extensions, s);
  153. message.SetStage (SoapMessageStage.BeforeSerialize);
  154. SoapExtension.ExecuteProcessMessage (extensions, message, true);
  155. }
  156. // What a waste of UTF8encoders, but it has to be thread safe.
  157. XmlTextWriter xtw = new XmlTextWriter (s, new UTF8Encoding (false));
  158. xtw.Formatting = Formatting.Indented;
  159. WebServiceHelper.WriteSoapMessage (xtw, type_info, message.MethodStubInfo.Use, message.MethodStubInfo.RequestSerializer, message.Parameters, message.Headers);
  160. if (extensions != null) {
  161. message.SetStage (SoapMessageStage.AfterSerialize);
  162. SoapExtension.ExecuteProcessMessage (extensions, message, true);
  163. }
  164. xtw.Flush ();
  165. xtw.Close ();
  166. }
  167. }
  168. //
  169. // TODO:
  170. // Handle other web responses (multi-output?)
  171. //
  172. object [] ReceiveResponse (WebResponse response, SoapClientMessage message, SoapExtension[] extensions)
  173. {
  174. SoapMethodStubInfo msi = message.MethodStubInfo;
  175. HttpWebResponse http_response = response as HttpWebResponse;
  176. if (http_response != null)
  177. {
  178. HttpStatusCode code = http_response.StatusCode;
  179. if (!(code == HttpStatusCode.Accepted || code == HttpStatusCode.OK || code == HttpStatusCode.InternalServerError))
  180. throw new WebException ("Request error. Return code was: " + http_response.StatusCode);
  181. }
  182. //
  183. // Remove optional encoding
  184. //
  185. string ctype;
  186. Encoding encoding = WebServiceHelper.GetContentEncoding (response.ContentType, out ctype);
  187. if (ctype != "text/xml")
  188. WebServiceHelper.InvalidOperation (
  189. "Content is not 'text/xml' but '" + response.ContentType + "'",
  190. response, encoding);
  191. Stream stream = response.GetResponseStream ();
  192. if (extensions != null) {
  193. stream = SoapExtension.ExecuteChainStream (extensions, stream);
  194. message.SetStage (SoapMessageStage.BeforeDeserialize);
  195. SoapExtension.ExecuteProcessMessage (extensions, message, false);
  196. }
  197. // Deserialize the response
  198. StreamReader reader = new StreamReader (stream, encoding, false);
  199. XmlTextReader xml_reader = new XmlTextReader (reader);
  200. SoapHeaderCollection headers;
  201. object content;
  202. WebServiceHelper.ReadSoapMessage (xml_reader, type_info, msi.Use, msi.ResponseSerializer, out content, out headers);
  203. if (content is Fault)
  204. {
  205. Fault fault = (Fault) content;
  206. SoapException ex = new SoapException (fault.faultstring, fault.faultcode, fault.faultactor, fault.detail);
  207. message.SetException (ex);
  208. }
  209. else
  210. message.OutParameters = (object[]) content;
  211. message.SetHeaders (headers);
  212. message.UpdateHeaderValues (this, message.MethodStubInfo.Headers);
  213. if (extensions != null) {
  214. message.SetStage (SoapMessageStage.AfterDeserialize);
  215. SoapExtension.ExecuteProcessMessage (extensions, message, false);
  216. }
  217. if (message.Exception == null)
  218. return message.OutParameters;
  219. else
  220. throw message.Exception;
  221. }
  222. protected object[] Invoke (string method_name, object[] parameters)
  223. {
  224. SoapMethodStubInfo msi = (SoapMethodStubInfo) type_info.GetMethod (method_name);
  225. SoapClientMessage message = new SoapClientMessage (this, msi, Url, parameters);
  226. message.CollectHeaders (this, message.MethodStubInfo.Headers, SoapHeaderDirection.In);
  227. SoapExtension[] extensions = SoapExtension.CreateExtensionChain (type_info.SoapExtensions[0], msi.SoapExtensions, type_info.SoapExtensions[1]);
  228. WebResponse response;
  229. try
  230. {
  231. WebRequest request = GetRequestForMessage (uri, message);
  232. SendRequest (request.GetRequestStream (), message, extensions);
  233. response = GetWebResponse (request);
  234. }
  235. catch (WebException ex)
  236. {
  237. response = ex.Response;
  238. HttpWebResponse http_response = response as HttpWebResponse;
  239. if (http_response == null || http_response.StatusCode != HttpStatusCode.InternalServerError)
  240. throw ex;
  241. }
  242. return ReceiveResponse (response, message, extensions);
  243. }
  244. #endregion // Methods
  245. }
  246. }