Garbled httpWebResponse string when posting data to web form programmatically

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???^<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="58676718">[email protected]</a>?Y??"?k??,?a?H?Lp?A?$ ;<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="380707077b78">[email protected]</a>????e6'<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1a2554252525562d25705a">[email protected]</a>???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.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x