Garbled httpWebResponse string when posting data t

2019-03-03 04:39发布

问题:

I tried to search previous discussion about this issue but I didn't find one, maybe it's because I didn't use right keywords.

I am writing a small program which posts data to a webpage and gets the response. The site I'm posting data to does not provide an API. After some Googling I came up to the use of HttpWebRequest and HttpWebResponse. The code looks like this:

HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create("https://www.site.com/index.aspx");

CookieContainer cookie = new CookieContainer();

httpRequest.CookieContainer = cookie;

String sRequest = "SomeDataHere";

httpRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";

httpRequest.Headers.Add("Accept-Encoding: gzip, deflate");

httpRequest.Headers.Add("Accept-Language: en-us,en;q=0.5");

httpRequest.Headers.Add("Cookie: SomecookieHere");

httpRequest.Host = "www.site.com";
httpRequest.Referer = "https://www.site.com/";
httpRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:14.0) Gecko/20100101 Firefox/14.0.1";
httpRequest.ContentType = "application/x-www-form-urlencoded";
//httpRequest.Connection = "keep-alive";

httpRequest.ContentLength = sRequest.Length;

byte[] bytedata = Encoding.UTF8.GetBytes(sRequest);
httpRequest.ContentLength = bytedata.Length;
httpRequest.Method = "POST";

Stream requestStream = httpRequest.GetRequestStream();
requestStream.Write(bytedata, 0, bytedata.Length);
requestStream.Flush();
requestStream.Close();


HttpWebResponse httpWebResponse = (HttpWebResponse)httpRequest.GetResponse();

string sResponse;
using (Stream stream = httpWebResponse.GetResponseStream())
{
    StreamReader reader = new StreamReader(stream, System.Text.Encoding.GetEncoding("iso-8859-1"));
    sResponse = reader.ReadToEnd();
}

return sResponse;

I used firefox's firebug to get the header and data to post.

My question is, when I store and display the response using a string, all I got are garbled characters, like:

?????*??????xV?J-4Si1?]R?r)f?|??;????2+g???6?N-?????7??? ?6?? x???q v ??? j?Ro??_*?e*??tZN^? 4s?????? ??Pwc??3???|??_????_??9???^??@?Y??"?k??,?a?H?Lp?A?$ ;???C@????e6'?N???L7?j@???ph??y=?I??=(e?V?6C??

By reading the response header using FireBug I got the content type of response:

Content-Type    text/html; charset=ISO-8859-1

And it is reflected in my code. I have even tried other encoding such as utf-8 and ascii, still no luck. Maybe I am in the wrong direction. Please advise. A small code snippet will be even better. Thanks you.

回答1:

You're telling the server that you can accept compressed responses with httpRequest.Headers.Add("Accept-Encoding: gzip, deflate");. Try removing that line, and you should get a clear-text response.

HttpWebRequest does have built in support for gzip and deflate if you want to allow compressed responses. Remove the Accept-Encoding header line, and replace it with

httpRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate

This will take care of adding the appropriate Accept-Encoding header for you, and handle decompressing the content automatically when you receive it.