HttpSimpleClientProtocol.cs 6.8 KB

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