【发布时间】:2011-11-15 16:40:14
【问题描述】:
我想向 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 服务没有 WSDL,那么您似乎在重新发明轮子。使用soap web 服务的常用方法是添加web 引用或使用wsdl.exe。这将创建代理类,为您处理所有的肥皂内容,您只需在类上调用方法即可。
标签: c# web-services soap