1

I need to get the 500 error response message from HttpWebResponse.

HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
using (StreamReader responseStream = new StreamReader(objResponse.GetResponseStream()))
{
      post_response = responseStream.ReadToEnd();
      responseStream.Close();
}

enter image description here

Thank you!

[EDIT]

open this url using browser,

https://www.sagepayments.net/web_services/vterm_extensions/transaction_processing.asmx/BANKCARD_PRIOR_AUTH_SALE

The browser will print response message, 'Missing parameter: M_ID."

Now, I want to get that response message using asp.net

var post_string = "hello=hi";

HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create("https://www.sagepayments.net/web_services/vterm_extensions/transaction_processing.asmx/BANKCARD_PRIOR_AUTH_SALE");
    objRequest.Method = "POST";
    objRequest.ContentLength = post_string.Length;
    objRequest.ContentType = "application/x-www-form-urlencoded";

    // post data is sent as a stream
    StreamWriter myWriter = null;
    myWriter = new StreamWriter(objRequest.GetRequestStream());
    myWriter.Write(post_string);
    myWriter.Close();

    // returned values are returned as a stream, then read into a string

    try
    {
    HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
    using (StreamReader responseStream = new StreamReader(objResponse.GetResponseStream()))
    {
        post_response = responseStream.ReadToEnd();
        responseStream.Close();
    }

    Response.Write(post_response);
    }
    catch(WebException e)
    {   
    Response.Write("Error : "+e.Message); 
    Response.Write("<br /> Data : " + e.Data);
    Response.Write("<br /> HelpLink : " + e.HelpLink);
    Response.Write("<br /> InnerException : " + e.InnerException);
    Response.Write("<br /> Response : " + e.Response);
    Response.Write("<br /> Source : " + e.Source);
    Response.Write("<br /> Status : " + e.Status);
    Response.Write("<br /> TargetSite : " + e.TargetSite);
    }


Result : 
Error : The remote server returned an error: (500) Internal Server Error.
Data : System.Collections.ListDictionaryInternal
HelpLink :
InnerException :
Response : System.Net.HttpWebResponse
Source : System
Status : ProtocolError
TargetSite : System.Net.WebResponse GetResponse()

How to get this message ?

Missing parameter: M_ID.

Expert wanna be
  • 10,218
  • 26
  • 105
  • 158

3 Answers3

4

I think you need to check the StatusCode & StatusDescription properties of the web response if an exception is thrown during the web request.

For example:

try
{
    HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
    using (StreamReader responseStream = new StreamReader(objResponse.GetResponseStream()))
    {
        post_response = responseStream.ReadToEnd();
        responseStream.Close();
    }
}
catch(WebException wex)
{
    // This is the line that gets you the response object
    HttpWebResponse response = (HttpWebResponse)wex.Response;

    if(response != null)
    {
        // You can now read the response StatusCode and StatusDescription
        HttpStatusCode responseCode = response.StatusCode;
        String statusDescription = response.StatusDescription;

        // Add your status checking logic here
    }
}
KazR
  • 961
  • 7
  • 16
  • Thank you for your answer, but still I couldn't solve my problem. Could you review my edited question again please? – Expert wanna be Jun 19 '12 at 18:05
  • @Expertwannabe - if you look at my example, you'll see that I've cast the Response property of the thrown exception to a HttpWebResponse. This give you access to the StatusCode and StatusDescription properties that should give you what you want. This is the bit you're missing in your updated question example. (I've highlighted the relevant lines in my answer) – KazR Jun 19 '12 at 21:23
0

HTTP Errors are handled at the IIS level unless you have custom errors turned on. Check out this post on how to change your web config to add your own custom error pages: Returning 404 Error ASP.NET MVC 3

This question: How to send a Status Code 500 in ASP.Net and still write to the response?

...also features specifically how a 500 might be thrown and again how to do customer errors. It also includes a bit on Context.Response.TrySkipIisCustomErrors which is the accepted answer.

Community
  • 1
  • 1
Chris Barlow
  • 3,274
  • 4
  • 31
  • 52
  • I added the HttpContext.Reponse.TrySkipIisCustomErrors into top of the method, but still I couldn't get the response message. Could you review my edited question again please? – Expert wanna be Jun 19 '12 at 18:06
  • And did you try the code in this error in conjunction with the TrySkipIisCustomErrors property? http://stackoverflow.com/a/4496012/590783 – Chris Barlow Jun 19 '12 at 18:14
0

That's work in my situation. Hope it wold help you.

try
{
    response = (HttpWebResponse)request.GetResponse();
    using (StreamReader streamReader = new StreamReader(response.GetResponseStream()))
    {
        responseMessage = streamReader.ReadToEnd();
        Assert.IsTrue(response.StatusCode == HttpStatusCode.OK);
        response.Close();
    }
}
catch(WebException ex)
{
    using (StreamReader streamReader = new StreamReader(ex.Response.GetResponseStream()))
    {
        responseMessage = streamReader.ReadToEnd();
        File.WriteAllText(@"C:\Users\Paul\Documents\text.txt", responseMessage);
    }
}