使用 C# 帮助发送 SOAP 消息需要 [英] Sending SOAP message with C# Help Needed

查看:16
本文介绍了使用 C# 帮助发送 SOAP 消息需要的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

给出错误

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

任何帮助将不胜感激

推荐答案

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

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 = "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);
    }
}

看看它是多么容易.您不必担心任何 SOAP 和 XML 管道.

See how easy it is. You should not worry about any SOAP and XML plumbing.

如果您对使用此请求在线发送的实际底层 SOAP 信封感兴趣:

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>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>

这篇关于使用 C# 帮助发送 SOAP 消息需要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆