我正在尝试将 XML 直接发布到 Web 服务,而不是使用 VS 的“添加 Web 引用...”功能。这是有充分理由的。为了获得正确的测试 XML,我记录了使用标准 Web 参考生成的内容。我使用了在这个问题Capturing SOAP requests to an ASP.NET ASMX web service中找到的内容
Web 服务就是在 Visual Studio 中创建新的 Web 服务时生成的,没什么特别的。
为了尝试传递 XML 本身,我从这个问题Client to send SOAP request and received response 中借用了代码
不幸的是,当我调用它时,我得到“远程服务器返回错误:(500)内部服务器错误。” 如果我像这样调用相同的网络方法,则为 100%
WebService1 ws = new WebService1();
String output = ws.HelloWorld();
然后一切正常,所以我知道该服务在部署时运行良好。我会非常感谢一些关于我做错了什么的建议=)
这是我的代码:
var _url = "http://localhost/simulator/webservice1.asmx";
var _action = "http://localhost/simulator/webservice1.asmx?op=HelloWorld";
String soapMessage = soapMessage = @"<?xml version='1.0' encoding='utf-8'?><soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'><soap:Body><HelloWorld xmlns='http://tempuri.org/' /></soap:Body></soap:Envelope>";
XmlDocument soapEnvelop = new XmlDocument();
soapEnvelop.LoadXml(soapMessage);
XmlDocument soapEnvelopeXml = soapEnvelop;
HttpWebRequest webRequest = CreateWebRequest(_url, _action);
InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest);
// begin async call to web request.
IAsyncResult asyncResult = webRequest.BeginGetResponse(null, null);
// suspend this thread until call is complete. You might want to
// do something usefull here like update your UI.
asyncResult.AsyncWaitHandle.WaitOne();
// get the response from the completed web request.
string soapResult;
using (WebResponse webResponse = webRequest.EndGetResponse(asyncResult))
using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))
{
soapResult = rd.ReadToEnd();
}