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
{
39 [System
.Runtime
.InteropServices
.ComVisible (true)]
41 public abstract class HttpSimpleClientProtocol
: HttpWebClientProtocol
{
45 internal HttpSimpleTypeStubInfo TypeStub
;
51 protected HttpSimpleClientProtocol ()
55 #endregion // Constructors
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;
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
;
76 ainfo
.Request
.BeginGetRequestStream (new AsyncCallback (AsyncGetRequestStreamDone
), ainfo
);
77 RegisterMapping (asyncState
, ainfo
);
82 ainfo
.SetCompleted (null, ex
, false);
91 void AsyncGetRequestStreamDone (IAsyncResult ar
)
93 SimpleWebClientAsyncResult ainfo
= (SimpleWebClientAsyncResult
) ar
.AsyncState
;
96 if (ainfo
.ParameterWriter
.UsesWriteRequest
)
98 Stream stream
= ainfo
.Request
.GetRequestStream ();
99 ainfo
.ParameterWriter
.WriteRequest (stream
, ainfo
.Parameters
);
103 ainfo
.Request
.BeginGetResponse (new AsyncCallback (AsyncGetResponseDone
), ainfo
);
107 ainfo
.SetCompleted (null, ex
, true);
111 void AsyncGetResponseDone (IAsyncResult ar
)
113 SimpleWebClientAsyncResult ainfo
= (SimpleWebClientAsyncResult
) ar
.AsyncState
;
114 WebResponse response
= null;
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);
127 catch (Exception ex
) {
128 ainfo
.SetCompleted (null, ex
, true);
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
;
151 if (!ainfo
.IsCompleted
)
152 ainfo
.WaitForComplete ();
154 UnregisterMapping (ainfo
.AsyncState
);
156 if (ainfo
.Exception
!= null)
157 throw ainfo
.Exception
;
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
);
180 WebResponse response
= GetWebResponse (request
);
182 MimeReturnReader returnReader
= (MimeReturnReader
) method
.ReturnReaderType
.Create ();
183 return returnReader
.Read (response
, response
.GetResponseStream ());
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
);
207 info
.Callback (args
);
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
;