c# - Client to send SOAP request and received response -


trying create c# client (will developed windows service) sends soap requests web service (and gets results).

from question saw code:

protected virtual webrequest createrequest(isoapmessage soapmessage) {     var wr = webrequest.create(soapmessage.uri);     wr.contenttype = "text/xml;charset=utf-8";     wr.contentlength = soapmessage.contentxml.length;      wr.headers.add("soapaction", soapmessage.soapaction);     wr.credentials = soapmessage.credentials;     wr.method = "post";     wr.getrequeststream().write(encoding.utf8.getbytes(soapmessage.contentxml), 0, soapmessage.contentxml.length);      return wr; }  public interface isoapmessage {     string uri { get; }     string contentxml { get; }     string soapaction { get; }     icredentials credentials { get; } } 

looks nice, knows how use , if best practice?

however, use way same

using system.xml; using system.net; using system.io;  public static void callwebservice() {     var _url = "http://xxxxxxxxx/service1.asmx";     var _action = "http://xxxxxxxx/service1.asmx?op=helloworld";      xmldocument soapenvelopexml = createsoapenvelope();     httpwebrequest webrequest = createwebrequest(_url, _action);     insertsoapenvelopeintowebrequest(soapenvelopexml, webrequest);      // begin async call web request.     iasyncresult asyncresult = webrequest.begingetresponse(null, null);      // suspend thread until call complete. might want     // usefull here update ui.     asyncresult.asyncwaithandle.waitone();      // response completed web request.     string soapresult;     using (webresponse webresponse = webrequest.endgetresponse(asyncresult))     {         using (streamreader rd = new streamreader(webresponse.getresponsestream()))         {             soapresult = rd.readtoend();         }         console.write(soapresult);             } }  private static httpwebrequest createwebrequest(string url, string action) {     httpwebrequest webrequest = (httpwebrequest)webrequest.create(url);     webrequest.headers.add("soapaction", action);     webrequest.contenttype = "text/xml;charset=\"utf-8\"";     webrequest.accept = "text/xml";     webrequest.method = "post";     return webrequest; }  private static xmldocument createsoapenvelope() {     xmldocument soapenvelopedocument = new xmldocument();     soapenvelopedocument.loadxml(@"<soap-env:envelope xmlns:soap-env=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:xsi=""http://www.w3.org/1999/xmlschema-instance"" xmlns:xsd=""http://www.w3.org/1999/xmlschema""><soap-env:body><helloworld xmlns=""http://tempuri.org/"" soap-env:encodingstyle=""http://schemas.xmlsoap.org/soap/encoding/""><int1 xsi:type=""xsd:integer"">12</int1><int2 xsi:type=""xsd:integer"">32</int2></helloworld></soap-env:body></soap-env:envelope>");     return soapenvelopedocument; }  private static void insertsoapenvelopeintowebrequest(xmldocument soapenvelopexml, httpwebrequest webrequest) {     using (stream stream = webrequest.getrequeststream())     {         soapenvelopexml.save(stream);     } } 

Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -