使用C#Help发送SOAP消息

use*_*006 4 c# soap web-services

我想将SOAP消息发送到Web服务并读取响应.我的代码如下:我将非常感谢您的帮助.

我希望我的问题不会重复,我已经四处寻找解决方案但是我没有成功.

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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

更新按照达林的建议.我按照指示做了以下代码行

using (var client = new RegistrationBindingsClient("RegistrationBindings"))
Run Code Online (Sandbox Code Playgroud)

给出了错误

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

任何帮助将不胜感激

Dar*_*rov 8

您尝试使用的Web服务在以下地址提供WSDL .因此,只需右键单击解决方案资源管理器中的"引用",然后使用Visual Studio中的"添加服务引用"对话框并指向WSDL,它将生成强类型类,以便您轻松使用该服务,如下所示:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

看它有多容易.您不应该担心任何SOAP和XML管道.

如果您对使用此请求在线路上发送的实际基础SOAP信封感兴趣:

<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>
Run Code Online (Sandbox Code Playgroud)


小智 0

是否有任何错误消息或者您是否使用了 HTTP 监视器?

一些可能有用的链接: