2 // System.Web.Services.Protocols.SoapHttpClientProtocol.cs
\r
5 // Tim Coleman (tim@timcoleman.com)
\r
6 // Miguel de Icaza (miguel@ximian.com)
\r
7 // Lluis Sanchez Gual (lluis@ximian.com)
\r
9 // Copyright (C) Tim Coleman, 2002
\r
10 // Copyright (C) Ximian, Inc, 2003
\r
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
39 using System.Reflection;
\r
40 using System.Web.Services;
\r
41 using System.Diagnostics;
\r
42 using System.Runtime.CompilerServices;
\r
43 using System.Web.Services.Description;
\r
44 using System.Web.Services.Discovery;
\r
45 using System.Xml.Serialization;
\r
46 using System.Xml.Schema;
\r
47 using System.Collections;
\r
48 using System.Threading;
\r
50 namespace System.Web.Services.Protocols {
\r
51 public class SoapHttpClientProtocol : HttpWebClientProtocol {
\r
52 SoapTypeStubInfo type_info;
\r
54 #region SoapWebClientAsyncResult class
\r
56 internal class SoapWebClientAsyncResult: WebClientAsyncResult
\r
58 public SoapWebClientAsyncResult (WebRequest request, AsyncCallback callback, object asyncState)
\r
59 : base (request, callback, asyncState)
\r
63 public SoapClientMessage Message;
\r
64 public SoapExtension[] Extensions;
\r
68 #region Constructors
\r
70 public SoapHttpClientProtocol ()
\r
72 type_info = (SoapTypeStubInfo) TypeStubManager.GetTypeStub (this.GetType (), "Soap");
\r
75 #endregion // Constructors
\r
79 protected IAsyncResult BeginInvoke (string methodName, object[] parameters, AsyncCallback callback, object asyncState)
\r
81 SoapMethodStubInfo msi = (SoapMethodStubInfo) type_info.GetMethod (methodName);
\r
83 SoapWebClientAsyncResult ainfo = null;
\r
86 SoapClientMessage message = new SoapClientMessage (this, msi, Url, parameters);
\r
87 message.CollectHeaders (this, message.MethodStubInfo.Headers, SoapHeaderDirection.In);
\r
89 WebRequest request = GetRequestForMessage (uri, message);
\r
91 ainfo = new SoapWebClientAsyncResult (request, callback, asyncState);
\r
92 ainfo.Message = message;
\r
93 ainfo.Extensions = SoapExtension.CreateExtensionChain (type_info.SoapExtensions[0], msi.SoapExtensions, type_info.SoapExtensions[1]);
\r
95 ainfo.Request.BeginGetRequestStream (new AsyncCallback (AsyncGetRequestStreamDone), ainfo);
\r
97 catch (Exception ex)
\r
100 ainfo.SetCompleted (null, ex, false);
\r
106 void AsyncGetRequestStreamDone (IAsyncResult ar)
\r
108 SoapWebClientAsyncResult ainfo = (SoapWebClientAsyncResult) ar.AsyncState;
\r
111 SendRequest (ainfo.Request.EndGetRequestStream (ar), ainfo.Message, ainfo.Extensions);
\r
112 ainfo.Request.BeginGetResponse (new AsyncCallback (AsyncGetResponseDone), ainfo);
\r
114 catch (Exception ex)
\r
116 ainfo.SetCompleted (null, ex, true);
\r
120 void AsyncGetResponseDone (IAsyncResult ar)
\r
122 SoapWebClientAsyncResult ainfo = (SoapWebClientAsyncResult) ar.AsyncState;
\r
123 WebResponse response = null;
\r
126 response = GetWebResponse (ainfo.Request, ar);
\r
128 catch (WebException ex) {
\r
129 response = ex.Response;
\r
130 HttpWebResponse http_response = response as HttpWebResponse;
\r
131 if (http_response == null || http_response.StatusCode != HttpStatusCode.InternalServerError) {
\r
132 ainfo.SetCompleted (null, ex, true);
\r
136 catch (Exception ex) {
\r
137 ainfo.SetCompleted (null, ex, true);
\r
142 object[] result = ReceiveResponse (response, ainfo.Message, ainfo.Extensions);
\r
143 ainfo.SetCompleted (result, null, true);
\r
145 catch (Exception ex) {
\r
146 ainfo.SetCompleted (null, ex, true);
\r
150 protected object[] EndInvoke (IAsyncResult asyncResult)
\r
152 if (!(asyncResult is SoapWebClientAsyncResult)) throw new ArgumentException ("asyncResult is not the return value from BeginInvoke");
\r
154 SoapWebClientAsyncResult ainfo = (SoapWebClientAsyncResult) asyncResult;
\r
157 if (!ainfo.IsCompleted) ainfo.WaitForComplete ();
\r
158 if (ainfo.Exception != null) throw ainfo.Exception;
\r
159 else return (object[]) ainfo.Result;
\r
163 public void Discover ()
\r
165 BindingInfo bnd = (BindingInfo) type_info.Bindings [0];
\r
167 DiscoveryClientProtocol discoverer = new DiscoveryClientProtocol ();
\r
168 discoverer.Discover (Url);
\r
170 foreach (object info in discoverer.AdditionalInformation)
\r
172 System.Web.Services.Discovery.SoapBinding sb = info as System.Web.Services.Discovery.SoapBinding;
\r
173 if (sb != null && sb.Binding.Name == bnd.Name && sb.Binding.Namespace == bnd.Namespace) {
\r
179 string msg = string.Format ("The binding named '{0}' from namespace '{1}' was not found in the discovery document at '{2}'", bnd.Name, bnd.Namespace, Url);
\r
180 throw new Exception (msg);
\r
183 protected override WebRequest GetWebRequest (Uri uri)
\r
185 return base.GetWebRequest (uri);
\r
188 WebRequest GetRequestForMessage (Uri uri, SoapClientMessage message)
\r
190 WebRequest request = GetWebRequest (uri);
\r
191 request.Method = "POST";
\r
192 WebHeaderCollection headers = request.Headers;
\r
193 headers.Add ("SOAPAction", "\"" + message.Action + "\"");
\r
194 request.ContentType = message.ContentType + "; charset=utf-8";
\r
198 void SendRequest (Stream s, SoapClientMessage message, SoapExtension[] extensions)
\r
202 if (extensions != null) {
\r
203 s = SoapExtension.ExecuteChainStream (extensions, s);
\r
204 message.SetStage (SoapMessageStage.BeforeSerialize);
\r
205 SoapExtension.ExecuteProcessMessage (extensions, message, true);
208 XmlTextWriter xtw = WebServiceHelper.CreateXmlWriter (s);
\r
210 WebServiceHelper.WriteSoapMessage (xtw, type_info, message.MethodStubInfo.Use, message.MethodStubInfo.RequestSerializer, message.Parameters, message.Headers);
\r
212 if (extensions != null) {
\r
213 message.SetStage (SoapMessageStage.AfterSerialize);
\r
214 SoapExtension.ExecuteProcessMessage (extensions, message, true);
225 // Handle other web responses (multi-output?)
\r
227 object [] ReceiveResponse (WebResponse response, SoapClientMessage message, SoapExtension[] extensions)
\r
229 SoapMethodStubInfo msi = message.MethodStubInfo;
\r
230 HttpWebResponse http_response = response as HttpWebResponse;
\r
232 if (http_response != null)
\r
234 HttpStatusCode code = http_response.StatusCode;
\r
236 if (!(code == HttpStatusCode.Accepted || code == HttpStatusCode.OK || code == HttpStatusCode.InternalServerError)) {
\r
237 string msg = "The request failed with HTTP status {0}: {1}";
\r
238 msg = String.Format (msg, (int) code, code);
\r
239 throw new WebException (msg, null, WebExceptionStatus.ProtocolError, http_response);
\r
244 // Remove optional encoding
\r
247 Encoding encoding = WebServiceHelper.GetContentEncoding (response.ContentType, out ctype);
\r
248 if (ctype != "text/xml")
249 WebServiceHelper.InvalidOperation (
250 "Content is not 'text/xml' but '" + response.ContentType + "'",
253 message.ContentType = ctype;
\r
254 message.ContentEncoding = encoding.WebName;
\r
256 Stream stream = response.GetResponseStream ();
\r
258 if (extensions != null) {
\r
259 stream = SoapExtension.ExecuteChainStream (extensions, stream);
\r
260 message.SetStage (SoapMessageStage.BeforeDeserialize);
\r
261 SoapExtension.ExecuteProcessMessage (extensions, message, false);
264 // Deserialize the response
\r
266 StreamReader reader = new StreamReader (stream, encoding, false);
\r
267 XmlTextReader xml_reader = new XmlTextReader (reader);
\r
269 SoapHeaderCollection headers;
\r
272 WebServiceHelper.ReadSoapMessage (xml_reader, type_info, msi.Use, msi.ResponseSerializer, out content, out headers);
\r
274 if (content is Fault)
\r
276 Fault fault = (Fault) content;
\r
277 SoapException ex = new SoapException (fault.faultstring, fault.faultcode, fault.faultactor, fault.detail);
\r
278 message.SetException (ex);
\r
281 message.OutParameters = (object[]) content;
\r
283 message.SetHeaders (headers);
\r
284 message.UpdateHeaderValues (this, message.MethodStubInfo.Headers);
\r
286 if (extensions != null) {
\r
287 message.SetStage (SoapMessageStage.AfterDeserialize);
\r
288 SoapExtension.ExecuteProcessMessage (extensions, message, false);
291 if (message.Exception == null)
\r
292 return message.OutParameters;
\r
294 throw message.Exception;
\r
297 protected object[] Invoke (string method_name, object[] parameters)
\r
299 SoapMethodStubInfo msi = (SoapMethodStubInfo) type_info.GetMethod (method_name);
\r
301 SoapClientMessage message = new SoapClientMessage (this, msi, Url, parameters);
\r
302 message.CollectHeaders (this, message.MethodStubInfo.Headers, SoapHeaderDirection.In);
\r
304 SoapExtension[] extensions = SoapExtension.CreateExtensionChain (type_info.SoapExtensions[0], msi.SoapExtensions, type_info.SoapExtensions[1]);
\r
306 WebResponse response;
\r
309 WebRequest request = GetRequestForMessage (uri, message);
\r
310 SendRequest (request.GetRequestStream (), message, extensions);
\r
311 response = GetWebResponse (request);
\r
313 catch (WebException ex)
\r
315 response = ex.Response;
\r
316 HttpWebResponse http_response = response as HttpWebResponse;
\r
317 if (http_response == null || http_response.StatusCode != HttpStatusCode.InternalServerError)
\r
321 return ReceiveResponse (response, message, extensions);
\r
324 #endregion // Methods
\r