2 // System.Web.Services.Protocols.HttpSimpleClientProtocol.cs
5 // Tim Coleman (tim@timcoleman.com)
6 // Lluis Sanchez Gual (lluis@ximian.com)
8 // Copyright (C) Tim Coleman, 2002
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Web.Services;
35 using System.Threading;
37 namespace System.Web.Services.Protocols {
38 [System.Runtime.InteropServices.ComVisible (true)]
39 public abstract class HttpSimpleClientProtocol : HttpWebClientProtocol {
43 internal HttpSimpleTypeStubInfo TypeStub;
49 protected HttpSimpleClientProtocol ()
53 #endregion // Constructors
57 protected IAsyncResult BeginInvoke (string methodName, string requestUrl, object[] parameters, AsyncCallback callback, object asyncState)
59 HttpSimpleMethodStubInfo method = (HttpSimpleMethodStubInfo) TypeStub.GetMethod (methodName);
60 SimpleWebClientAsyncResult ainfo = null;
64 MimeParameterWriter parameterWriter = (MimeParameterWriter) method.ParameterWriterType.Create ();
65 string url = parameterWriter.GetRequestUrl (requestUrl, parameters);
66 WebRequest req = GetWebRequest (new Uri(url));
68 ainfo = new SimpleWebClientAsyncResult (req, callback, asyncState);
69 ainfo.Parameters = parameters;
70 ainfo.ParameterWriter = parameterWriter;
71 ainfo.Method = method;
74 ainfo.Request.BeginGetRequestStream (new AsyncCallback (AsyncGetRequestStreamDone), ainfo);
75 RegisterMapping (asyncState, ainfo);
80 ainfo.SetCompleted (null, ex, false);
89 void AsyncGetRequestStreamDone (IAsyncResult ar)
91 SimpleWebClientAsyncResult ainfo = (SimpleWebClientAsyncResult) ar.AsyncState;
94 if (ainfo.ParameterWriter.UsesWriteRequest)
96 Stream stream = ainfo.Request.GetRequestStream ();
97 ainfo.ParameterWriter.WriteRequest (stream, ainfo.Parameters);
101 ainfo.Request.BeginGetResponse (new AsyncCallback (AsyncGetResponseDone), ainfo);
105 ainfo.SetCompleted (null, ex, true);
109 void AsyncGetResponseDone (IAsyncResult ar)
111 SimpleWebClientAsyncResult ainfo = (SimpleWebClientAsyncResult) ar.AsyncState;
112 WebResponse response = null;
115 response = GetWebResponse (ainfo.Request, ar);
117 catch (WebException ex) {
118 response = ex.Response;
119 HttpWebResponse http_response = response as HttpWebResponse;
120 if (http_response == null || http_response.StatusCode != HttpStatusCode.InternalServerError) {
121 ainfo.SetCompleted (null, ex, true);
125 catch (Exception ex) {
126 ainfo.SetCompleted (null, ex, true);
131 MimeReturnReader returnReader = (MimeReturnReader) ainfo.Method.ReturnReaderType.Create ();
132 object result = returnReader.Read (response, response.GetResponseStream ());
133 ainfo.SetCompleted (result, null, true);
135 catch (Exception ex) {
136 ainfo.SetCompleted (null, ex, true);
141 protected object EndInvoke (IAsyncResult asyncResult)
143 if (!(asyncResult is SimpleWebClientAsyncResult))
144 throw new ArgumentException ("asyncResult is not the return value from BeginInvoke");
146 SimpleWebClientAsyncResult ainfo = (SimpleWebClientAsyncResult) asyncResult;
149 if (!ainfo.IsCompleted)
150 ainfo.WaitForComplete ();
152 UnregisterMapping (ainfo.AsyncState);
154 if (ainfo.Exception != null)
155 throw ainfo.Exception;
161 protected object Invoke (string methodName, string requestUrl, object[] parameters)
163 HttpSimpleMethodStubInfo method = (HttpSimpleMethodStubInfo) TypeStub.GetMethod (methodName);
164 MimeParameterWriter parameterWriter = (MimeParameterWriter) method.ParameterWriterType.Create ();
166 string url = parameterWriter.GetRequestUrl (requestUrl, parameters);
167 WebRequest request = GetWebRequest (new Uri(url, true));
169 parameterWriter.InitializeRequest (request, parameters);
171 if (parameterWriter.UsesWriteRequest)
173 Stream stream = request.GetRequestStream ();
174 parameterWriter.WriteRequest (stream, parameters);
178 WebResponse response = GetWebResponse (request);
180 MimeReturnReader returnReader = (MimeReturnReader) method.ReturnReaderType.Create ();
181 return returnReader.Read (response, response.GetResponseStream ());
185 protected void InvokeAsync (string methodName, string requestUrl, object[] parameters, SendOrPostCallback callback)
187 InvokeAsync (methodName, requestUrl, parameters, callback, null);
190 protected void InvokeAsync (string methodName, string requestUrl, object[] parameters, SendOrPostCallback callback, object userState)
192 InvokeAsyncInfo info = new InvokeAsyncInfo (callback, userState);
193 BeginInvoke (methodName, requestUrl, parameters, new AsyncCallback (InvokeAsyncCallback), info);
196 void InvokeAsyncCallback (IAsyncResult ar)
198 InvokeAsyncInfo info = (InvokeAsyncInfo) ar.AsyncState;
199 SimpleWebClientAsyncResult sar = (SimpleWebClientAsyncResult) ar;
200 InvokeCompletedEventArgs args = new InvokeCompletedEventArgs (sar.Exception, false, info.UserState, (object[]) sar.Result);
201 if (info.Context != null)
202 info.Context.Send (info.Callback, args);
204 info.Callback (args);
207 #endregion // Methods
210 internal class SimpleWebClientAsyncResult : WebClientAsyncResult
212 public SimpleWebClientAsyncResult (WebRequest request, AsyncCallback callback, object asyncState)
213 : base (request, callback, asyncState)
217 public object[] Parameters;
218 public HttpSimpleMethodStubInfo Method;
219 public MimeParameterWriter ParameterWriter;