Rob Garrett - Blogs

Welcome to Rob Garrett - Blogs Sign in | Join | Help
in Search
Google

Software/Technology Discussion

Software and Technology Tid-bits

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.

Share this post: Email it! | bookmark it! | digg it! | reddit!
Published Friday, June 29, 2007 3:10 AM by Rob Garrett
Filed under: ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

Pranav said:

NO..... That can't be..... I refuse to believe that.... that is SILLY.....
June 29, 2007 6:29 AM
 

tapicer said:

Cool, I was looking for this for a few hours. Thanks.
December 13, 2007 8:50 AM

Leave a Comment

(required) 
(optional)
(required) 
Submit

Blurb


Head Shot
Rob Garrett is a British Expat living in Maryland USA. Rob is a trained software engineer and experienced in Windows .NET development.

Rob enjoys listening to Rock music, posting to blogs, driving in the country with the sunroof open, beer (not in conjunction with country driving) and spending time with his family.

This Blog

Syndication

Powered by Community Server, by Telligent Systems