WebServiceHelper.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //
  2. // System.Web.Services.Protocols.WebServiceHelper.cs
  3. //
  4. // Author:
  5. // Lluis Sanchez Gual ([email protected])
  6. //
  7. // Copyright (C) Ximian, Inc. 2003
  8. //
  9. using System;
  10. using System.IO;
  11. using System.Net;
  12. using System.Text;
  13. using System.Xml;
  14. using System.Xml.Schema;
  15. using System.Xml.Serialization;
  16. using System.Web.Services.Description;
  17. namespace System.Web.Services.Protocols
  18. {
  19. internal class WebServiceHelper
  20. {
  21. public const string SoapEnvelopeNamespace = "http://schemas.xmlsoap.org/soap/envelope/";
  22. public static Encoding GetContentEncoding (string cts, out string content_type)
  23. {
  24. string encoding;
  25. encoding = "utf-8";
  26. int start = 0;
  27. int idx = cts.IndexOf (';');
  28. if (idx == -1)
  29. content_type = cts;
  30. else
  31. content_type = cts.Substring (0, idx);
  32. content_type = content_type.Trim ();
  33. for (start = idx + 1; idx != -1;)
  34. {
  35. idx = cts.IndexOf (";", start);
  36. string body;
  37. if (idx == -1)
  38. body = cts.Substring (start);
  39. else
  40. {
  41. body = cts.Substring (start, idx - start);
  42. start = idx + 1;
  43. }
  44. body = body.Trim ();
  45. if (body.StartsWith ("charset="))
  46. {
  47. encoding = body.Substring (8);
  48. }
  49. }
  50. return Encoding.GetEncoding (encoding);
  51. }
  52. public static void WriteSoapMessage (XmlTextWriter xtw, SoapTypeStubInfo info, SoapBindingUse methodUse, XmlSerializer bodySerializer, object bodyContent, SoapHeaderCollection headers)
  53. {
  54. xtw.WriteStartDocument ();
  55. xtw.WriteStartElement ("soap", "Envelope", WebServiceHelper.SoapEnvelopeNamespace);
  56. xtw.WriteAttributeString ("xmlns", "xsi", null, XmlSchema.InstanceNamespace);
  57. xtw.WriteAttributeString ("xmlns", "xsd", null, XmlSchema.Namespace);
  58. // Serialize headers
  59. if (headers != null)
  60. {
  61. foreach (SoapHeader header in headers)
  62. {
  63. XmlSerializer ser = info.GetHeaderSerializer (header.GetType(), methodUse);
  64. xtw.WriteStartElement ("soap", "Header", WebServiceHelper.SoapEnvelopeNamespace);
  65. ser.Serialize (xtw, header);
  66. xtw.WriteEndElement ();
  67. }
  68. }
  69. // Serialize body
  70. xtw.WriteStartElement ("soap", "Body", WebServiceHelper.SoapEnvelopeNamespace);
  71. bodySerializer.Serialize (xtw, bodyContent);
  72. xtw.WriteEndElement ();
  73. xtw.WriteEndElement ();
  74. xtw.Flush ();
  75. }
  76. public static void ReadSoapMessage (XmlTextReader xmlReader, SoapTypeStubInfo typeStubInfo, SoapBindingUse methodUse, XmlSerializer bodySerializer, out object body, out SoapHeaderCollection headers)
  77. {
  78. xmlReader.MoveToContent ();
  79. xmlReader.ReadStartElement ("Envelope", WebServiceHelper.SoapEnvelopeNamespace);
  80. headers = ReadHeaders (typeStubInfo, methodUse, xmlReader);
  81. xmlReader.MoveToContent ();
  82. xmlReader.ReadStartElement ("Body", WebServiceHelper.SoapEnvelopeNamespace);
  83. xmlReader.MoveToContent ();
  84. if (xmlReader.LocalName == "Fault" && xmlReader.NamespaceURI == SoapEnvelopeNamespace)
  85. bodySerializer = typeStubInfo.GetFaultSerializer ();
  86. body = bodySerializer.Deserialize (xmlReader);
  87. }
  88. static SoapHeaderCollection ReadHeaders (SoapTypeStubInfo typeStubInfo, SoapBindingUse methodUse, XmlTextReader xmlReader)
  89. {
  90. SoapHeaderCollection headers = new SoapHeaderCollection ();
  91. while (! (xmlReader.NodeType == XmlNodeType.Element && xmlReader.LocalName == "Body" && xmlReader.NamespaceURI == WebServiceHelper.SoapEnvelopeNamespace))
  92. {
  93. if (xmlReader.NodeType == XmlNodeType.Element && xmlReader.LocalName == "Header" && xmlReader.NamespaceURI == WebServiceHelper.SoapEnvelopeNamespace)
  94. {
  95. xmlReader.ReadStartElement ();
  96. xmlReader.MoveToContent ();
  97. XmlQualifiedName qname = new XmlQualifiedName (xmlReader.LocalName, xmlReader.NamespaceURI);
  98. XmlSerializer headerSerializer = typeStubInfo.GetHeaderSerializer (qname, methodUse);
  99. if (headerSerializer != null)
  100. {
  101. SoapHeader header = (SoapHeader) headerSerializer.Deserialize (xmlReader);
  102. headers.Add (header);
  103. }
  104. else
  105. {
  106. while (xmlReader.NodeType == XmlNodeType.EndElement)
  107. xmlReader.Skip (); // TODO: Check if the header has mustUnderstand=true
  108. xmlReader.Skip ();
  109. }
  110. }
  111. else
  112. xmlReader.Skip ();
  113. }
  114. return headers;
  115. }
  116. public static void InvalidOperation (string message, WebResponse response, Encoding enc)
  117. {
  118. if (response == null)
  119. throw new InvalidOperationException (message);
  120. if (enc == null)
  121. enc = Encoding.UTF8;
  122. StringBuilder sb = new StringBuilder ();
  123. sb.Append (message);
  124. sb.Append ("\r\nResponse error message:\r\n--\r\n");
  125. try {
  126. StreamReader resp = new StreamReader (response.GetResponseStream (), enc);
  127. sb.Append (resp.ReadToEnd ());
  128. } catch (Exception) {
  129. }
  130. throw new InvalidOperationException (sb.ToString ());
  131. }
  132. }
  133. }