【发布时间】:2011-08-25 13:37:28
【问题描述】:
最近,我遇到了 .NET (C#) 和 SOAP 传输的噩梦。 我必须使用一个网络服务(这应该是一件容易的事),但它变得很糟糕,似乎没有任何效果。
HttpWebRequest webRequest = (HttpWebRequest)System.Net.WebRequest.Create("http://api.myapi.com/apis/services/theapi");
webRequest.AllowAutoRedirect = true;
webRequest.Timeout = 1000 * 30;
webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";
webRequest.PreAuthenticate = true;
webRequest.Method = "POST";
webRequest.Headers.Add("SOAPAction: \"\"");
webRequest.Accept = "text/xml";
WebResponse webResponse = null;
try
{
webResponse = webRequest.GetResponse();
Stream Stream = webRequest.GetRequestStream();
string SoapEnvelope = "<soap:Envelope>...SOAP CODE ...</soap:Envelope>";
StreamReader streamReader = new StreamReader(webResponse.GetResponseStream(), System.Text.Encoding.UTF8);
XmlDocument SoapEnvelopeXML = new XmlDocument();
SoapEnvelopeXML.LoadXml(SoapEnvelope);
SoapEnvelopeXML.Save(Stream);
string result = streamReader.ReadToEnd();
return result;
}
当我尝试使用 Wireshark 嗅探软件包时,我得到了:
---- 客户输入 ------ POST /apis/services/theapi HTTP/1.1 用户代理:Mozilla/4.0(兼容;MSIE 5.01;Windows NT 5.0) SOAPAction:“” 接受:文本/xml 主机:api.myapi.com 连接:保持活动 ---- 服务器回答 ------ HTTP/1.1 500 内部服务器错误 日期:2011 年 5 月 14 日星期六 15:35:32 GMT X-Powered-By:Servlet 2.4; JBoss-4.0.5.GA(构建:CVSTag=Branch_4_0 日期=200610162339)/Tomcat-5.5 内容类型:text/xml;charset=ISO-8859-1 内容长度:225 连接:关闭 X-Pad:避免浏览器错误正如预期的那样,由于我没有发布 Soap 请求(请求中没有 XML),我收到一个 SOAP 错误和 ERROR 500。
有什么想法吗?
我必须以某种方式手动执行此操作。我什至尝试使用 TCPClient(在较低级别处理它),但我所有的尝试都失败了。
【问题讨论】:
-
为什么需要手动操作? VS 可以免费为您创建一个包装器。手动操作 SOAP 信封可能容易出错且很困难。