stuck with WebRequest
-
I'm running the following code:
StringBuilder sb = new StringBuilder(); byte\[\] buf = new byte\[131072\]; string url = null; url = "http://google.com"; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); if (request.HaveResponse) { Stream resStream = response.GetResponseStream(); string tempString = null; int count = 0; do { count = resStream.Read(buf, 0, buf.Length); if (count != 0) { tempString = Encoding.ASCII.GetString(buf, 0, count); sb.Append(tempString); } } while (count > 0); } richTextBox1.Text = sb.ToString();
and after code executed richTextBox1.Text contains the same code as I can see if I open in browser url and click on "view source". But not always. There are some urls, that I can see server's replay in browser, and see html code in "view source", but richTextBox1.Text contains junk. May be there is some problem with page encoding or something? Thanks.
-
I'm running the following code:
StringBuilder sb = new StringBuilder(); byte\[\] buf = new byte\[131072\]; string url = null; url = "http://google.com"; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); if (request.HaveResponse) { Stream resStream = response.GetResponseStream(); string tempString = null; int count = 0; do { count = resStream.Read(buf, 0, buf.Length); if (count != 0) { tempString = Encoding.ASCII.GetString(buf, 0, count); sb.Append(tempString); } } while (count > 0); } richTextBox1.Text = sb.ToString();
and after code executed richTextBox1.Text contains the same code as I can see if I open in browser url and click on "view source". But not always. There are some urls, that I can see server's replay in browser, and see html code in "view source", but richTextBox1.Text contains junk. May be there is some problem with page encoding or something? Thanks.
-
That is what: Took the code from msdn page, compiled and here is what we have for the first itteration of the while loop: in 'url', type string, variable: http://capitalcity.combats.com/inf.pl?1183463649 in 'str' variable: "�\b\0\0\0\0\0\0��[[s�F�~^���6$#�ąw��*�y7N\\�3����\t8\0(Y������/�h;���Q��le� J�!��y�JkO7�;eɒ�Q�2��>���9}Nw;�x��y_vqa�\n�������;����:F�L��V��*�9u;�����\\@�*^��Wu���F\vBE�� �[��R^t���\"�Y�˒�I���T,�4}K��*�:ҷ*8���m=Z�4�(Y�\\%-\v�!�\\�*(��e�#A8͡ª�jX�m{" But when I do the same on http://google.com - everything is fine! str variable contains readable html code. Changing encoding to "windows-1251" instead of "utf-8" didn't help also. Any ideas what's going wrong? Thanks!
-
That is what: Took the code from msdn page, compiled and here is what we have for the first itteration of the while loop: in 'url', type string, variable: http://capitalcity.combats.com/inf.pl?1183463649 in 'str' variable: "�\b\0\0\0\0\0\0��[[s�F�~^���6$#�ąw��*�y7N\\�3����\t8\0(Y������/�h;���Q��le� J�!��y�JkO7�;eɒ�Q�2��>���9}Nw;�x��y_vqa�\n�������;����:F�L��V��*�9u;�����\\@�*^��Wu���F\vBE�� �[��R^t���\"�Y�˒�I���T,�4}K��*�:ҷ*8���m=Z�4�(Y�\\%-\v�!�\\�*(��e�#A8͡ª�jX�m{" But when I do the same on http://google.com - everything is fine! str variable contains readable html code. Changing encoding to "windows-1251" instead of "utf-8" didn't help also. Any ideas what's going wrong? Thanks!
-
That is what: Took the code from msdn page, compiled and here is what we have for the first itteration of the while loop: in 'url', type string, variable: http://capitalcity.combats.com/inf.pl?1183463649 in 'str' variable: "�\b\0\0\0\0\0\0��[[s�F�~^���6$#�ąw��*�y7N\\�3����\t8\0(Y������/�h;���Q��le� J�!��y�JkO7�;eɒ�Q�2��>���9}Nw;�x��y_vqa�\n�������;����:F�L��V��*�9u;�����\\@�*^��Wu���F\vBE�� �[��R^t���\"�Y�˒�I���T,�4}K��*�:ҷ*8���m=Z�4�(Y�\\%-\v�!�\\�*(��e�#A8͡ª�jX�m{" But when I do the same on http://google.com - everything is fine! str variable contains readable html code. Changing encoding to "windows-1251" instead of "utf-8" didn't help also. Any ideas what's going wrong? Thanks!
The content returned from http://capitalcity.combats.com/inf.pl?1183463649 is compressed (gzip). You may want to check the HttpWebResponse.ContentEncoding property before assuming the HTTP content is text :) Here's an example (first one I found Googling): HttpWebRequest and GZip Http Responses[^] Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
The content returned from http://capitalcity.combats.com/inf.pl?1183463649 is compressed (gzip). You may want to check the HttpWebResponse.ContentEncoding property before assuming the HTTP content is text :) Here's an example (first one I found Googling): HttpWebRequest and GZip Http Responses[^] Mark
Mark Salsbery Microsoft MVP - Visual C++ :java: