Pages

Wednesday, June 19, 2019

Read the Response message when a web request throws “500 Internal Server Error”

Introduction

When I call a SOAP request from Postman I got the response as “500 Internal Server Error”. I had to call the same api from C# and read the response and had to pass it from api to front end.

Here is the screenshot of the response.




Solution



   
                string soapResult = string.Empty;
                try
                {
                    using (WebResponse webResponse = webRequest.EndGetResponse(asyncResult))
                    {
                        using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))
                        {
                            soapResult = rd.ReadToEnd();
                        }
                    }
                }
                catch (WebException wex)
                {
                    soapResult = new StreamReader(wex.Response.GetResponseStream())
                                          .ReadToEnd();
                }
                return soapResult;