views:

133

answers:

3

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\"&gt;";
            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(soapEnvelope);

            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?

+4  A: 

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.

John Saunders
What did you mean by "It's usually better to consume a SOAP service from the server side. It's much easier." ?
Allen
@Allen: until I noticed "`string.Empty`", I thought this was JavaScript.
John Saunders
ahhh that makes sense
Allen
A: 

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.

Dr. Wily's Apprentice
(Deleted my previous comment due to being arguably incorrect) I accede to your point; at this point WCF should be the recommended approach if available. Note that a number of the links on MSDN under VS2010 ASP.NET Web Services (http://msdn.microsoft.com/en-us/library/yzbxwf53.aspx) specifically mention that the "Web Reference" / wsdl.exe functionality is legacy and is provided for backward compatibility.
Dr. Wily's Apprentice
+5  A: 

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.

Kieren Johnstone
Finally, a sane answer
Allen
-1 for suggesting a web reference.
John Saunders
@John - could you elaborate? Edit: surely you don't mean my minor faux pas of not using 'Service Reference' terminology? I will duly update and remain baffled as to why that makes it a bad answer..
Kieren Johnstone
Idk if i'd -1 him for suggesting a 'web reference' vs a 'service reference'. What if he's using an older version of .net or visual studio and that is his only option. At any rate, its far more sane than calling it manually
Allen
John's a WCF guy who is none too pleased when someone wants to use VS2K5-style web references. Can't argue with that myself, but in some cases (SharePoint in particular), the service reference just plain doesn't work when the web reference does.
Jesse C. Slicer
ah, WCF ONLY! RABBLE RABBLE RABBLE! :)
Allen
I think perhaps it was more 'I misread the question. But I can edit and this guy used old terminology, and I'd rather be on top'. The other answer here also refers to 'Web reference' but was spared the wrath.
Kieren Johnstone
If the asker happens to be using Visual Studio 2005, then the "Add Service Reference" option does not exist. All we really know is that the technologies involved are C# and a SOAP web service.
Dr. Wily's Apprentice
@Kieren: "Web Reference" is not minor. For someone who is new to this, they might very well go off on the wrong track and be using legacy technology for no good reason, for years to come. I've seen devs start off by using WSE simply because a WSE example came up in a Google search for "web service security". People actually _follow_ our advice, so we have to be careful with it. I've removed the downvote now that you've corrected the error.
John Saunders
@Jesse: I'm hardly a "WCF Person". I just know what everyone should - don't use ASMX technology for any new development, if you have any choice.
John Saunders
@DrWily: when he says, "I'm using VS2005 and don't see Add Service Reference in the menu", _then_ it would make sense to say, "upgrade", and "use Add Web Reference in the meantime". Remember that these answers are not just read by the OP, but also by all sorts of other developers who will not read the entire question and answers and comments. Besides, I didn't notice your use of "Add Web Reference". Downvoting now.
John Saunders