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

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

问题描述

我想一个SOAP消息发送到Web服务,并读取响应。我的代码如下:我会感谢你的帮助。



我希望我的问题是不能重复的,我看了看周围的解决方案,但是我都没有成功。

 使用系统; 
使用System.Collections.Generic;
使用System.Linq的;
使用的System.Web;
使用System.Web.UI程序;使用System.Web.UI.WebControls
;使用System.Web.Services
;
使用的System.Xml;使用System.Net
;
:使用System.IO;
命名空间TolunaPush
{
公共部分类_Default:System.Web.UI.Page
{
私人字符串的sourceID =50001255;
私人字符串email =adsvine@gmail.com;
私人字符串的firstName =穆;
私人字符串的lastName =汗;
私人字符串countryID =2000077;
私人字符串countryLanguage =2000240;
私人字符串邮编=N19 3NU
私人字符串DOB =1977年3月8日;
私人字符串性别=2000247;



保护无效的Page_Load(对象发件人,EventArgs五)
{
sendSoapMessage();
}

保护无效sendSoapMessage()
{




XmlDocument的DOC =新的XmlDocument() ;
doc.InnerXml = @<肥皂:信封的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/>
<肥皂:身体与GT;
< SubmitPanelist的xmlns =http://www.greenfield.com/RegistrationGateway/Messages>
<注册的xmlns =http://www.greenfield.com/RegistrationGateway/Types>
<信源>
<&的SourceID GT; +的sourceID + @< /&的SourceID GT;
< /来源>
<电子邮件和GT; +电子邮件+ @< /电子邮件与GT;
<&名字GT; +的firstName + @< /姓>
<&姓氏GT; +的lastName + @< /姓氏>
< CountryUK>
< CountryID>中+ countryID + @< / CountryID>
<语言和GT; + countryLanguage + @< /语言与GT;
<地址>
<邮编>中+邮编+ @< /邮编>
< /地址>
< / CountryUK>
<&DOB GT; + DOB + @< / DOB>
<性别与GT; +性别+ @< /性别与GT;
< /登记>
< / SubmitPanelist>
< / SOAP:身体>
< / SOAP:信封>



HttpWebRequest的REQ =(HttpWebRequest的)WebRequest.Create(http://coreg.surveycenter.com/RegistrationGateway/PanelistService.asmx);
//如果(!代理= NULL)req.Proxy =新WebProxy(代理,真正的);
// req.Headers.Add(GetClientInfo,http://tempuri.org/GetClientInfo);

req.Co​​ntentType =文本/ XML的,字符集= \UTF-8\;
req.Accept =文/ XML;
req.Method =POST;




流STM = req.GetRequestStream();
doc.Save(STM);
stm.Close();
WebResponse的RESP = req.GetResponse();

STM = resp.GetResponseStream();
StreamReader的R =新的StreamReader(STM);
的Response.Write(r.ReadToEnd());
//Response.Write(stm.ToString());
//Response.Write(r.ToString());
到Response.End();


}
}
}

更新
的建议由达林。我照然而指示的代码



 使用(VAR的客户=新RegistrationBindingsClient(RegistrationBindings))$ B $以下行b  

给出了错误

 类型或命名空间名称'RegistrationBindingsClient'找不到(是否缺少using指令或程序集引用?)

任何帮助将不胜感激。


解决方案

您尝试使用Web服务在提供WSDL 以下地址。在Solution Explorer中的引用,以便只需右键点击使用Visual Studio和点的添加服务引用对话框的WSDL,它会产生强类型类,你可以轻松地使用该服务,就像这样:

 使用保护无效sendSoapMessage()
{
(VAR的客户=新RegistrationBindingsClient(RegistrationBindings))
{
VAR注册=新RegistrationType();
registration.Source =新SourceType中();
registration.Source.SourceID =50001255;
registration.Email =adsvine@gmail.com;
registration.FirstName =穆;
registration.LastName =汗;
变种countryUK =新CountryTypeUK();
countryUK.CountryID = 2000077;
countryUK.Language = 2000240;
countryUK.Address =新AddressTypeUK();
countryUK.Address.Postalcode =N19 3NU
registration.Item = countryUK;
registration.DOB =新日期时间(1977年,3,8);
registration.Gender = 2000247;

client.SubmitPanelist(注册);
}
}



看到它是多么容易。你不应该担心任何SOAP和XML管道



如果您有兴趣,正在使用该请求线路发送实际的底层SOAP信封:



<预类=郎咸平的XML prettyprint-覆盖> < S:信封的xmlns:S =htt​​p://schemas.xmlsoap.org/soap /信封/>
< S:身体的xmlns:XSI =htt​​p://www.w3.org/2001/XMLSchema-instance的xmlns:XSD =htt​​p://www.w3.org/2001/XMLSchema> ;
< SubmitPanelist的xmlns =htt​​p://www.greenfield.com/RegistrationGateway/Messages>
<注册的xmlns =htt​​p://www.greenfield.com/RegistrationGateway/Types>
<信源>
<&的SourceID GT; 50001255< /&的SourceID GT;
< /来源>
<电子邮件和GT; adsvine@gmail.com< /电子邮件与GT;
<&名字GT;&穆LT; /姓>
<&姓氏GT;&汗LT; /姓氏>
< CountryUK>
< CountryID> 2000077< / CountryID>
<语言和GT; 2000240< /语言与GT;
<收入及GT; 0℃; /收入及GT;
<教育与GT; 0℃; /教育与GT;
<地址>
<邮编> N19 3NU< /邮编>
< /地址>
< / CountryUK>
<&DOB GT; 1977年3月8日< / DOB>
<性别与GT; 2000247< /性别>
< /登记>
< / SubmitPanelist>
< / S:身体与GT;
< / S:信封>


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

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

解决方案

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

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>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天全站免登陆