HttpSimpleClientProtocol.cs 4.8 KB

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