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. C#
  4. WebResponse/WebRequest problem

WebResponse/WebRequest problem

Scheduled Pinned Locked Moved C#
questionhtmlhelptutorial
8 Posts 3 Posters 14 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.
  • K Offline
    K Offline
    Keith Iveson
    wrote on last edited by
    #1

    The following code loads the contents of a web page (Page) into string PageText: HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(**Page**, true)); request.Method = "GET"; HttpWebResponse response = (HttpWebResponse) request.GetResponse(); Stream responseStream = response.GetResponseStream(); StreamReader reader = new StreamReader(responseStream); string **PageText** = reader.ReadToEnd(); The problem I have is that the page I receive comes back with encoding that doesn't show up correctly in a text box. For example a single quote ’ comes back as & # 8 2 1 7 ; (without the spaces). I think that this would be correctly displayed in a HTML viewer, but how do I get it to display correctly in a text box? Putting an encoding in the constructor for the StreamReader seems to make no difference. Thanks in advance, Keith :)

    C J 2 Replies Last reply
    0
    • K Keith Iveson

      The following code loads the contents of a web page (Page) into string PageText: HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(**Page**, true)); request.Method = "GET"; HttpWebResponse response = (HttpWebResponse) request.GetResponse(); Stream responseStream = response.GetResponseStream(); StreamReader reader = new StreamReader(responseStream); string **PageText** = reader.ReadToEnd(); The problem I have is that the page I receive comes back with encoding that doesn't show up correctly in a text box. For example a single quote ’ comes back as & # 8 2 1 7 ; (without the spaces). I think that this would be correctly displayed in a HTML viewer, but how do I get it to display correctly in a text box? Putting an encoding in the constructor for the StreamReader seems to make no difference. Thanks in advance, Keith :)

      C Offline
      C Offline
      Chad Young
      wrote on last edited by
      #2

      Would the System.Web.HttpUtility.HtmlDecode() method work?

      K 1 Reply Last reply
      0
      • K Keith Iveson

        The following code loads the contents of a web page (Page) into string PageText: HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(**Page**, true)); request.Method = "GET"; HttpWebResponse response = (HttpWebResponse) request.GetResponse(); Stream responseStream = response.GetResponseStream(); StreamReader reader = new StreamReader(responseStream); string **PageText** = reader.ReadToEnd(); The problem I have is that the page I receive comes back with encoding that doesn't show up correctly in a text box. For example a single quote ’ comes back as & # 8 2 1 7 ; (without the spaces). I think that this would be correctly displayed in a HTML viewer, but how do I get it to display correctly in a text box? Putting an encoding in the constructor for the StreamReader seems to make no difference. Thanks in advance, Keith :)

        J Offline
        J Offline
        John Mautari
        wrote on last edited by
        #3

        Did you tried System.Net.WebClient? using System.Text; using System.Net; ... WebClient request = new WebClient(); string PageText = Encoding.Default.GetString(request.DownloadData(Page)); // You can change Encoding.Default to Encoding.UTF8 if needed Cheers, John

        K 1 Reply Last reply
        0
        • C Chad Young

          Would the System.Web.HttpUtility.HtmlDecode() method work?

          K Offline
          K Offline
          Keith Iveson
          wrote on last edited by
          #4

          This is a C# application and not ASP.NET code or a WebService. Therefore I don't have access to the System.Web namespace. Sorry, I should have mentioned this first time. Thanks for the response. Keith

          1 Reply Last reply
          0
          • J John Mautari

            Did you tried System.Net.WebClient? using System.Text; using System.Net; ... WebClient request = new WebClient(); string PageText = Encoding.Default.GetString(request.DownloadData(Page)); // You can change Encoding.Default to Encoding.UTF8 if needed Cheers, John

            K Offline
            K Offline
            Keith Iveson
            wrote on last edited by
            #5

            I've tried this but the result is exactly the same regardless of the encoding.

            J 1 Reply Last reply
            0
            • K Keith Iveson

              I've tried this but the result is exactly the same regardless of the encoding.

              J Offline
              J Offline
              John Mautari
              wrote on last edited by
              #6

              What happens if you use: ... string pagetext = Encoding.ASCII.GetString(request.DownloadData(Page)); ... Sorry if you already have tried it... :rolleyes:

              K 1 Reply Last reply
              0
              • J John Mautari

                What happens if you use: ... string pagetext = Encoding.ASCII.GetString(request.DownloadData(Page)); ... Sorry if you already have tried it... :rolleyes:

                K Offline
                K Offline
                Keith Iveson
                wrote on last edited by
                #7

                I tried Encoding.<everything I could think of>.GetString all to no avail. :( Actually .ASCII makes things slightly worse!

                J 1 Reply Last reply
                0
                • K Keith Iveson

                  I tried Encoding.<everything I could think of>.GetString all to no avail. :( Actually .ASCII makes things slightly worse!

                  J Offline
                  J Offline
                  John Mautari
                  wrote on last edited by
                  #8

                  It seems that you're getting an unicode encoded page from the web server. Did you tried Encoding.Convert? using System.Text; ... string pagetext = Encoding.Default.GetString(Encoding.Convert(Encoding.Unicode, Encoding.ASCII, request.DownloadData(Page))); ... What language is your web page/web server? You may try to change Encoding.Unicode to other encoding like UTF8, and Encoding.ASCII to UTF8 if you're still getting wrong results. I'm using the default encoding to read web pages in english/portuguese, and the results are ok for me. Cheers, John

                  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