简体   繁体   中英

HttpWebResponse text does not appear to be JSON

I am making a rest call in which I get a HttpWebResponse that contains data. It seems the data is serialized, and I am trying to get the plain text of the request. I have been using the chrome extension Advanced Rest client, which when calling the same request it is able to display the text version of the json response.

From what I have read on here, you are required to deserialize into the expected object. However, it is pretty clear that chrome plugin has no idea about the object type and can still print out plain text.

Is it possible to do the same in c#?

HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;

request.Method = "POST";
request.ContentType = "application/json";

// [code removed for setting json data via stream writer


using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{

   // This is where I am trying to figure out how to get plain readable text out of response.GetResponseStream()

}

Edit: If I simply use a StreamReader to get the text from the response stream, I get a bunch of binary data and not the plain json text.

Edit: realized the problem had to do with compression. This can be closed.

I'm not sure if got it right, but you can get the response as a string doing this:

using (var sr = new StreamReader(response.GetResponseStream()))
{
    text = sr.ReadToEnd();
}

Turned out my problem was due to compression. I realized the header contained "Content-Encoding: gzip" so I searched on how to unzip with gzip compression and then the text was proper json. Thanks all

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM