1

I am trying to post XML to a web service directly rather than using VS's "add a web reference..." functionality. There is a good reason for this. To get proper test XML I logged what was generated using the standard web reference. I used what I found in this question Capturing SOAP requests to an ASP.NET ASMX web service

The web service was just what is generated when you create a new web service in visual studio, nothing fancy.

To try and pass the XML itself, I borrowed the code from this question Client to send SOAP request and received response

Unfortunately, when I call it, I get "The remote server returned an error: (500) Internal Server Error." 100% if I call the same web method like this

WebService1 ws = new WebService1();
String output = ws.HelloWorld();

then everything works great so I know the service is working well as deployed. I would appreciate so greatly some advice as to what I am doing wrong =)

Here is my code:

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();
}
Community
  • 1
  • 1
Ready Cent
  • 1,821
  • 3
  • 18
  • 25
  • 1
    I would eliminate .NET from the equation and try posting your XML to the web service using [soapUI](http://www.soapui.org/). With soapUI, you can view the request/response side by side which is handy for debugging. – Derek Hunziker Aug 28 '12 at 18:47
  • Also, note that 500 is a server-level error. The server is probably throwing an exception. Look in the event log to see what it was. – John Saunders Aug 28 '12 at 18:49
  • I'm not a Soap expert by any means, but don't you already have a soap envelope defined in your soapMessage? What does soapEnvelop look like after you do a LoadXml(soapMessage). – Mike Aug 28 '12 at 19:13
  • 1
    Found something that might be useful http://p2p.wrox.com/asp-net-1-0-1-1-professional/57098-c-post-xml-using-httpwebrequest-response.html – Mike Aug 28 '12 at 19:29
  • Oddly enough, copying what this guy did worked fine as well http://bytes.com/topic/net/answers/427794-send-soap-xml-webservice-url – Ready Cent Aug 28 '12 at 19:57

0 Answers0