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 public abstract class HttpSimpleClientProtocol
: HttpWebClientProtocol
{
42 internal HttpSimpleTypeStubInfo TypeStub
;
48 protected HttpSimpleClientProtocol ()
52 #endregion // Constructors
56 protected IAsyncResult
BeginInvoke (string methodName
, string requestUrl
, object[] parameters
, AsyncCallback callback
, object asyncState
)
58 HttpSimpleMethodStubInfo method
= (HttpSimpleMethodStubInfo
) TypeStub
.GetMethod (methodName
);
59 SimpleWebClientAsyncResult ainfo
= null;
63 MimeParameterWriter parameterWriter
= (MimeParameterWriter
) method
.ParameterWriterType
.Create ();
64 string url
= parameterWriter
.GetRequestUrl (requestUrl
, parameters
);
65 WebRequest req
= GetWebRequest (new Uri(url
));
67 ainfo
= new SimpleWebClientAsyncResult (req
, callback
, asyncState
);
68 ainfo
.Parameters
= parameters
;
69 ainfo
.ParameterWriter
= parameterWriter
;
70 ainfo
.Method
= method
;
73 ainfo
.Request
.BeginGetRequestStream (new AsyncCallback (AsyncGetRequestStreamDone
), ainfo
);
78 ainfo
.SetCompleted (null, ex
, false);
87 void AsyncGetRequestStreamDone (IAsyncResult ar
)
89 SimpleWebClientAsyncResult ainfo
= (SimpleWebClientAsyncResult
) ar
.AsyncState
;
92 if (ainfo
.ParameterWriter
.UsesWriteRequest
)
94 Stream stream
= ainfo
.Request
.GetRequestStream ();
95 ainfo
.ParameterWriter
.WriteRequest (stream
, ainfo
.Parameters
);
99 ainfo
.Request
.BeginGetResponse (new AsyncCallback (AsyncGetResponseDone
), ainfo
);
103 ainfo
.SetCompleted (null, ex
, true);
107 void AsyncGetResponseDone (IAsyncResult ar
)
109 SimpleWebClientAsyncResult ainfo
= (SimpleWebClientAsyncResult
) ar
.AsyncState
;
110 WebResponse response
= null;
113 response
= GetWebResponse (ainfo
.Request
, ar
);
115 catch (WebException ex
) {
116 response
= ex
.Response
;
117 HttpWebResponse http_response
= response
as HttpWebResponse
;
118 if (http_response
== null || http_response
.StatusCode
!= HttpStatusCode
.InternalServerError
) {
119 ainfo
.SetCompleted (null, ex
, true);
123 catch (Exception ex
) {
124 ainfo
.SetCompleted (null, ex
, true);
129 MimeReturnReader returnReader
= (MimeReturnReader
) ainfo
.Method
.ReturnReaderType
.Create ();
130 object result
= returnReader
.Read (response
, response
.GetResponseStream ());
131 ainfo
.SetCompleted (result
, null, true);
133 catch (Exception ex
) {
134 ainfo
.SetCompleted (null, ex
, true);
139 protected object EndInvoke (IAsyncResult asyncResult
)
141 if (!(asyncResult
is SimpleWebClientAsyncResult
)) throw new ArgumentException ("asyncResult is not the return value from BeginInvoke");
143 SimpleWebClientAsyncResult ainfo
= (SimpleWebClientAsyncResult
) asyncResult
;
146 if (!ainfo
.IsCompleted
) ainfo
.WaitForComplete ();
147 if (ainfo
.Exception
!= null) throw ainfo
.Exception
;
148 else return ainfo
.Result
;
152 protected object Invoke (string methodName
, string requestUrl
, object[] parameters
)
154 HttpSimpleMethodStubInfo method
= (HttpSimpleMethodStubInfo
) TypeStub
.GetMethod (methodName
);
155 MimeParameterWriter parameterWriter
= (MimeParameterWriter
) method
.ParameterWriterType
.Create ();
157 string url
= parameterWriter
.GetRequestUrl (requestUrl
, parameters
);
158 WebRequest request
= GetWebRequest (new Uri(url
, true));
160 parameterWriter
.InitializeRequest (request
, parameters
);
162 if (parameterWriter
.UsesWriteRequest
)
164 Stream stream
= request
.GetRequestStream ();
165 parameterWriter
.WriteRequest (stream
, parameters
);
169 WebResponse response
= GetWebResponse (request
);
171 MimeReturnReader returnReader
= (MimeReturnReader
) method
.ReturnReaderType
.Create ();
172 return returnReader
.Read (response
, response
.GetResponseStream ());
177 protected void InvokeAsync (string methodName
, string requestUrl
, object[] parameters
, SendOrPostCallback callback
)
179 InvokeAsync (methodName
, requestUrl
, parameters
, callback
, null);
182 protected void InvokeAsync (string methodName
, string requestUrl
, object[] parameters
, SendOrPostCallback callback
, object userState
)
184 InvokeAsyncInfo info
= new InvokeAsyncInfo (callback
, userState
);
185 BeginInvoke (methodName
, requestUrl
, parameters
, new AsyncCallback (InvokeAsyncCallback
), info
);
188 void InvokeAsyncCallback (IAsyncResult ar
)
190 InvokeAsyncInfo info
= (InvokeAsyncInfo
) ar
.AsyncState
;
191 SimpleWebClientAsyncResult sar
= (SimpleWebClientAsyncResult
) ar
;
192 InvokeCompletedEventArgs args
= new InvokeCompletedEventArgs (sar
.Exception
, false, info
.UserState
, (object[]) sar
.Result
);
193 if (info
.Context
!= null)
194 info
.Context
.SendOrPost (info
.Callback
, args
);
196 info
.Callback (args
);
200 #endregion // Methods
203 internal class SimpleWebClientAsyncResult
: WebClientAsyncResult
205 public SimpleWebClientAsyncResult (WebRequest request
, AsyncCallback callback
, object asyncState
)
206 : base (request
, callback
, asyncState
)
210 public object[] Parameters
;
211 public HttpSimpleMethodStubInfo Method
;
212 public MimeParameterWriter ParameterWriter
;