HttpSimpleClientProtocol.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. //
  2. // System.Web.Services.Protocols.HttpSimpleClientProtocol.cs
  3. //
  4. // Author:
  5. // Tim Coleman ([email protected])
  6. // Lluis Sanchez Gual ([email protected])
  7. //
  8. // Copyright (C) Tim Coleman, 2002
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System.Web.Services;
  31. using System.Net;
  32. using System.IO;
  33. using System.Threading;
  34. namespace System.Web.Services.Protocols {
  35. #if NET_2_0
  36. [System.Runtime.InteropServices.ComVisible (true)]
  37. #endif
  38. public abstract class HttpSimpleClientProtocol : HttpWebClientProtocol {
  39. #region Fields
  40. internal HttpSimpleTypeStubInfo TypeStub;
  41. #endregion // Fields
  42. #region Constructors
  43. protected HttpSimpleClientProtocol ()
  44. {
  45. }
  46. #endregion // Constructors
  47. #region Methods
  48. protected IAsyncResult BeginInvoke (string methodName, string requestUrl, object[] parameters, AsyncCallback callback, object asyncState)
  49. {
  50. HttpSimpleMethodStubInfo method = (HttpSimpleMethodStubInfo) TypeStub.GetMethod (methodName);
  51. SimpleWebClientAsyncResult ainfo = null;
  52. try
  53. {
  54. MimeParameterWriter parameterWriter = (MimeParameterWriter) method.ParameterWriterType.Create ();
  55. string url = parameterWriter.GetRequestUrl (requestUrl, parameters);
  56. WebRequest req = GetWebRequest (new Uri(url));
  57. ainfo = new SimpleWebClientAsyncResult (req, callback, asyncState);
  58. ainfo.Parameters = parameters;
  59. ainfo.ParameterWriter = parameterWriter;
  60. ainfo.Method = method;
  61. ainfo.Request = req;
  62. ainfo.Request.BeginGetRequestStream (new AsyncCallback (AsyncGetRequestStreamDone), ainfo);
  63. RegisterMapping (asyncState, ainfo);
  64. }
  65. catch (Exception ex)
  66. {
  67. if (ainfo != null)
  68. ainfo.SetCompleted (null, ex, false);
  69. else
  70. throw ex;
  71. }
  72. return ainfo;
  73. }
  74. void AsyncGetRequestStreamDone (IAsyncResult ar)
  75. {
  76. SimpleWebClientAsyncResult ainfo = (SimpleWebClientAsyncResult) ar.AsyncState;
  77. try
  78. {
  79. if (ainfo.ParameterWriter.UsesWriteRequest)
  80. {
  81. Stream stream = ainfo.Request.GetRequestStream ();
  82. ainfo.ParameterWriter.WriteRequest (stream, ainfo.Parameters);
  83. stream.Close ();
  84. }
  85. ainfo.Request.BeginGetResponse (new AsyncCallback (AsyncGetResponseDone), ainfo);
  86. }
  87. catch (Exception ex)
  88. {
  89. ainfo.SetCompleted (null, ex, true);
  90. }
  91. }
  92. void AsyncGetResponseDone (IAsyncResult ar)
  93. {
  94. SimpleWebClientAsyncResult ainfo = (SimpleWebClientAsyncResult) ar.AsyncState;
  95. WebResponse response = null;
  96. try {
  97. response = GetWebResponse (ainfo.Request, ar);
  98. }
  99. catch (WebException ex) {
  100. response = ex.Response;
  101. HttpWebResponse http_response = response as HttpWebResponse;
  102. if (http_response == null || http_response.StatusCode != HttpStatusCode.InternalServerError) {
  103. ainfo.SetCompleted (null, ex, true);
  104. return;
  105. }
  106. }
  107. catch (Exception ex) {
  108. ainfo.SetCompleted (null, ex, true);
  109. return;
  110. }
  111. try {
  112. MimeReturnReader returnReader = (MimeReturnReader) ainfo.Method.ReturnReaderType.Create ();
  113. object result = returnReader.Read (response, response.GetResponseStream ());
  114. ainfo.SetCompleted (result, null, true);
  115. }
  116. catch (Exception ex) {
  117. ainfo.SetCompleted (null, ex, true);
  118. }
  119. }
  120. protected object EndInvoke (IAsyncResult asyncResult)
  121. {
  122. if (!(asyncResult is SimpleWebClientAsyncResult))
  123. throw new ArgumentException ("asyncResult is not the return value from BeginInvoke");
  124. SimpleWebClientAsyncResult ainfo = (SimpleWebClientAsyncResult) asyncResult;
  125. lock (ainfo)
  126. {
  127. if (!ainfo.IsCompleted)
  128. ainfo.WaitForComplete ();
  129. UnregisterMapping (ainfo.AsyncState);
  130. if (ainfo.Exception != null)
  131. throw ainfo.Exception;
  132. else
  133. return ainfo.Result;
  134. }
  135. }
  136. protected object Invoke (string methodName, string requestUrl, object[] parameters)
  137. {
  138. HttpSimpleMethodStubInfo method = (HttpSimpleMethodStubInfo) TypeStub.GetMethod (methodName);
  139. MimeParameterWriter parameterWriter = (MimeParameterWriter) method.ParameterWriterType.Create ();
  140. string url = parameterWriter.GetRequestUrl (requestUrl, parameters);
  141. WebRequest request = GetWebRequest (new Uri(url, true));
  142. parameterWriter.InitializeRequest (request, parameters);
  143. if (parameterWriter.UsesWriteRequest)
  144. {
  145. Stream stream = request.GetRequestStream ();
  146. parameterWriter.WriteRequest (stream, parameters);
  147. stream.Close ();
  148. }
  149. WebResponse response = GetWebResponse (request);
  150. MimeReturnReader returnReader = (MimeReturnReader) method.ReturnReaderType.Create ();
  151. return returnReader.Read (response, response.GetResponseStream ());
  152. }
  153. #if NET_2_0
  154. protected void InvokeAsync (string methodName, string requestUrl, object[] parameters, SendOrPostCallback callback)
  155. {
  156. InvokeAsync (methodName, requestUrl, parameters, callback, null);
  157. }
  158. protected void InvokeAsync (string methodName, string requestUrl, object[] parameters, SendOrPostCallback callback, object userState)
  159. {
  160. InvokeAsyncInfo info = new InvokeAsyncInfo (callback, userState);
  161. BeginInvoke (methodName, requestUrl, parameters, new AsyncCallback (InvokeAsyncCallback), info);
  162. }
  163. void InvokeAsyncCallback (IAsyncResult ar)
  164. {
  165. InvokeAsyncInfo info = (InvokeAsyncInfo) ar.AsyncState;
  166. SimpleWebClientAsyncResult sar = (SimpleWebClientAsyncResult) ar;
  167. InvokeCompletedEventArgs args = new InvokeCompletedEventArgs (sar.Exception, false, info.UserState, (object[]) sar.Result);
  168. if (info.Context != null)
  169. info.Context.Send (info.Callback, args);
  170. else
  171. info.Callback (args);
  172. }
  173. #endif
  174. #endregion // Methods
  175. }
  176. internal class SimpleWebClientAsyncResult : WebClientAsyncResult
  177. {
  178. public SimpleWebClientAsyncResult (WebRequest request, AsyncCallback callback, object asyncState)
  179. : base (request, callback, asyncState)
  180. {
  181. }
  182. public object[] Parameters;
  183. public HttpSimpleMethodStubInfo Method;
  184. public MimeParameterWriter ParameterWriter;
  185. }
  186. }