HttpSimpleClientProtocol.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. }
  64. catch (Exception ex)
  65. {
  66. if (ainfo != null)
  67. ainfo.SetCompleted (null, ex, false);
  68. else
  69. throw ex;
  70. }
  71. return ainfo;
  72. }
  73. void AsyncGetRequestStreamDone (IAsyncResult ar)
  74. {
  75. SimpleWebClientAsyncResult ainfo = (SimpleWebClientAsyncResult) ar.AsyncState;
  76. try
  77. {
  78. if (ainfo.ParameterWriter.UsesWriteRequest)
  79. {
  80. Stream stream = ainfo.Request.GetRequestStream ();
  81. ainfo.ParameterWriter.WriteRequest (stream, ainfo.Parameters);
  82. stream.Close ();
  83. }
  84. ainfo.Request.BeginGetResponse (new AsyncCallback (AsyncGetResponseDone), ainfo);
  85. }
  86. catch (Exception ex)
  87. {
  88. ainfo.SetCompleted (null, ex, true);
  89. }
  90. }
  91. void AsyncGetResponseDone (IAsyncResult ar)
  92. {
  93. SimpleWebClientAsyncResult ainfo = (SimpleWebClientAsyncResult) ar.AsyncState;
  94. WebResponse response = null;
  95. try {
  96. response = GetWebResponse (ainfo.Request, ar);
  97. }
  98. catch (WebException ex) {
  99. response = ex.Response;
  100. HttpWebResponse http_response = response as HttpWebResponse;
  101. if (http_response == null || http_response.StatusCode != HttpStatusCode.InternalServerError) {
  102. ainfo.SetCompleted (null, ex, true);
  103. return;
  104. }
  105. }
  106. catch (Exception ex) {
  107. ainfo.SetCompleted (null, ex, true);
  108. return;
  109. }
  110. try {
  111. MimeReturnReader returnReader = (MimeReturnReader) ainfo.Method.ReturnReaderType.Create ();
  112. object result = returnReader.Read (response, response.GetResponseStream ());
  113. ainfo.SetCompleted (result, null, true);
  114. }
  115. catch (Exception ex) {
  116. ainfo.SetCompleted (null, ex, true);
  117. }
  118. }
  119. protected object EndInvoke (IAsyncResult asyncResult)
  120. {
  121. if (!(asyncResult is SimpleWebClientAsyncResult)) throw new ArgumentException ("asyncResult is not the return value from BeginInvoke");
  122. SimpleWebClientAsyncResult ainfo = (SimpleWebClientAsyncResult) asyncResult;
  123. lock (ainfo)
  124. {
  125. if (!ainfo.IsCompleted) ainfo.WaitForComplete ();
  126. if (ainfo.Exception != null) throw ainfo.Exception;
  127. else return ainfo.Result;
  128. }
  129. }
  130. protected object Invoke (string methodName, string requestUrl, object[] parameters)
  131. {
  132. HttpSimpleMethodStubInfo method = (HttpSimpleMethodStubInfo) TypeStub.GetMethod (methodName);
  133. MimeParameterWriter parameterWriter = (MimeParameterWriter) method.ParameterWriterType.Create ();
  134. string url = parameterWriter.GetRequestUrl (requestUrl, parameters);
  135. WebRequest request = GetWebRequest (new Uri(url, true));
  136. parameterWriter.InitializeRequest (request, parameters);
  137. if (parameterWriter.UsesWriteRequest)
  138. {
  139. Stream stream = request.GetRequestStream ();
  140. parameterWriter.WriteRequest (stream, parameters);
  141. stream.Close ();
  142. }
  143. WebResponse response = GetWebResponse (request);
  144. MimeReturnReader returnReader = (MimeReturnReader) method.ReturnReaderType.Create ();
  145. return returnReader.Read (response, response.GetResponseStream ());
  146. }
  147. #if NET_2_0
  148. protected void InvokeAsync (string methodName, string requestUrl, object[] parameters, SendOrPostCallback callback)
  149. {
  150. InvokeAsync (methodName, requestUrl, parameters, callback, null);
  151. }
  152. protected void InvokeAsync (string methodName, string requestUrl, object[] parameters, SendOrPostCallback callback, object userState)
  153. {
  154. InvokeAsyncInfo info = new InvokeAsyncInfo (callback, userState);
  155. BeginInvoke (methodName, requestUrl, parameters, new AsyncCallback (InvokeAsyncCallback), info);
  156. }
  157. void InvokeAsyncCallback (IAsyncResult ar)
  158. {
  159. InvokeAsyncInfo info = (InvokeAsyncInfo) ar.AsyncState;
  160. SimpleWebClientAsyncResult sar = (SimpleWebClientAsyncResult) ar;
  161. InvokeCompletedEventArgs args = new InvokeCompletedEventArgs (sar.Exception, false, info.UserState, (object[]) sar.Result);
  162. if (info.Context != null)
  163. info.Context.Send (info.Callback, args);
  164. else
  165. info.Callback (args);
  166. }
  167. #endif
  168. #endregion // Methods
  169. }
  170. internal class SimpleWebClientAsyncResult : WebClientAsyncResult
  171. {
  172. public SimpleWebClientAsyncResult (WebRequest request, AsyncCallback callback, object asyncState)
  173. : base (request, callback, asyncState)
  174. {
  175. }
  176. public object[] Parameters;
  177. public HttpSimpleMethodStubInfo Method;
  178. public MimeParameterWriter ParameterWriter;
  179. }
  180. }