Consuming SOAP web service in ASP.NET
I have a SOAP WSDL (found here: https://portal.bsh-partner.com/picenter/server/a2a/) and I am trying to consume the web services.
var soapEnvelope = string.Empty;
soapEnvelope = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
soapEnvelope += "<soapenv:Envelope xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:q0=\"http://thexmlhost.com\">";
soapEnvelope += "<q0:Connect>";
soapEnvelope += "<q0:Username>username</q0:Username>";
soapEnvelope += "<q0:Password>password</q0:Password>";
soapEnvelope += "</q0:Connect>";
soapEnvelope += "</soapenv:Body>";
soapEnvelope += "</soapenv:Envelope>";
var xmlHttp = new MSXML2.ServerXMLHTTP40();
xmlHttp.open("POST", "https://thexmlhost.com/", "", "");
xmlHttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
xmlHttp.setRequestHeader("SOAPAction", "thexmlhost.com/");
xmlHttp.send(soapEnvel开发者_开发技巧ope);
xmlHttp.waitForResponse(500);
var outXml = xmlHttp.responseText;
The response keeps returning a general HTTP response error page. Any idea how I should be passing my Soap Envelope to get the proper response back from the web service?
If you have WSDL, you can create a Service Reference in Visual Studio and it will generate a proxy class for you. Unless I am missing some fine point in your question, it would be a lot more reliable, and much easier, to implement and use.
Hope that helps.
I just noticed that you are using this code on the server side.
This is not the way to consume web services in .NET. Use the "Add Service Reference" command in Visual Studio to create proxy classes that will allow you to very easily consume the service.
See How to Consume a Web Service for an example, as well as http://msdn.microsoft.com/wcf/.
Also, never use string manipulation techniques when working with XML. Briefly, the rules for strings and for XML are different. .NET has several XML APIs to work with XML, and they all know the rules.
Just glancing at your code, it looks like you forgot to add your body start tag: <soapenv:Body>
Unless you have a particular reason to be constructing your message manually, I would highly recommend using a proxy generated using the svcutil.exe tool that comes with visual studio 2008 or later. (this tool is used automatically by Visual Studio if you use the "Add Service Reference" feature), assuming that you don't have a problem with using WCF to handle communication with your web service.
Alternatively, if for some reason you cannot use WCF, such as if you happen to be developing in .Net 2.0 with Visual Studio 2005 in which case you wouldn't have the "Add Service Reference" feature available, then could still use Visual Studio's older "Add Web Reference" feature to generate a proxy for your web service. This feature uses the wsdl.exe tool rather than svcutil.exe.
Otherwise, if you really must continue with this approach of constructing the SOAP message manually, then I'd at least recommend adding some logging to your application so that you can more easily analyze the constructed message when something goes wrong.
精彩评论