0

hello,
i have checked online on a website which gives error BadRequest(status code=400) and gives response like :

'"message": "messages": [ "We are sorry, the item you selected, cannot be done" ] , "resultInfo": "result": "fail", "url": "http://www.abc.com/xyz/pqr/home", "resultCode": 400'

but when i am doing the same thing at my code, i am receiving the same error BadRequest(status code=400) which is OK but i am receiving below response which is wrong:

En0 D|Y]SYtAETBe1ilB@uCdtJ%Tr*,r;#$xEo B(g]άt3?C7;G1k>=ʪ6'+%(m;MS xZAVԥ@~mO]7on;2

How to Resolve this?
thanks,

try
{
//something
}
catch(WebException webException)
{
                string strResponse = string.Empty;
                Encoding Encding;

                using (HttpWebResponse response = (HttpWebResponse)webException.Response)
                {
                    if (response != null)
                    {
                        MemoryStream s = new MemoryStream();

                        using (var errorResponse = (HttpWebResponse)response)
                        { 
                            using (var reader = new StreamReader(errorResponse.GetResponseStream()))
                            {
                                string error = reader.ReadToEnd();
                                Console.WriteLine(error);
                            }
                        }
                      }
                  }
}

Edited by deceptikon: Changed to a discussion thread

3
Contributors
4
Replies
6
Views
5 Years
Discussion Span
Last Post by JOSheaIV
0

Looks like an encoding issue. Do you know what encoding the HTTP response is in? Something like this will read the response in UTF8 encoding:

using (var reader = new StreamReader(errorResponse.GetResponseStream(), Encoding.UTF8))
{
  string error = reader.ReadToEnd();
  Console.WriteLine(error);
}
0

2nd the Encoding. Speaking from past experience (I built a code used to read in webpage sources), I had to read in funky characters and other stuff, and UTF8 seemed to clear it up.

BTW The way to check (at least I think so, this is what I just did) is to view the page source and there should be a line like "charset=utf-8"

Of course this is somewhat I guess, but it should work

0

When trying that Encoding.UTF8 option. Try some of the other options under Encoding (you know in Visual Studio when you type the dot a list of possibilities appear in a list, try some of those).

See if maybe some of those other ones work ... probably not what you were expecting but sometimes trial and error works for this (careful not to try just one and think it works, test multiple ... I have made that mistake before)

Edited by JOSheaIV

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.