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 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 5, 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 11 9 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-18 0 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
Using System; Using System.Collections; Using System.IO; Using System.Net; Using System.Text; Using System.Xml; Using System.Xml.Serialization; namespace Hishop.plugins {///<summary>///class webrequest/webresponse </summary> public using WebService for///invocation Class Webservicecaller {#region Tip: Use instructions//webservices should support get and post calls, Web.config should add the following code//<webservices>// <protocols>//<add name= "HttpGet"/>//<add name= "HttpPost"/>//</protocols>//</ Webservices> //Call Example://hashtable HT = new Hashtable (); Hashtable is the set of parameters required for 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 ///<summary>///need WebService support post invocation///</summary> public static XmlDocument Querypostwe Bservice (String URL, String methodname, Hashtable Pars) {HttpWebRequest request = (HttpWebRequest) httpweBrequest.create (URL + "/" + methodname); Request. method = "POST"; Request. ContentType = "application/x-www-form-urlencoded"; Setwebrequest (Request); byte[] data = Encodepars (Pars); Writerequestdata (request, data); return Readxmlresponse (Request. GetResponse ()); } ///<summary>///need webservice support get invocation///</summary> public static XmlDocument Querygetwebservice (St Ring URL, String methodname, Hashtable Pars) {HttpWebRequest request = (HttpWebRequest) httpwebrequest.create (URL + "/" + MethodName + "?" + parstostring (Pars)); Request. method = ' Get '; Request. ContentType = "application/x-www-form-urlencoded"; Setwebrequest (Request); return Readxmlresponse (Request. GetResponse ()); } ///<summary>///Universal WebService Call (SOAP), parameter pars to string type parameter name, parameter value///</summary> public static XmlDocument Querysoapwebservice (String URL, String methodname, Hashtable Pars) {if (_xmlnamespaces.containskey (URL)) {R Eturn Querysoapwebservice (URL, MethodName, Pars, _xmlnamespaces[url). ToString ()); else {return Querysoapwebservice (URL, methodname, Pars, GetNamespace (URL));} private static XmlDocument Querysoapwebservice (String URL, String methodname, Hashtable Pars, String XmlNs) {_xmln Amespaces[url] = xmlns;//Add cache, improve efficiency HttpWebRequest request = (HttpWebRequest) httpwebrequest.create (URL); Request. method = "POST"; Request. ContentType = "Text/xml; Charset=utf-8 "; Request. Headers.add ("SOAPAction", "" "+ XmlNs + (Xmlns.endswith ("/")?" ":"/") + methodname +" ""); Setwebrequest (Request); byte[] data = Encodeparstosoap (Pars, XmlNs, MethodName); Writerequestdata (request, data); XmlDocument doc = new XmlDocument (), doc2 = new XmlDocument (); doc = Readxmlresponse (request. GetResponse ()); XmlNamespaceManager mgr = new XmlNamespaceManager (Doc. NameTable); Mgr. AddNamespace ("Soap", "http://schemas.xmlsoap.org/soap/envelope/"); String retxml = doc. selectSingleNode ("//soap:body/*/*", Mgr). INNERXML; Doc2. Loadxml ("<root>" + retxml + "</root>"); AdddelAration (DOC2); return doc2; } private static string GetNamespace (String URL) {HttpWebRequest request = (HttpWebRequest) webrequest.create (URL +?) WSDL "); Setwebrequest (Request); WebResponse response = Request. GetResponse (); StreamReader sr = new StreamReader (response. GetResponseStream (), Encoding.UTF8); XmlDocument doc = new XmlDocument (); Doc. Loadxml (Sr. ReadToEnd ()); Sr. Close (); return Doc. selectSingleNode ("//@targetNamespace"). Value; } private static byte[] Encodeparstosoap (Hashtable Pars, String XmlNs, String methodname) {XmlDocument doc = new X Mldocument (); 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>"); Adddelaration (DOC); XmlElement soapbody = doc.createelement_x_x ("soap", "Body", "http://schemas.xmlsoap.org/soap/envelope/"); XmlElement soapbody = doc. createelement ("soap", "Body", "http://schemas.xmlsoap.org/soap/envelope/"); XmlElement soapmethod = doc.createelement_x_x (methodname); XmlElement Soapmethod = doc. CreateElement (methodname); Soapmethod.setattribute ("xmlns", xmlns); foreach (string k in Pars.keys) {//xmlelement Soappar = doc.createelement_x_x (k); XmlElement soappar = doc. CreateElement (k); Soappar.innerxml = Objecttosoapxml (Pars[k]); Soapmethod.appendchild (Soappar); } soapbody.appendchild (Soapmethod); Doc. Documentelement.appendchild (Soapbody); Return Encoding.UTF8.GetBytes (Doc. OuterXml); private static string Objecttosoapxml (object o) {XmlSerializer myserializer = new XmlSerializer (O.gettype ()); 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 ();}} ///<summary>///set voucher and timeout///</summary>///<param name= "Request" ></param> 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 byte[] Encodepars (Hashtable Pars) {return Encoding.UTF8.GetBytes (parstostring (Pars));} PR Ivate static String parstostring (Hashtable Pars) {StringBuilder sb = new StringBuilder (); foreach (String k in Pars.keys) {if (sb.) Length > 0) {sb. Append ("&"); }//SB. Append (Httputility.urlencode (k) + "=" + Httputility.urlencode (pars[k). ToString ())); Return SB. ToString (); } 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 void Adddelaration (XmlDocument doc) {XmlDeclaration decl = doc. Createxmldeclaration ("1.0", "Utf-8", null); Doc. InsertBefore (Decl, Doc. DocumentElement); } private static Hashtable _xmlnamespaces = new Hashtable ()//cache xmlnamespace to avoid repeated calls to GetNamespace}} |