Net 下采用GET/POST/SOAP方式动态调用WebService C#实现
一直以来,我们都为动态调用WebService方法而烦恼。在.Net环境下,最常用的方法就是采用代理类来调用WebService,可以通过改变代理类的Url属性来实现动态调用,但当xmlns改变时就会出错,似乎要重新绑定Webservice并重新编译后才能再次运行。我无意中通过百度搜索找了一个采用GET/POST/SOAP方式动态调用WebService的简易灵活方法,只需传入WebService地址、需调用的方法及其参数,就可以随时动态调用了。经过测试调用成功,现分享给大家,代码如下:
1 using System; 2 using System.Web; 3 using System.Xml; 4 using System.Collections; 5 using System.Net; 6 using System.Text; 7 using System.IO; 8 using System.Xml.Serialization; 9 10 //By huangz 2008-3-19 11 12 /**//// <summary> 13 /// 利用WebRequest/WebResponse进行WebService调用的类,By 同济黄正 http://hz932.ys168.com 2008-3-19 14 /// </summary> 15 public class WebSvcCaller 16 { 17 //<webServices> 18 // <protocols> 19 // <add name="HttpGet"/> 20 // <add name="HttpPost"/> 21 // </protocols> 22 //</webServices> 23 24 private static Hashtable _xmlNamespaces = new Hashtable();//缓存xmlNamespace,避免重复调用GetNamespace 25 26 /**//// <summary> 27 /// 需要WebService支持Post调用 28 /// </summary> 29 public static XmlDocument QueryPostWebService(String URL , String MethodName , Hashtable Pars) 30 { 31 HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URL + "/" + MethodName); 32 request.Method = "POST"; 33 request.ContentType = "application/x-www-form-urlencoded"; 34 SetWebRequest(request); 35 byte[] data = EncodePars(Pars); 36 WriteRequestData(request , data); 37 38 return ReadXmlResponse(request.GetResponse()); 39 } 40 /**//// <summary> 41 /// 需要WebService支持Get调用 42 /// </summary> 43 public static XmlDocument QueryGetWebService(String URL , String MethodName , Hashtable Pars) 44 { 45 HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URL + "/" + MethodName + "?" + ParsToString(Pars)); 46 request.Method = "GET"; 47 request.ContentType = "application/x-www-form-urlencoded"; 48 SetWebRequest(request); 49 return ReadXmlResponse(request.GetResponse()); 50 } 51 52 53 54 /**//// <summary> 55 /// 通用WebService调用(Soap),参数Pars为String类型的参数名、参数值 56 /// </summary> 57 public static XmlDocument QuerySoapWebService(String URL , String MethodName , Hashtable Pars) 58 { 59 if (_xmlNamespaces.ContainsKey(URL)) 60 { 61 return QuerySoapWebService(URL , MethodName , Pars , _xmlNamespaces[URL].ToString()); 62 } 63 else 64 { 65 return QuerySoapWebService(URL , MethodName , Pars ,GetNamespace(URL)); 66 } 67 } 68 69 70 private static XmlDocument QuerySoapWebService(String URL , String MethodName , Hashtable Pars , string XmlNs) 71 { //By 同济黄正 http://hz932.ys168.com 2008-3-19 72 _xmlNamespaces[URL] = XmlNs;//加入缓存,提高效率 73 HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URL); 74 request.Method = "POST"; 75 request.ContentType = "text/xml; charset=utf-8"; 76 request.Headers.Add("SOAPAction" , "\"" + XmlNs + (XmlNs.EndsWith("/") ? "" : "/") + MethodName + "\""); 77 SetWebRequest(request); 78 byte[] data = EncodeParsToSoap(Pars , XmlNs , MethodName); 79 WriteRequestData(request , data); 80 XmlDocument doc = new XmlDocument() , doc2 = new XmlDocument(); 81 doc = ReadXmlResponse(request.GetResponse()); 82 83 84 XmlNamespaceManager mgr = new XmlNamespaceManager(doc.NameTable); 85 mgr.AddNamespace("soap" , "http://schemas.xmlsoap.org/soap/envelope/"); 86 String RetXml = doc.SelectSingleNode("//soap:Body/*/*" , mgr).InnerXml; 87 doc2.LoadXml("<root>" + RetXml + "</root>"); 88 AddDelaration(doc2); 89 return doc2; 90 } 91 private static string GetNamespace(String URL) 92 { 93 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL + "?WSDL"); 94 SetWebRequest(request); 95 WebResponse response = request.GetResponse(); 96 StreamReader sr = new StreamReader(response.GetResponseStream() , Encoding.UTF8); 97 XmlDocument doc = new XmlDocument(); 98 doc.LoadXml(sr.ReadToEnd()); 99 sr.Close(); 100 return doc.SelectSingleNode("//@targetNamespace").Value; 101 } 102 private static byte[] EncodeParsToSoap(Hashtable Pars , String XmlNs , String MethodName) 103 { 104 XmlDocument doc = new XmlDocument(); 105 doc.LoadXml("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"></soap:Envelope>"); 106 AddDelaration(doc); 107 XmlElement soapBody = doc.CreateElement("soap" , "Body" , "http://schemas.xmlsoap.org/soap/envelope/"); 108 XmlElement soapMethod = doc.CreateElement(MethodName); 109 soapMethod.SetAttribute("xmlns" , XmlNs); 110 foreach (string k in Pars.Keys) 111 { 112 XmlElement soapPar = doc.CreateElement(k); 113 soapPar.InnerXml = ObjectToSoapXml(Pars[k]); 114 soapMethod.AppendChild(soapPar); 115 } 116 soapBody.AppendChild(soapMethod); 117 doc.DocumentElement.AppendChild(soapBody); 118 return Encoding.UTF8.GetBytes(doc.OuterXml); 119 } 120 private static string ObjectToSoapXml(object o) 121 { 122 XmlSerializer mySerializer = new XmlSerializer(o.GetType()); 123 MemoryStream ms=new MemoryStream(); 124 mySerializer.Serialize(ms,o); 125 XmlDocument doc=new XmlDocument(); 126 doc.LoadXml(Encoding.UTF8.GetString(ms.ToArray())); 127 if(doc.DocumentElement !=null) 128 { 129 return doc.DocumentElement.InnerXml ; 130 } 131 else 132 { 133 return o.ToString(); 134 } 135 } 136 private static void SetWebRequest(HttpWebRequest request) 137 { 138 request.Credentials = CredentialCache.DefaultCredentials; 139 request.Timeout = 10000; 140 } 141 142 143 private static void WriteRequestData(HttpWebRequest request , byte[] data) 144 { 145 request.ContentLength = data.Length; 146 Stream writer = request.GetRequestStream(); 147 writer.Write(data , 0 , data.Length); 148 writer.Close(); 149 } 150 151 152 private static byte[] EncodePars(Hashtable Pars) 153 { 154 return Encoding.UTF8.GetBytes(ParsToString(Pars)); 155 } 156 157 158 private static String ParsToString(Hashtable Pars) 159 { 160 StringBuilder sb = new StringBuilder(); 161 foreach (string k in Pars.Keys) 162 { 163 if (sb.Length > 0) 164 { 165 sb.Append("&"); 166 } 167 sb.Append(HttpUtility.UrlEncode(k) + "=" + HttpUtility.UrlEncode(Pars[k].ToString())); 168 } 169 return sb.ToString(); 170 } 171 172 173 private static XmlDocument ReadXmlResponse(WebResponse response) 174 { 175 StreamReader sr = new StreamReader(response.GetResponseStream() , Encoding.UTF8); 176 String retXml = sr.ReadToEnd(); 177 sr.Close(); 178 XmlDocument doc = new XmlDocument(); 179 doc.LoadXml(retXml); 180 return doc; 181 } 182 183 184 private static void AddDelaration(XmlDocument doc) 185 { 186 XmlDeclaration decl = doc.CreateXmlDeclaration("1.0" , "utf-8" , null); 187 doc.InsertBefore(decl , doc.DocumentElement); 188 } 189 }
这个类有三个公用的方法:QuerySoapWebService为通用的采用Soap方式调用WebService,QueryGetWebService采用GET方式调用,QueryPostWebService采用POST方式调用,后两个方法需要WebService服务器支持相应的调用方式。三个方法的参数和返回值相同:URL为Webservice的Url地址(以.asmx结尾的);MethodName为要调用的方法名称;Pars为参数表,它的Key为参数名称,Value为要传递的参数的值,Value可为任意对象,前提是这个对象可以被xml序列化。注意方法名称、参数名称、参数个数必须完全匹配才能正确调用。第一次以Soap方式调用时,因为需要查询WSDL获取xmlns,因此需要时间相对长些,第二次调用不用再读WSDL,直接从缓存读取。这三个方法的返回值均为XmlDocument对象,这个返回的对象可以进行各种灵活的操作。最常用的一个SelectSingleNode方法,可以让你一步定位到Xml的任何节点,再读取它的文本或属性。也可以直接调用Save保存到磁盘。采用Soap方式调用时,根结点名称固定为root。
这个类主要是利用了WebRequest/WebResponse来完成各种网络查询操作。为了精简明了,这个类中没有添加错误处理,需要在调用的地方设置异常捕获。
下面是一个调用实例:
1 protected void Page_Load(object sender , EventArgs e) 2 { 3 try 4 { 5 Hashtable pars = new Hashtable(); 6 String Url = "http://www.260dns.cn/Services/Weather.asmx"; 7 pars["city"] = "上海"; 8 pars["wdate"]="2008-3-19"; 9 XmlDocument doc = WebSvcCaller.QuerySoapWebService(Url , "GetWeather" , pars); 10 Response.Write(doc.OuterXml); 11 } 12 catch (Exception ex) 13 { 14 Response.Write(ex.Message); 15 } 16 }