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);
                            }
                        }
                      }
                  }
}

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);
}

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

Encoding.UTF8 did not resolve this issue :(

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)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.