Modern definitions of webservice generally tend to be restfull-style HTTP requests, but they still encounter the time code written by their predecessors.
We prefer to encapsulate the code to invoke instead of the service reference. Please see:
Annotations for Service.asmx Services
1[WebService (Namespace ="http://shanghai.org/")]2[WebServiceBinding (ConformsTo =wsiprofiles.basicprofile1_1)]3[System.ComponentModel.ToolboxItem (false)]4 //To allow this Web service to be called from a script using ASP. NET AJAX, uncomment the following line. 5 //[System.Web.Script.Services.ScriptService]6 Public classService:System.Web.Services.WebService7{}
Web. config profile important node
1<system.web>2"service.requestvalidatordisabled"/>3<webServices>4<protocols>5<add name="HttpPost"/>6<add name="HttpGet"/>7</protocols>8</webServices>9<compilation debug="true"targetframework="4.0"/>Ten</system.web> One A<system.webServer> -<defaultDocument> -<files> the<add value="Service.asmx"/> -</files> -</defaultDocument> -</system.webServer>
1 Public classRequestValidatorDisabled:System.Web.Util.RequestValidator2 {3 protected Override BOOLIsvalidrequeststring (System.Web.HttpContext context,stringValue, System.Web.Util.RequestValidationSource RequestValidationSource,stringCollectionkey, out intValidationfailureindex)4 {5 6Validationfailureindex =-1;7 return true;8 }9}
The above can not be verified.
Client Invoke Tool
Public Static stringSerialize<t> (T entity,BOOLBase64 =true) { using(System.IO.StringWriter SW =NewSystem.IO.StringWriter ()) {XmlSerializer Serializer=NewXmlSerializer (typeof(T)); Serializer. Serialize (SW, entity); if(base64) {returnconvert.tobase64string (Encoding.UTF8.GetBytes (SW). ToString ())); } Else { returnSW. ToString (); } } } Public StaticT deserialize<t> (stringDataBOOLBase64 =true) { varXML =""; if(base64) {XML= Encoding.UTF8.GetString (convert.frombase64string (data. Replace (" ","+"))); } Else{XML=data; } using(System.IO.StringReader sr =NewSystem.IO.StringReader (XML)) {XmlSerializer Deserializer=NewXmlSerializer (typeof(T)); return(T) deserializer. Deserialize (SR); } }
The request method (which removes the namespace of the service returned by the server) is submitted as a post
1 Public StaticT post<t> (stringURL, dictionary<string,string>KVS)2 {3 vardata ="";4 foreach(varKinchKVS. Keys)5 {6 if(string. IsNullOrEmpty (data))7 {8Data + + K +"="+Kvs[k];9 }Ten Else One { AData + ="&"+ K +"="+Kvs[k]; - } - } the -HttpWebRequest RQ =NULL; - - Try + { -RQ = webrequest.create (URL) asHttpWebRequest; + } A Catch(Exception) {Throw NewException ("Error: WebService address is invalid"); } at -Rq. Method ="POST"; -Rq. ContentType ="application/x-www-form-urlencoded"; - - byte[] bs =Encoding.UTF8.GetBytes (data); -Rq. ContentLength =BS. Length; in Try - { to using(Stream writer =RQ. GetRequestStream ()) + { - if(BS. Length >0) the { *Writer. Write (BS,0, BS. Length); $ }Panax Notoginseng } - } the Catch(Exception) {Throw NewException ("error: Send failed, network exception"); } + A varResponse =""; the Try + { - varRP = RQ. GetResponse () asHttpWebResponse; $ $ using(Stream reader =Rp. GetResponseStream ()) - { -Response =NewStreamReader (reader, Encoding.UTF8). ReadToEnd (); the } - }Wuyi Catch(Exception Err) {Throw NewException ("Error: Receive failed, network exception"); } the - Try Wu { - returnDeserialize<t> (Trimxmlns (response),false); About } $ Catch(Exception Err) {Throw NewException ("Error: Data serialization exception"); } - } - - Private Static stringTrimxmlns (stringsource) A { + returnSource. Replace ("xmlns=\ "Http://shanghai.org/\"",""); the}
Server-Side Definition interface
1[WebMethod (Description ="Import Excel Move Library schedule")]2 PublicTransexceldto Importtransexcel (stringdata)3 {4 Try5 {6 vartf = common.utilities.deserialize<transexceldto>(data);7 8 returnDataaccess.importtransexcel (TF);9 }Ten Catch(Exception e) One { A return NewTransexceldto (). Failure (98, E.message) astransexceldto; - } -}
Call for example, note the following key = data, even if the parameters of the interface
1Transexceldto request =NewTransexceldto () {SessionId =Cache.sessionid};2 3 4 varPostData =Newdictionary<string,string>();5Postdata.add ("Data", common.utilities.serialize<transexceldto>(Request));6 7Transexceldto result = common.utilities.post<transexceldto> (Metadata.webserviceurl +"/importtransexcel", PostData);
Standard WebService calls