WebServiceCaller.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Xml;
  10. using System.Xml.Serialization;
  11. using ZLPlugin_LisPacs_MR.Domain.Units;
  12. namespace ZLPlugin_LisPacs_MR.Domain.Web
  13. {
  14. public class WebServiceCaller
  15. {
  16. #region Tip:使用说明
  17. //调用示例:
  18. //Hashtable ht = new Hashtable(); //Hashtable 为webservice所需要的参数集
  19. //ht.Add("str", "test");
  20. //ht.Add("b", "true");
  21. //XmlDocument xx = WebSvcCaller.QuerySoapWebService("http://localhost:81/service.asmx", "HelloWorld", ht);
  22. //MessageBox.Show(xx.OuterXml);
  23. #endregion
  24. /// <summary>
  25. /// 通用WebService调用(Soap),参数Pars为string类型的参数名、参数值
  26. /// </summary>
  27. public static string SoapWebQuery(string URL, string MethodName, Hashtable Pars)
  28. {
  29. try
  30. {
  31. Log.Info("Web调用:");
  32. Log.Info("URL:" + URL);
  33. Log.Info("MethodName:" + MethodName);
  34. HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URL);
  35. request.Method = "POST";
  36. request.ContentType = "text/xml; charset=utf-8";
  37. SetWebRequest(request);
  38. var message = Encoding.UTF8.GetBytes(ParsToSoapMessage(MethodName, Pars).OuterXml);
  39. Log.Info("message:" + Encoding.UTF8.GetString(message));
  40. WriteRequestData(request, message);
  41. var result = ReadXmlResponse(request.GetResponse());
  42. Log.Info("result:" + result.OuterXml);
  43. return GetReturnString(result);
  44. }
  45. catch (Exception ex)
  46. {
  47. throw new Exception("平台信息:" + ex.Message);
  48. }
  49. }
  50. /// <summary>
  51. /// 根据方法名和参数生成请求报文
  52. /// </summary>
  53. /// <param name="MethodName">方法名</param>
  54. /// <param name="Pars">参数</param>
  55. /// <returns></returns>
  56. private static XmlDocument ParsToSoapMessage(string MethodName, Hashtable Pars)
  57. {
  58. XmlDocument doc = new XmlDocument();
  59. doc.LoadXml("<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"></s:Envelope>");
  60. XmlDeclaration decl = doc.CreateXmlDeclaration("1.0", "utf-8", null);
  61. doc.InsertBefore(decl, doc.DocumentElement);
  62. XmlElement soapBody = doc.CreateElement("s", "Body", "http://schemas.xmlsoap.org/soap/envelope/");
  63. soapBody.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
  64. soapBody.SetAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema");
  65. XmlElement soapMethod = doc.CreateElement(MethodName);
  66. soapMethod.SetAttribute("xmlns", "http://service.delivery.gzsino.com/");
  67. foreach (string k in Pars.Keys)
  68. {
  69. XmlElement soapPar = doc.CreateElement(k);
  70. soapPar.InnerXml = ObjectToSoapXml(Pars[k]);
  71. soapPar.SetAttribute("xmlns", "");
  72. soapMethod.AppendChild(soapPar);
  73. }
  74. soapBody.AppendChild(soapMethod);
  75. doc.DocumentElement.AppendChild(soapBody);
  76. return doc;
  77. }
  78. /// <summary>
  79. /// 类型转化成Xml
  80. /// </summary>
  81. /// <param name="o"></param>
  82. /// <returns></returns>
  83. private static string ObjectToSoapXml(object o)
  84. {
  85. Type type = o.GetType();
  86. if (!type.Name.Contains("string"))
  87. {
  88. XmlSerializer mySerializer = new XmlSerializer(type);
  89. MemoryStream ms = new MemoryStream();
  90. mySerializer.Serialize(ms, o);
  91. XmlDocument doc = new XmlDocument();
  92. doc.LoadXml(Encoding.UTF8.GetString(ms.ToArray()));
  93. if (doc.DocumentElement != null)
  94. {
  95. return doc.DocumentElement.InnerXml;
  96. }
  97. else
  98. {
  99. return o.ToString();
  100. }
  101. }
  102. else return o.ToString();
  103. }
  104. /// <summary>
  105. /// 设置凭证与超时时间
  106. /// </summary>
  107. /// <param name="request"></param>
  108. private static void SetWebRequest(HttpWebRequest request)
  109. {
  110. request.Credentials = CredentialCache.DefaultCredentials;
  111. request.Timeout = 10000;
  112. }
  113. /// <summary>
  114. /// 网络请求写入内容
  115. /// </summary>
  116. /// <param name="request"></param>
  117. /// <param name="data"></param>
  118. private static void WriteRequestData(HttpWebRequest request, byte[] data)
  119. {
  120. request.ContentLength = data.Length;
  121. Stream writer = request.GetRequestStream();
  122. writer.Write(data, 0, data.Length);
  123. writer.Close();
  124. }
  125. /// <summary>
  126. /// 网络请求读取内容
  127. /// </summary>
  128. /// <param name="response"></param>
  129. /// <returns></returns>
  130. private static XmlDocument ReadXmlResponse(WebResponse response)
  131. {
  132. StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
  133. string retXml = sr.ReadToEnd();
  134. sr.Close();
  135. XmlDocument doc = new XmlDocument();
  136. doc.LoadXml(retXml);
  137. return doc;
  138. }
  139. /// <summary>
  140. /// 读取内容解析返回值
  141. /// </summary>
  142. /// <param name="doc"></param>
  143. /// <returns></returns>
  144. private static string GetReturnString(XmlDocument doc)
  145. {
  146. string str = doc.InnerText;
  147. str = str.Replace("&", "&amp;");
  148. return str;
  149. }
  150. }
  151. }