【发布时间】:2014-02-26 21:42:58
【问题描述】:
我需要帮助我将 XML 包装到第三方 SOAP 服务器的 SOAP 信封中。第三方为入站请求和出站响应提供了 xsd 文件。我已经使用 xsd 工具获取了这些 XSD 文件并创建了它们的 C# 类。我的问题是我需要用 SOAP 信封包装序列化请求,但我不知道从哪里开始。我正在查看 Microsoft Web Service Enhancements 3,但它表示它仅适用于 .net 2.0 和 VS2005。我正在使用 VS2012 和 .net 4.5。另外,我研究过通过 Web 服务连接到服务器,但它似乎不兼容并且没有 WSDL。
以下是 SOAP 服务器对入站请求的期望示例。
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<soap:Body>
<GetBasicData xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="CRS610MI">
<CONO xmlns="">1</CONO>
<CUNO xmlns="">12345</CUNO>
</GetBasicData>
</soap:Body>
</soap:Envelope>
这就是序列化的 XML 字符串的样子。
<?xml version="1.0" encoding="utf-8"?>
<GetBasicData xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="CRS610MI">
<CONO xmlns="">1</CONO>
<CUNO xmlns="">12345</CUNO>
</GetBasicData>
我用于网络请求和响应的代码。
Byte[] byteArray = System.Text.UTF8Encoding.UTF8.GetBytes(data);
WebRequest webRequest = WebRequest.Create(@"http://myserver:8888");
webRequest.ContentLength = byteArray.Length;
webRequest.ContentType = @"text/xml; charset=utf-8";
webRequest.Headers.Add("SOAPAction", @"http://schemas.xmlsoap.org/soap/envelope/");
webRequest.Method = "POST";
Stream requestStream = webRequest.GetRequestStream();
requestStream.Write(byteArray, 0, byteArray.Length);
requestStream.Close();
requestStream.Dispose();
WebResponse webResponse = webRequest.GetResponse();
Stream responseStream = webResponse.GetResponseStream();
StreamReader streamReader = new StreamReader(responseStream);
String line;
while ((line = streamReader.ReadLine()) != null)
{
Debug.WriteLine(line);
}
我已经通过将我的序列化字符串替换为第三方提供的示例文件中的文本来测试我的代码,并且它按预期工作。我还拿了我的序列化字符串并将信封文本插入到正确的位置,这也有效,网络请求通过了,我得到了我正在寻找的响应。没有手动将信封文本插入到我的序列化字符串中,我该怎么办。我必须想象有一个方法或类会以标准化的方式为我处理这个问题?
【问题讨论】:
-
“标准化方式”是为服务提供一个WSDL。
-
顺便说一句,您确定该服务没有 WSDL 吗?尝试使用浏览器,并浏览到服务的地址。如果这不起作用,请尝试将
?wsdl添加到服务 URL 的末尾。
标签: c# xml web-services serialization soap