Invocation of SOAP Web Service gives 500 Internal Server Error

Picture this situation:  It’s now 3am, start of business is deadline day for development of your project, and you’ve been working for the last 7 hours on a web service client bug; where dynamic invocation of the service, via SOAP 1.2, causes the server to return an HTTP 500 Internal Server error.  The air conditioning kicked off at 7pm and now everything you touch is sticky from sweat and frustration.  This was me.

After staring at the code responsible for creating my SOAP envelope and the HTTP post code, and trawling through Google, I finally found a solution.  Apparently, the raw server response is buried in the System.Net.WebException, thrown by .NET, and with some code added to the exception catch block it is possible to find out what really went wrong.

Here’s the code:

   1: private static string PostSoap(string url, string soapEnv)
   2: {
   3:     try
   4:     {
   5:         string result;
   6:         byte[] postData = Encoding.UTF8.GetBytes(soapEnv);
   7:         HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
   8:         if (null == request)
   9:             throw new WebException("Failed to create WebRequest object");
  10:         request.Method = "POST";
  11:         request.ContentType = "application/soap+xml; charset=utf-8";
  12:         request.ContentLength = postData.Length;
  13:         request.UserAgent = "DynamicFormWebPart";
  14:         using (Stream stream = request.GetRequestStream())
  15:         {
  16:             stream.Write(postData, 0, postData.Length);
  17:             stream.Flush();
  18:             stream.Close();
  19:         }
  20:         HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  21:         if (null == response)
  22:             throw new WebException("Failed to get a WebResponse object");
  23:         using (StreamReader reader = new StreamReader(response.GetResponseStream()))
  24:             result = reader.ReadToEnd();
  25:         return result;
  26:     }
  27:     catch (WebException ex)
  28:     {
  29:         HttpWebResponse response = ex.Response as HttpWebResponse;
  30:         if (null == response)
  31:             throw new ArgumentNullException("Response was null");
  32:         using (StreamReader reader = new StreamReader(response.GetResponseStream()))
  33:         {
  34:             string error = reader.ReadToEnd();
  35:             throw new PSWebPartException(error, ex);
  36:         }
  37:     }
  38: }

 

By the way, the error causing me mental turmoil this evening was because I passed ‘True’ to a boolean parameter, instead of lowercase ‘true’.  What threw me, was the .NET HTTP POST test page, which allows uppercase T and F in boolean values.  I guess I loose such niceties when creating my own lower level code.

7 hours for an upper case ‘T’ – geez, woot.

4 thoughts on “Invocation of SOAP Web Service gives 500 Internal Server Error

Comments are closed.