Sending SOAP message using C # help

I would like to send a SOAP message to a web service and read the response. My code is as follows: I will be grateful for your help.

I hope my question does not happen again, I was looking for a solution, but I did not succeed.

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.Services; using System.Xml; using System.Net; using System.IO; namespace TolunaPush { public partial class _Default : System.Web.UI.Page { private string sourceID = "50001255"; private string email = " adsvine@gmail.com "; private string firstName = "Muz"; private string lastName = "Khan"; private string countryID = "2000077"; private string countryLanguage = "2000240"; private string postalCode = "N19 3NU"; private string dob = "1977-03-08"; private string gender = "2000247"; protected void Page_Load(object sender, EventArgs e) { sendSoapMessage(); } protected void sendSoapMessage() { XmlDocument doc = new XmlDocument(); doc.InnerXml = @"<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:Body> <SubmitPanelist xmlns=""http://www.greenfield.com/RegistrationGateway/Messages""> <Registration xmlns=""http://www.greenfield.com/RegistrationGateway/Types""> <Source> <SourceID>" + sourceID + @"</SourceID> </Source> <Email>" + email + @"</Email> <FirstName>" + firstName + @"</FirstName> <LastName>" + lastName + @"</LastName> <CountryUK> <CountryID>" + countryID + @"</CountryID> <Language>" + countryLanguage + @"</Language> <Address> <Postalcode>" + postalCode + @"</Postalcode> </Address> </CountryUK> <DOB>" + dob + @"</DOB> <Gender>" + gender + @"</Gender> </Registration> </SubmitPanelist> </soap:Body> </soap:Envelope>"; HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://coreg.surveycenter.com/RegistrationGateway/PanelistService.asmx"); //if (proxy != null) req.Proxy = new WebProxy(proxy, true); // req.Headers.Add("GetClientInfo", "http://tempuri.org/GetClientInfo"); req.ContentType = "text/xml;charset=\"utf-8\""; req.Accept = "text/xml"; req.Method = "POST"; Stream stm = req.GetRequestStream(); doc.Save(stm); stm.Close(); WebResponse resp = req.GetResponse(); stm = resp.GetResponseStream(); StreamReader r = new StreamReader(stm); Response.Write(r.ReadToEnd()); //Response.Write(stm.ToString()); //Response.Write(r.ToString()); Response.End(); } } } 

Update As Darin suggested. I did, as indicated, the following line of code

  using (var client = new RegistrationBindingsClient("RegistrationBindings")) 

gives an error

  The type or namespace name 'RegistrationBindingsClient' could not be found (are you missing a using directive or an assembly reference?) 

Any help would be greatly appreciated.

+4
source share
4 answers

The web service you are trying to use offers WSDL at the following address . Therefore, just right-click on the links in the solution explorer and use the "Add Service Link" dialog box in Visual Studio and point to WSDL and it will generate strongly typed classes so that you can easily use this service, for example:

 protected void sendSoapMessage() { using (var client = new RegistrationBindingsClient("RegistrationBindings")) { var registration = new RegistrationType(); registration.Source = new SourceType(); registration.Source.SourceID = "50001255"; registration.Email = " adsvine@gmail.com "; registration.FirstName = "Muz"; registration.LastName = "Khan"; var countryUK = new CountryTypeUK(); countryUK.CountryID = 2000077; countryUK.Language = 2000240; countryUK.Address = new AddressTypeUK(); countryUK.Address.Postalcode = "N19 3NU"; registration.Item = countryUK; registration.DOB = new DateTime(1977, 3, 8); registration.Gender = 2000247; client.SubmitPanelist(registration); } } 

See how simple it is. You do not have to worry about SOAP and XML plumbing.

And if you are interested in the actual basic SOAP envelope that is sent to the wire using this request:

 <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <SubmitPanelist xmlns="http://www.greenfield.com/RegistrationGateway/Messages"> <Registration xmlns="http://www.greenfield.com/RegistrationGateway/Types"> <Source> <SourceID>50001255</SourceID> </Source> <Email> adsvine@gmail.com </Email> <FirstName>Muz</FirstName> <LastName>Khan</LastName> <CountryUK> <CountryID>2000077</CountryID> <Language>2000240</Language> <Income>0</Income> <Education>0</Education> <Address> <Postalcode>N19 3NU</Postalcode> </Address> </CountryUK> <DOB>1977-03-08</DOB> <Gender>2000247</Gender> </Registration> </SubmitPanelist> </s:Body> </s:Envelope> 
+8
source

Is there an error message or did you use an HTTP monitor?

Some possibly useful links:

0
source

You can access the service in two ways:

  • By adding a service web link. In the visual studio, you can right-click on your project and select the Add Web Link option, and then paste the service URL.

  • Create a client proxy from wsdl using the wsdl tool from the Visual Studio command line. The command will look like this:

c:> wsdl "http://coreg.surveycenter.com/RegistrationGateway/PanelistService.asmx?wsdl

It will generate a .cs file and output.config. Include the .cs file in your project, and you can use it directly to access the service. Make sure the configuration file entries are added to your project configuration.

If you want to use HttpWebRequest, find the following code:

 string soap = @"<?xml version=""1.0"" encoding=""utf-8""?> <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:Body> <Register xmlns=""http://tempuri.org/""> <id>123</id> <data1>string</data1> </Register> </soap:Body> </soap:Envelope>"; HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://localhost/WebServices/CustomerWebService.asmx"); req.Headers.Add("SOAPAction", "\"http://tempuri.org/Register\""); req.ContentType = "text/xml;charset=\"utf-8\""; req.Accept = "text/xml"; req.Method = "POST"; using (Stream stm = req.GetRequestStream()) { using (StreamWriter stmw = new StreamWriter(stm)) { stmw.Write(soap); } } WebResponse response = req.GetResponse(); Stream responseStream = response.GetResponseStream(); // TODO: Do whatever you need with the response 

My service looks like this:

 [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class CustomerWebService : System.Web.Services.WebService { [WebMethod] public string Register(long id, string data1) { return "ID.CUSTOMER"; } } 
0
source

Here is a general purpose class that you can use to achieve what you need.

IMPORTANT DISCLAIMER . You should use this only when you want (or need) to manually start the SOAP-based web service: in most common scenarios, you should definitely use the WSDL web services along with the Add service link . The Visual Studio function is the right way to do this.

 using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Xml; namespace Ryadel.Web.SOAP { /// <summary> /// Helper class to send custom SOAP requests. /// </summary> public static class SOAPHelper { /// <summary> /// Sends a custom sync SOAP request to given URL and receive a request /// </summary> /// <param name="url">The WebService endpoint URL</param> /// <param name="action">The WebService action name</param> /// <param name="parameters">A dictionary containing the parameters in a key-value fashion</param> /// <param name="soapAction">The SOAPAction value, as specified in the Web Service WSDL (or NULL to use the url parameter)</param> /// <param name="useSOAP12">Set this to TRUE to use the SOAP v1.2 protocol, FALSE to use the SOAP v1.1 (default)</param> /// <returns>A string containing the raw Web Service response</returns> public static string SendSOAPRequest(string url, string action, Dictionary<string, string> parameters, string soapAction = null, bool useSOAP12 = false) { // Create the SOAP envelope XmlDocument soapEnvelopeXml = new XmlDocument(); var xmlStr = (useSOAP12) ? @"<?xml version=""1.0"" encoding=""utf-8""?> <soap12:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap12=""http://www.w3.org/2003/05/soap-envelope""> <soap12:Body> <{0} xmlns=""{1}"">{2}</{0}> </soap12:Body> </soap12:Envelope>" : @"<?xml version=""1.0"" encoding=""utf-8""?> <soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema""> <soap:Body> <{0} xmlns=""{1}"">{2}</{0}> </soap:Body> </soap:Envelope>"; string parms = string.Join(string.Empty, parameters.Select(kv => String.Format("<{0}>{1}</{0}>", kv.Key, kv.Value)).ToArray()); var s = String.Format(xmlStr, action, new Uri(url).GetLeftPart(UriPartial.Authority) + "/", parms); soapEnvelopeXml.LoadXml(s); // Create the web request HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url); webRequest.Headers.Add("SOAPAction", soapAction ?? url); webRequest.ContentType = (useSOAP12) ? "application/soap+xml;charset=\"utf-8\"" : "text/xml;charset=\"utf-8\""; webRequest.Accept = (useSOAP12) ? "application/soap+xml" : "text/xml"; webRequest.Method = "POST"; // Insert SOAP envelope using (Stream stream = webRequest.GetRequestStream()) { soapEnvelopeXml.Save(stream); } // Send request and retrieve result string result; using (WebResponse response = webRequest.GetResponse()) { using (StreamReader rd = new StreamReader(response.GetResponseStream())) { result = rd.ReadToEnd(); } } return result; } } } 

For more information and details regarding this class, you can also read this post .

0
source

Source: https://habr.com/ru/post/1381338/


All Articles