Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending SOAP message with C# Help Needed

I would like to send a SOAP Message to a Web Service and read the response. My code is as follows: I will appreciate your help.

I hope my question is not repeated, I have looked around for a solution however I have not been successful.

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 = "[email protected]";
        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 suggested by Darin. I did as instructed however the following line of code

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

gives the error

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

Any help will be greatly appreciated

like image 560
user1048006 Avatar asked Jan 19 '23 06:01

user1048006


1 Answers

The web service you are trying to consume offers a WSDL at the following address. So simply right click on the References in the solution explorer and use the Add Service Reference dialog in Visual Studio and point to the WSDL and it will generate strongly typed classes for you to easily consume the service, just like this:

protected void sendSoapMessage()
{
    using (var client = new RegistrationBindingsClient("RegistrationBindings"))
    {
        var registration = new RegistrationType();
        registration.Source = new SourceType();
        registration.Source.SourceID = "50001255";
        registration.Email = "[email protected]";
        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 easy it is. You should not worry about any SOAP and XML plumbing.

And if you are interested in the actual underlying SOAP envelope that is being sent on 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>[email protected]</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>
like image 122
Darin Dimitrov Avatar answered Jan 25 '23 06:01

Darin Dimitrov