1

My remote server is throwing a web exception as bad request. But I know there is more information included in the error than what I get. If I look at the details from the exception it does not list the actual content of the response. I can see the content-type, content-length and content-encoding only. If I run this same message through another library (such as restsharp) I will see detailed exception information from the remote server. How can I get more details from the response since I know the remote server is sending them?

static string getXMLString(string xmlContent, string url)
{
        //string Url;
        string sResult;
        //Url = ConfigurationManager.AppSettings["UserURl"] + url;

        var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
        httpWebRequest.ContentType = "application/xml";
        httpWebRequest.Method = "POST";
        using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
        {
            streamWriter.Write(xmlContent);
            streamWriter.Flush();
            streamWriter.Close();
            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                var result = streamReader.ReadToEnd();
                sResult = result;
            }
        }
        return sResult;
}
Bharath theorare
  • 524
  • 7
  • 27

1 Answers1

1

EDIT : Have you tried with a simple try-catch to see if you can get more details ?

try
{
    var response = (HttpWebResponse)(request.GetResponse());
}
catch(Exception ex)
{
    var response = (HttpWebResponse)ex.Response;
}

In my recherches in an answer for you, I noticed that in code there was something about encoding, that you didn't specified. Look here for exemple with such code.

var encoding = ASCIIEncoding.ASCII;
using (var reader = new System.IO.StreamReader(response.GetResponseStream(), encoding))
{
    string responseText = reader.ReadToEnd();
}

Or here, in the doc, also.

// Creates an HttpWebRequest with the specified URL. 
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url); 
// Sends the HttpWebRequest and waits for the response.         
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse(); 
// Gets the stream associated with the response.
Stream receiveStream = myHttpWebResponse.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
// Pipes the stream to a higher level stream reader with the required encoding format. 
StreamReader readStream = new StreamReader( receiveStream, encode );
Console.WriteLine("\r\nResponse stream received.");

Have you tried with such ?

Community
  • 1
  • 1
informaticienzero
  • 1,796
  • 1
  • 12
  • 22
  • The problem I am having is the response is calling it an error on the line where the response is defined in the first place so I do not have an opportunity to capture the stream because the code has already called it an error. (On this line: HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();) – Julius Alexander Mar 15 '16 at 14:15
  • Added proper error checking and then this solution worked. – Julius Alexander Mar 15 '16 at 14:27
  • Have exactly did you do as error checking ? Did you add a try-catch ? – informaticienzero Mar 15 '16 at 14:32
  • An error response will usually cause WebException to be thrown. Not all error responses with message bodies will populate WebException with a response. – Suncat2000 Jul 27 '23 at 19:23