gpt4 book ai didi

c# - 使用 C# 帮助发送 SOAP 消息需要

转载 作者:太空狗 更新时间:2023-10-30 00:34:17 24 4
gpt4 key购买 nike

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

我希望我的问题不再重复,我四处寻找解决方案,但没有成功。

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

更新正如达林所建议的那样。我按照指示做了但是下面的代码行

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 服务在 following address 处提供了一个 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);
}
}

看看它是多么容易。您不必担心任何 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>

关于c# - 使用 C# 帮助发送 SOAP 消息需要,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8139837/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com