locked
HttpWebRequest capture response even if 500 Internal Server Error RRS feed

  • Question

  • Hi,

    Hoping someone can point me to a solution, haven't been able to find one yet.

    I'm using the standard way for send HttpWebRequest

            webRequest = (HttpWebRequest)WebRequest.Create(request.URL);
                    webRequest.Method = "POST";
                    webRequest.ContentType = "text/xml";

    ...

            postStream = webRequest.GetRequestStream();
                    postStream.Write(requestBuffer, 0, requestBuffer.Length);
                    postStream.Close();
                    webResponse = (HttpWebResponse)webRequest.GetResponse();
                    responseStream = webResponse.GetResponseStream();
                    responseReader = new StreamReader(responseStream);
                    string response = responseReader.ReadToEnd();


    This is all fine, working correctly, no poblems.

    However the actual webservice that i'm calling returns a 500 Internal Server Error code for standard faults

    HTTP/1.1 500 Internal Server Error
    Server:
    Content-Type: text/xml; charset=utf-8
    Content-Length: 703
    Date: Fri, 22 Oct 2010 04:50:48 GMT

    <?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sf="urn:fault.enterprise.soap.sforce.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><soapenv:Fault><faultcode>sf:INVALID_SESSION_ID</faultcode><faultstring>INVALID_SESSION_ID: Invalid Session ID found in SessionHeader: Illegal Session</faultstring><detail><sf:UnexpectedErrorFault xsi:type="sf:UnexpectedErrorFault"><sf:exceptionCode>INVALID_SESSION_ID</sf:exceptionCode><sf:exceptionMessage>Invalid Session ID found in SessionHeader: Illegal Session</sf:exceptionMessage></sf:UnexpectedErrorFault></detail></soapenv:Fault></soapenv:Body></soapenv:Envelope>


    As you can see from Fiddler/SOAPUI it does return the SOAP fault, however using HttpWebRequest i can't capture the response.

    Returning 500 triggers my exception handling and Capturing the WebException doesn't expose the response.

    Does anyone know how to capture this response xml in the case of 500 Internal Server Errors.

    Thanks,

    Simon
    Friday, October 22, 2010 5:07 AM

Answers

  • Actually never mind - i've found the solution

     

    in the catch block you can get the error response also

     

    catch (WebException ex)
                {

    string exMessage = ex.Message;

                    if (ex.Response != null)
                    {
                        using (responseReader = new StreamReader(ex.Response.GetResponseStream()))
                        {
                            exMessage = responseReader.ReadToEnd();
                        }
                    }

    Hope that helps someone in the future

    • Marked as answer by simmohall Friday, October 22, 2010 5:29 AM
    Friday, October 22, 2010 5:29 AM

All replies

  • Actually never mind - i've found the solution

     

    in the catch block you can get the error response also

     

    catch (WebException ex)
                {

    string exMessage = ex.Message;

                    if (ex.Response != null)
                    {
                        using (responseReader = new StreamReader(ex.Response.GetResponseStream()))
                        {
                            exMessage = responseReader.ReadToEnd();
                        }
                    }

    Hope that helps someone in the future

    • Marked as answer by simmohall Friday, October 22, 2010 5:29 AM
    Friday, October 22, 2010 5:29 AM
  • Why don't you simply use "Add Wervice Reference" to consume the service? Why do you  have to use WebRequest?
    John Saunders
    WCF is Web Services. They are not two separate things.
    Use WCF for All New Web Service Development, instead of legacy ASMX or obsolete WSE
    Use File->New Project to create Web Service Projects
    Friday, October 22, 2010 8:05 PM
    Moderator