Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. stuck with WebRequest

stuck with WebRequest

Scheduled Pinned Locked Moved .NET (Core and Framework)
helphtmlcomsysadminquestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Evgeni57
    wrote on last edited by
    #1

    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.

    M 1 Reply Last reply
    0
    • E Evgeni57

      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.

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      What happens if you code your response stream handling as shown in the example here[^] ? Mark

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      E 1 Reply Last reply
      0
      • M Mark Salsbery

        What happens if you code your response stream handling as shown in the example here[^] ? Mark

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        E Offline
        E Offline
        Evgeni57
        wrote on last edited by
        #3

        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!

        L M 2 Replies Last reply
        0
        • E Evgeni57

          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!

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Font I guess? Maybe because it's a perl script?

          1 Reply Last reply
          0
          • E Evgeni57

            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!

            M Offline
            M Offline
            Mark Salsbery
            wrote on last edited by
            #5

            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:

            E 1 Reply Last reply
            0
            • M Mark Salsbery

              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:

              E Offline
              E Offline
              Evgeni57
              wrote on last edited by
              #6

              Thanks! That's exectly what I needed!

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups