MD Sayem Ahmed
MD Sayem Ahmed

Reputation: 29176

error while making request to an asmx web service

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?

Upvotes: 0

Views: 1897

Answers (2)

pyrocumulus
pyrocumulus

Reputation: 9290

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):

SoapMessage.Append(String.Format(@"<{0} xmlns='http://tempurl.org/'>", method));
SoapMessage.Append("<request>");    // Line added

foreach (KeyValuePair<string, string> kvp in KeyValue)
{
    SoapMessage.Append(String.Format("<{0}>", kvp.Key));
    SoapMessage.Append(kvp.Value);
    SoapMessage.Append(String.Format("</{0}>", kvp.Key));
}

SoapMessage.Append("</request>");   // Line added
SoapMessage.Append(String.Format("</{0}>", method));

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:

request.Headers.Add("SOAPAction:", "http://tempurl.org/YourMethodname"); 
// You might need to try some variations. 
// Or replacing tempurl.org with the actual domain.

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

Upvotes: 1

chead23
chead23

Reputation: 1879

Make sure the bindings for SOAP1.2 have been added to the web.config and they are configured for that service and enabled.

<wsdl:binding name="ServiceSoap12" type="tns:ServiceSoap">

and then

<configuration>
  <system.web>
    <webServices>
      <protocols>
        <add name="HttpSoap12"/>
      </protocols>
    </webServices>
  </system.web>
</configuration>

Upvotes: 1

Related Questions