HttpSimpleClientProtocol.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. using System.Web.Services;
  11. using System.Net;
  12. using System.IO;
  13. namespace System.Web.Services.Protocols {
  14. public abstract class HttpSimpleClientProtocol : HttpWebClientProtocol {
  15. #region Fields
  16. internal HttpSimpleTypeStubInfo TypeStub;
  17. #endregion // Fields
  18. #region Constructors
  19. protected HttpSimpleClientProtocol ()
  20. {
  21. }
  22. #endregion // Constructors
  23. #region Methods
  24. protected IAsyncResult BeginInvoke (string methodName, string requestUrl, object[] parameters, AsyncCallback callback, object asyncState)
  25. {
  26. HttpSimpleMethodStubInfo method = (HttpSimpleMethodStubInfo) TypeStub.GetMethod (methodName);
  27. SimpleWebClientAsyncResult ainfo = null;
  28. try
  29. {
  30. MimeParameterWriter parameterWriter = (MimeParameterWriter) method.ParameterWriterType.Create ();
  31. string url = parameterWriter.GetRequestUrl (requestUrl, parameters);
  32. WebRequest req = GetWebRequest (new Uri(url));
  33. ainfo = new SimpleWebClientAsyncResult (req, callback, asyncState);
  34. ainfo.Parameters = parameters;
  35. ainfo.ParameterWriter = parameterWriter;
  36. ainfo.Method = method;
  37. ainfo.Request = req;
  38. ainfo.Request.BeginGetRequestStream (new AsyncCallback (AsyncGetRequestStreamDone), ainfo);
  39. }
  40. catch (Exception ex)
  41. {
  42. if (ainfo != null)
  43. ainfo.SetCompleted (null, ex, false);
  44. else
  45. throw ex;
  46. }
  47. return ainfo;
  48. }
  49. void AsyncGetRequestStreamDone (IAsyncResult ar)
  50. {
  51. SimpleWebClientAsyncResult ainfo = (SimpleWebClientAsyncResult) ar.AsyncState;
  52. try
  53. {
  54. if (ainfo.ParameterWriter.UsesWriteRequest)
  55. {
  56. Stream stream = ainfo.Request.GetRequestStream ();
  57. ainfo.ParameterWriter.WriteRequest (stream, ainfo.Parameters);
  58. stream.Close ();
  59. }
  60. ainfo.Request.BeginGetResponse (new AsyncCallback (AsyncGetResponseDone), ainfo);
  61. }
  62. catch (Exception ex)
  63. {
  64. ainfo.SetCompleted (null, ex, true);
  65. }
  66. }
  67. void AsyncGetResponseDone (IAsyncResult ar)
  68. {
  69. SimpleWebClientAsyncResult ainfo = (SimpleWebClientAsyncResult) ar.AsyncState;
  70. WebResponse response = null;
  71. try {
  72. response = GetWebResponse (ainfo.Request, ar);
  73. }
  74. catch (WebException ex) {
  75. response = ex.Response;
  76. HttpWebResponse http_response = response as HttpWebResponse;
  77. if (http_response == null || http_response.StatusCode != HttpStatusCode.InternalServerError) {
  78. ainfo.SetCompleted (null, ex, true);
  79. return;
  80. }
  81. }
  82. catch (Exception ex) {
  83. ainfo.SetCompleted (null, ex, true);
  84. return;
  85. }
  86. try {
  87. MimeReturnReader returnReader = (MimeReturnReader) ainfo.Method.ReturnReaderType.Create ();
  88. object result = returnReader.Read (response, response.GetResponseStream ());
  89. ainfo.SetCompleted (result, null, true);
  90. }
  91. catch (Exception ex) {
  92. ainfo.SetCompleted (null, ex, true);
  93. }
  94. }
  95. protected object EndInvoke (IAsyncResult asyncResult)
  96. {
  97. if (!(asyncResult is SimpleWebClientAsyncResult)) throw new ArgumentException ("asyncResult is not the return value from BeginInvoke");
  98. SimpleWebClientAsyncResult ainfo = (SimpleWebClientAsyncResult) asyncResult;
  99. lock (ainfo)
  100. {
  101. if (!ainfo.IsCompleted) ainfo.WaitForComplete ();
  102. if (ainfo.Exception != null) throw ainfo.Exception;
  103. else return ainfo.Result;
  104. }
  105. }
  106. protected object Invoke (string methodName, string requestUrl, object[] parameters)
  107. {
  108. HttpSimpleMethodStubInfo method = (HttpSimpleMethodStubInfo) TypeStub.GetMethod (methodName);
  109. MimeParameterWriter parameterWriter = (MimeParameterWriter) method.ParameterWriterType.Create ();
  110. string url = parameterWriter.GetRequestUrl (requestUrl, parameters);
  111. WebRequest request = GetWebRequest (new Uri(url, true));
  112. parameterWriter.InitializeRequest (request, parameters);
  113. if (parameterWriter.UsesWriteRequest)
  114. {
  115. Stream stream = request.GetRequestStream ();
  116. parameterWriter.WriteRequest (stream, parameters);
  117. stream.Close ();
  118. }
  119. WebResponse response = GetWebResponse (request);
  120. MimeReturnReader returnReader = (MimeReturnReader) method.ReturnReaderType.Create ();
  121. return returnReader.Read (response, response.GetResponseStream ());
  122. }
  123. #endregion // Methods
  124. }
  125. internal class SimpleWebClientAsyncResult : WebClientAsyncResult
  126. {
  127. public SimpleWebClientAsyncResult (WebRequest request, AsyncCallback callback, object asyncState)
  128. : base (request, callback, asyncState)
  129. {
  130. }
  131. public object[] Parameters;
  132. public HttpSimpleMethodStubInfo Method;
  133. public MimeParameterWriter ParameterWriter;
  134. }
  135. }