1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
|
//
// System.Web.Services.Protocols.SoapHttpClientProtocol.cs
//
// Author:
// Tim Coleman (tim@timcoleman.com)
// Miguel de Icaza (miguel@ximian.com)
// Lluis Sanchez Gual (lluis@ximian.com)
//
// Copyright (C) Tim Coleman, 2002
// Copyright (C) Ximian, Inc, 2003
//
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System.ComponentModel;
using System.Globalization;
using System.IO;
using System.Net;
using System.Web;
using System.Xml;
using System.Text;
using System.Reflection;
using System.Web.Services;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Web.Services.Description;
using System.Web.Services.Discovery;
using System.Xml.Serialization;
using System.Xml.Schema;
using System.Collections;
using System.Threading;
namespace System.Web.Services.Protocols
{
[System.Runtime.InteropServices.ComVisible (true)]
public class SoapHttpClientProtocol : HttpWebClientProtocol
{
SoapTypeStubInfo type_info;
SoapProtocolVersion soapVersion;
#region SoapWebClientAsyncResult class
internal class SoapWebClientAsyncResult: WebClientAsyncResult
{
public SoapWebClientAsyncResult (WebRequest request, AsyncCallback callback, object asyncState)
: base (request, callback, asyncState)
{
}
public SoapClientMessage Message;
public SoapExtension[] Extensions;
}
#endregion
#region Constructors
public SoapHttpClientProtocol ()
{
type_info = (SoapTypeStubInfo) TypeStubManager.GetTypeStub (this.GetType (), "Soap");
}
#endregion // Constructors
#region Methods
protected IAsyncResult BeginInvoke (string methodName, object[] parameters, AsyncCallback callback, object asyncState)
{
SoapMethodStubInfo msi = (SoapMethodStubInfo) type_info.GetMethod (methodName);
SoapWebClientAsyncResult ainfo = null;
try
{
SoapClientMessage message = new SoapClientMessage (this, msi, Url, parameters);
message.CollectHeaders (this, message.MethodStubInfo.Headers, SoapHeaderDirection.In);
WebRequest request = GetRequestForMessage (uri, message);
ainfo = new SoapWebClientAsyncResult (request, callback, asyncState);
ainfo.Message = message;
ainfo.Extensions = SoapExtension.CreateExtensionChain (type_info.SoapExtensions[0], msi.SoapExtensions, type_info.SoapExtensions[1]);
ainfo.Request.BeginGetRequestStream (new AsyncCallback (AsyncGetRequestStreamDone), ainfo);
RegisterMapping (asyncState, ainfo);
}
catch (Exception ex)
{
if (ainfo != null)
ainfo.SetCompleted (null, ex, false);
}
return ainfo;
}
void AsyncGetRequestStreamDone (IAsyncResult ar)
{
SoapWebClientAsyncResult ainfo = (SoapWebClientAsyncResult) ar.AsyncState;
try
{
SendRequest (ainfo.Request.EndGetRequestStream (ar), ainfo.Message, ainfo.Extensions);
ainfo.Request.BeginGetResponse (new AsyncCallback (AsyncGetResponseDone), ainfo);
}
catch (Exception ex)
{
ainfo.SetCompleted (null, ex, true);
}
}
void AsyncGetResponseDone (IAsyncResult ar)
{
SoapWebClientAsyncResult ainfo = (SoapWebClientAsyncResult) ar.AsyncState;
WebResponse response = null;
try {
response = GetWebResponse (ainfo.Request, ar);
}
catch (WebException ex) {
response = ex.Response;
HttpWebResponse http_response = response as HttpWebResponse;
if (http_response == null || http_response.StatusCode != HttpStatusCode.InternalServerError) {
ainfo.SetCompleted (null, ex, true);
return;
}
}
catch (Exception ex) {
ainfo.SetCompleted (null, ex, true);
return;
}
try {
object[] result = ReceiveResponse (response, ainfo.Message, ainfo.Extensions);
ainfo.SetCompleted (result, null, true);
}
catch (Exception ex) {
ainfo.SetCompleted (null, ex, true);
}
finally {
response.Close();
}
}
protected object[] EndInvoke (IAsyncResult asyncResult)
{
if (!(asyncResult is SoapWebClientAsyncResult)) throw new ArgumentException ("asyncResult is not the return value from BeginInvoke");
SoapWebClientAsyncResult ainfo = (SoapWebClientAsyncResult) asyncResult;
lock (ainfo)
{
if (!ainfo.IsCompleted)
ainfo.WaitForComplete ();
UnregisterMapping (ainfo.AsyncState);
if (ainfo.Exception != null)
throw ainfo.Exception;
else
return (object[]) ainfo.Result;
}
}
public void Discover ()
{
BindingInfo bnd = (BindingInfo) type_info.Bindings [0];
DiscoveryClientProtocol discoverer = new DiscoveryClientProtocol ();
discoverer.Discover (Url);
foreach (object info in discoverer.AdditionalInformation)
{
System.Web.Services.Discovery.SoapBinding sb = info as System.Web.Services.Discovery.SoapBinding;
if (sb != null && sb.Binding.Name == bnd.Name && sb.Binding.Namespace == bnd.Namespace) {
Url = sb.Address;
return;
}
}
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);
throw new Exception (msg);
}
protected override WebRequest GetWebRequest (Uri uri)
{
return base.GetWebRequest (uri);
}
WebRequest GetRequestForMessage (Uri uri, SoapClientMessage message)
{
WebRequest request = GetWebRequest (uri);
request.Method = "POST";
WebHeaderCollection headers = request.Headers;
request.ContentType = message.ContentType + "; charset=utf-8";
if (!message.IsSoap12) {
headers.Add ("SOAPAction", "\"" + message.Action + "\"");
} else {
request.ContentType += "; action=\"" + message.Action + "\"";
}
return request;
}
[MonoTODO]
protected virtual
XmlReader GetReaderForMessage (
SoapClientMessage message, int bufferSize)
{
throw new NotImplementedException ();
}
[MonoTODO]
protected virtual
XmlWriter GetWriterForMessage (
SoapClientMessage message, int bufferSize)
{
throw new NotImplementedException ();
}
void SendRequest (Stream s, SoapClientMessage message, SoapExtension[] extensions)
{
using (s) {
if (extensions != null) {
s = SoapExtension.ExecuteChainStream (extensions, s);
message.SetStage (SoapMessageStage.BeforeSerialize);
SoapExtension.ExecuteProcessMessage (extensions, message, s, true);
}
XmlTextWriter xtw = WebServiceHelper.CreateXmlWriter (s);
WebServiceHelper.WriteSoapMessage (xtw, message.MethodStubInfo, SoapHeaderDirection.In, message.Parameters, message.Headers, message.IsSoap12);
if (extensions != null) {
message.SetStage (SoapMessageStage.AfterSerialize);
SoapExtension.ExecuteProcessMessage (extensions, message, s, true);
}
xtw.Flush ();
xtw.Close ();
}
}
//
// TODO:
// Handle other web responses (multi-output?)
//
object [] ReceiveResponse (WebResponse response, SoapClientMessage message, SoapExtension[] extensions)
{
SoapMethodStubInfo msi = message.MethodStubInfo;
HttpWebResponse http_response = response as HttpWebResponse;
if (http_response != null)
{
HttpStatusCode code = http_response.StatusCode;
if (!(code == HttpStatusCode.Accepted || code == HttpStatusCode.OK || code == HttpStatusCode.InternalServerError)) {
string msg = "The request failed with HTTP status {0}: {1}";
msg = String.Format (msg, (int) code, code);
throw new WebException (msg, null, WebExceptionStatus.ProtocolError, http_response);
}
if (message.OneWay && response.ContentLength <= 0 && (code == HttpStatusCode.Accepted || code == HttpStatusCode.OK)) {
return new object[0];
}
}
//
// Remove optional encoding
//
string ctype;
Encoding encoding = WebServiceHelper.GetContentEncoding (response.ContentType, out ctype);
ctype = ctype.ToLower (CultureInfo.InvariantCulture);
if ((!message.IsSoap12 || ctype != "application/soap+xml") && ctype != "text/xml")
WebServiceHelper.InvalidOperation (
String.Format ("Not supported Content-Type in the response: '{0}'", response.ContentType),
response, encoding);
message.ContentType = ctype;
message.ContentEncoding = encoding.WebName;
Stream stream = response.GetResponseStream ();
if (extensions != null) {
stream = SoapExtension.ExecuteChainStream (extensions, stream);
message.SetStage (SoapMessageStage.BeforeDeserialize);
SoapExtension.ExecuteProcessMessage (extensions, message, stream, false);
}
// Deserialize the response
SoapHeaderCollection headers;
object content;
using (StreamReader reader = new StreamReader (stream, encoding, false)) {
XmlTextReader xml_reader = new XmlTextReader (reader);
WebServiceHelper.ReadSoapMessage (xml_reader, msi, SoapHeaderDirection.Out, message.IsSoap12, out content, out headers);
}
if (content is Soap12Fault) {
SoapException ex = WebServiceHelper.Soap12FaultToSoapException ((Soap12Fault) content);
message.SetException (ex);
}
else
if (content is Fault) {
Fault fault = (Fault) content;
SoapException ex = new SoapException (fault.faultstring, fault.faultcode, fault.faultactor, fault.detail);
message.SetException (ex);
}
else
message.OutParameters = (object[]) content;
message.SetHeaders (headers);
message.UpdateHeaderValues (this, message.MethodStubInfo.Headers);
if (extensions != null) {
message.SetStage (SoapMessageStage.AfterDeserialize);
SoapExtension.ExecuteProcessMessage (extensions, message, stream, false);
}
if (message.Exception == null)
return message.OutParameters;
else
throw message.Exception;
}
protected object[] Invoke (string method_name, object[] parameters)
{
SoapMethodStubInfo msi = (SoapMethodStubInfo) type_info.GetMethod (method_name);
SoapClientMessage message = new SoapClientMessage (this, msi, Url, parameters);
message.CollectHeaders (this, message.MethodStubInfo.Headers, SoapHeaderDirection.In);
SoapExtension[] extensions = SoapExtension.CreateExtensionChain (type_info.SoapExtensions[0], msi.SoapExtensions, type_info.SoapExtensions[1]);
WebResponse response;
try
{
WebRequest request = GetRequestForMessage (uri, message);
SendRequest (request.GetRequestStream (), message, extensions);
response = GetWebResponse (request);
}
catch (WebException ex)
{
response = ex.Response;
HttpWebResponse http_response = response as HttpWebResponse;
if (http_response == null || http_response.StatusCode != HttpStatusCode.InternalServerError)
throw ex;
}
try {
return ReceiveResponse (response, message, extensions);
}
finally {
response.Close();
}
}
[MonoTODO ("Do something with this")]
[System.Runtime.InteropServices.ComVisible(false)]
[DefaultValue (SoapProtocolVersion.Default)]
public SoapProtocolVersion SoapVersion {
get { return soapVersion; }
set { soapVersion = value; }
}
protected void InvokeAsync (string methodName, object[] parameters, SendOrPostCallback callback)
{
InvokeAsync (methodName, parameters, callback, null);
}
protected void InvokeAsync (string methodName, object[] parameters, SendOrPostCallback callback, object userState)
{
InvokeAsyncInfo info = new InvokeAsyncInfo (callback, userState);
BeginInvoke (methodName, parameters, new AsyncCallback (InvokeAsyncCallback), info);
}
void InvokeAsyncCallback (IAsyncResult ar)
{
InvokeAsyncInfo info = (InvokeAsyncInfo) ar.AsyncState;
SoapWebClientAsyncResult sar = (SoapWebClientAsyncResult) ar;
InvokeCompletedEventArgs args = new InvokeCompletedEventArgs (sar.Exception, false, info.UserState, (object[]) sar.Result);
UnregisterMapping (ar.AsyncState);
if (info.Context != null)
info.Context.Send (info.Callback, args);
else
info.Callback (args);
}
#endregion // Methods
}
}
|