using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; using System.Xml; using System.Xml.Serialization; using ZLPlugin_LisPacs_MR.Domain.Units; namespace ZLPlugin_LisPacs_MR.Domain.Web { public class WebServiceCaller { #region Tip:使用说明 //调用示例: //Hashtable ht = new Hashtable(); //Hashtable 为webservice所需要的参数集 //ht.Add("str", "test"); //ht.Add("b", "true"); //XmlDocument xx = WebSvcCaller.QuerySoapWebService("http://localhost:81/service.asmx", "HelloWorld", ht); //MessageBox.Show(xx.OuterXml); #endregion /// /// 通用WebService调用(Soap),参数Pars为string类型的参数名、参数值 /// public static string SoapWebQuery(string URL, string MethodName, Hashtable Pars) { try { Log.Info("Web调用:"); Log.Info("URL:" + URL); Log.Info("MethodName:" + MethodName); HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URL); request.Method = "POST"; request.ContentType = "text/xml; charset=utf-8"; SetWebRequest(request); var message = Encoding.UTF8.GetBytes(ParsToSoapMessage(MethodName, Pars).OuterXml); Log.Info("message:" + Encoding.UTF8.GetString(message)); WriteRequestData(request, message); var result = ReadXmlResponse(request.GetResponse()); Log.Info("result:" + result.OuterXml); return GetReturnString(result); } catch (Exception ex) { throw new Exception("平台信息:" + ex.Message); } } /// /// 根据方法名和参数生成请求报文 /// /// 方法名 /// 参数 /// private static XmlDocument ParsToSoapMessage(string MethodName, Hashtable Pars) { XmlDocument doc = new XmlDocument(); doc.LoadXml(""); XmlDeclaration decl = doc.CreateXmlDeclaration("1.0", "utf-8", null); doc.InsertBefore(decl, doc.DocumentElement); XmlElement soapBody = doc.CreateElement("s", "Body", "http://schemas.xmlsoap.org/soap/envelope/"); soapBody.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"); soapBody.SetAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema"); XmlElement soapMethod = doc.CreateElement(MethodName); soapMethod.SetAttribute("xmlns", "http://service.delivery.gzsino.com/"); foreach (string k in Pars.Keys) { XmlElement soapPar = doc.CreateElement(k); soapPar.InnerXml = ObjectToSoapXml(Pars[k]); soapPar.SetAttribute("xmlns", ""); soapMethod.AppendChild(soapPar); } soapBody.AppendChild(soapMethod); doc.DocumentElement.AppendChild(soapBody); return doc; } /// /// 类型转化成Xml /// /// /// private static string ObjectToSoapXml(object o) { Type type = o.GetType(); if (!type.Name.Contains("string")) { XmlSerializer mySerializer = new XmlSerializer(type); MemoryStream ms = new MemoryStream(); mySerializer.Serialize(ms, o); XmlDocument doc = new XmlDocument(); doc.LoadXml(Encoding.UTF8.GetString(ms.ToArray())); if (doc.DocumentElement != null) { return doc.DocumentElement.InnerXml; } else { return o.ToString(); } } else return o.ToString(); } /// /// 设置凭证与超时时间 /// /// private static void SetWebRequest(HttpWebRequest request) { request.Credentials = CredentialCache.DefaultCredentials; request.Timeout = 10000; } /// /// 网络请求写入内容 /// /// /// private static void WriteRequestData(HttpWebRequest request, byte[] data) { request.ContentLength = data.Length; Stream writer = request.GetRequestStream(); writer.Write(data, 0, data.Length); writer.Close(); } /// /// 网络请求读取内容 /// /// /// private static XmlDocument ReadXmlResponse(WebResponse response) { StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8); string retXml = sr.ReadToEnd(); sr.Close(); XmlDocument doc = new XmlDocument(); doc.LoadXml(retXml); return doc; } /// /// 读取内容解析返回值 /// /// /// private static string GetReturnString(XmlDocument doc) { string str = doc.InnerText; str = str.Replace("&", "&"); return str; } } }