I am making a request to an ASMX web service as follows –
private HttpWebResponse SendSoap12Msg(string url, string method,
Dictionary<string, string> KeyValue)
{
StringBuilder SoapMessage = new StringBuilder();
SoapMessage.Append("<?xml version='1.0' encoding='utf-8'?>");
SoapMessage.Append(@"<soap12:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'");
SoapMessage.Append(@" xmlns:xsd='http://www.w3.org/2001/XMLSchema'>");
SoapMessage.Append(@" xmlns:soap12='http://www.w3.org/2003/05/soap-envelope'");
//SoapMessage.Append(@" xmlns:soap12='http://schemas.xmlsoap.org/wsdl/soap12/'");
SoapMessage.Append("<soap12:Body>");
SoapMessage.Append("<");
SoapMessage.Append(method);
SoapMessage.Append(@" xmlns='http://tempurl.org/'>");
foreach (KeyValuePair<string, string> kvp in KeyValue)
{
SoapMessage.Append("<");
SoapMessage.Append(kvp.Key);
SoapMessage.Append(">");
SoapMessage.Append(kvp.Value);
SoapMessage.Append("</");
SoapMessage.Append(kvp.Key);
SoapMessage.Append(">");
}
SoapMessage.Append("</");
SoapMessage.Append(method);
SoapMessage.Append(">");
SoapMessage.Append("</soap12:Body>");
SoapMessage.Append("</soap12:Envelope>");
// Build HttpWebRequest
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(url));
request.Method = "POST";
request.ProtocolVersion = HttpVersion.Version11;
request.ContentType = "application/soap+xml; charset=\"utf-8\"";
//request.Accept = "application/soap+xml";
// Send SOAP Envelope
byte[] data = Encoding.UTF8.GetBytes(SoapMessage.ToString());
request.ContentLength = data.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(data, 0, data.Length);
requestStream.Close();
return (HttpWebResponse ) request.GetResponse();
}
However, whenever the request is sent, I am getting a 500 - Internal Server Error as a response. Digging deeper into the exception, using these –
catch (WebException ex)
{
Response.ContentType = "text/html";
Response.Write("---------- Start: A WebException occured ----------<br />");
Response.Write("Returned Content Type: " + ex.Response.ContentType);
Response.Write("<br />");
Response.Write("Is From Cache: " + ex.Response.IsFromCache);
Response.Write("<br />");
Response.Write("Response URI: " + ex.Response.ResponseUri.ToString());
Response.Write("<br />");
Response.Write("ToString: " + ex.Response.ToString());
Response.Write("<br />");
Response.Write("ReadToEnd: " +
new StreamReader(ex.Response.GetResponseStream()).ReadToEnd());
Response.Write("<br />");
Response.Write("---------- End: A WebException occured ----------");
}
I get the following output –
---------- Start: A WebException occured ----------
Returned Content Type: application/soap+xml; charset=utf-8
Is From Cache: False
Response URI: (the target uri, as expected)
ToString: System.Net.HttpWebResponse
ReadToEnd: soap:ReceiverServer was unable to process request. ---> 'soap12' is an undeclared prefix. Line 1, position 40.
---------- End: A WebException occured ----------
How can I solve it?
The only thing I see missing from the SOAP request you are building is the
<request>tag after the method name and before the parameters. Omitting it will sometimes produce errors like this, or make the translation of the request to the WS entities fail.Try the following (omitted some things for brevity):
Coincidently I came across this problem with a client of ours, and the missing request-tag was the cause. In some cases you can safely omit it, but sometimes it’s mandatory apparently. I also added the use of
String.Format, which makes the code shorter and easier to understand.Edit:
The only other thing I see missing is you setting the SOAP action in the request header. You can add it in the following manner:
If you google for “C# HttpWebRequest SoapAction” your will find a lot more people getting Error 500; especially when omitting it in the request.
See also, this blog entry which is doing almost exactly the same thing as you are trying: http://mikehadlow.blogspot.nl/2006/05/making-raw-web-service-calls-with.html