add ISafeSerializationData
[mcs.git] / class / System.Web.Services / System.Web.Services.Protocols / HttpSimpleClientProtocol.cs
blob0e9a3fd5c04254135d7d43460d15cbf69537b43b
1 //
2 // System.Web.Services.Protocols.HttpSimpleClientProtocol.cs
3 //
4 // Author:
5 // Tim Coleman (tim@timcoleman.com)
6 // Lluis Sanchez Gual (lluis@ximian.com)
7 //
8 // Copyright (C) Tim Coleman, 2002
9 //
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:
19 //
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 //
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;
33 using System.Net;
34 using System.IO;
35 using System.Threading;
37 namespace System.Web.Services.Protocols {
38 #if NET_2_0
39 [System.Runtime.InteropServices.ComVisible (true)]
40 #endif
41 public abstract class HttpSimpleClientProtocol : HttpWebClientProtocol {
43 #region Fields
45 internal HttpSimpleTypeStubInfo TypeStub;
47 #endregion // Fields
49 #region Constructors
51 protected HttpSimpleClientProtocol ()
55 #endregion // Constructors
57 #region Methods
59 protected IAsyncResult BeginInvoke (string methodName, string requestUrl, object[] parameters, AsyncCallback callback, object asyncState)
61 HttpSimpleMethodStubInfo method = (HttpSimpleMethodStubInfo) TypeStub.GetMethod (methodName);
62 SimpleWebClientAsyncResult ainfo = null;
64 try
66 MimeParameterWriter parameterWriter = (MimeParameterWriter) method.ParameterWriterType.Create ();
67 string url = parameterWriter.GetRequestUrl (requestUrl, parameters);
68 WebRequest req = GetWebRequest (new Uri(url));
70 ainfo = new SimpleWebClientAsyncResult (req, callback, asyncState);
71 ainfo.Parameters = parameters;
72 ainfo.ParameterWriter = parameterWriter;
73 ainfo.Method = method;
75 ainfo.Request = req;
76 ainfo.Request.BeginGetRequestStream (new AsyncCallback (AsyncGetRequestStreamDone), ainfo);
77 RegisterMapping (asyncState, ainfo);
79 catch (Exception ex)
81 if (ainfo != null)
82 ainfo.SetCompleted (null, ex, false);
83 else
84 throw ex;
87 return ainfo;
91 void AsyncGetRequestStreamDone (IAsyncResult ar)
93 SimpleWebClientAsyncResult ainfo = (SimpleWebClientAsyncResult) ar.AsyncState;
94 try
96 if (ainfo.ParameterWriter.UsesWriteRequest)
98 Stream stream = ainfo.Request.GetRequestStream ();
99 ainfo.ParameterWriter.WriteRequest (stream, ainfo.Parameters);
100 stream.Close ();
103 ainfo.Request.BeginGetResponse (new AsyncCallback (AsyncGetResponseDone), ainfo);
105 catch (Exception ex)
107 ainfo.SetCompleted (null, ex, true);
111 void AsyncGetResponseDone (IAsyncResult ar)
113 SimpleWebClientAsyncResult ainfo = (SimpleWebClientAsyncResult) ar.AsyncState;
114 WebResponse response = null;
116 try {
117 response = GetWebResponse (ainfo.Request, ar);
119 catch (WebException ex) {
120 response = ex.Response;
121 HttpWebResponse http_response = response as HttpWebResponse;
122 if (http_response == null || http_response.StatusCode != HttpStatusCode.InternalServerError) {
123 ainfo.SetCompleted (null, ex, true);
124 return;
127 catch (Exception ex) {
128 ainfo.SetCompleted (null, ex, true);
129 return;
132 try {
133 MimeReturnReader returnReader = (MimeReturnReader) ainfo.Method.ReturnReaderType.Create ();
134 object result = returnReader.Read (response, response.GetResponseStream ());
135 ainfo.SetCompleted (result, null, true);
137 catch (Exception ex) {
138 ainfo.SetCompleted (null, ex, true);
143 protected object EndInvoke (IAsyncResult asyncResult)
145 if (!(asyncResult is SimpleWebClientAsyncResult))
146 throw new ArgumentException ("asyncResult is not the return value from BeginInvoke");
148 SimpleWebClientAsyncResult ainfo = (SimpleWebClientAsyncResult) asyncResult;
149 lock (ainfo)
151 if (!ainfo.IsCompleted)
152 ainfo.WaitForComplete ();
154 UnregisterMapping (ainfo.AsyncState);
156 if (ainfo.Exception != null)
157 throw ainfo.Exception;
158 else
159 return ainfo.Result;
163 protected object Invoke (string methodName, string requestUrl, object[] parameters)
165 HttpSimpleMethodStubInfo method = (HttpSimpleMethodStubInfo) TypeStub.GetMethod (methodName);
166 MimeParameterWriter parameterWriter = (MimeParameterWriter) method.ParameterWriterType.Create ();
168 string url = parameterWriter.GetRequestUrl (requestUrl, parameters);
169 WebRequest request = GetWebRequest (new Uri(url, true));
171 parameterWriter.InitializeRequest (request, parameters);
173 if (parameterWriter.UsesWriteRequest)
175 Stream stream = request.GetRequestStream ();
176 parameterWriter.WriteRequest (stream, parameters);
177 stream.Close ();
180 WebResponse response = GetWebResponse (request);
182 MimeReturnReader returnReader = (MimeReturnReader) method.ReturnReaderType.Create ();
183 return returnReader.Read (response, response.GetResponseStream ());
186 #if NET_2_0
188 protected void InvokeAsync (string methodName, string requestUrl, object[] parameters, SendOrPostCallback callback)
190 InvokeAsync (methodName, requestUrl, parameters, callback, null);
193 protected void InvokeAsync (string methodName, string requestUrl, object[] parameters, SendOrPostCallback callback, object userState)
195 InvokeAsyncInfo info = new InvokeAsyncInfo (callback, userState);
196 BeginInvoke (methodName, requestUrl, parameters, new AsyncCallback (InvokeAsyncCallback), info);
199 void InvokeAsyncCallback (IAsyncResult ar)
201 InvokeAsyncInfo info = (InvokeAsyncInfo) ar.AsyncState;
202 SimpleWebClientAsyncResult sar = (SimpleWebClientAsyncResult) ar;
203 InvokeCompletedEventArgs args = new InvokeCompletedEventArgs (sar.Exception, false, info.UserState, (object[]) sar.Result);
204 if (info.Context != null)
205 info.Context.Send (info.Callback, args);
206 else
207 info.Callback (args);
209 #endif
211 #endregion // Methods
214 internal class SimpleWebClientAsyncResult : WebClientAsyncResult
216 public SimpleWebClientAsyncResult (WebRequest request, AsyncCallback callback, object asyncState)
217 : base (request, callback, asyncState)
221 public object[] Parameters;
222 public HttpSimpleMethodStubInfo Method;
223 public MimeParameterWriter ParameterWriter;