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. Getting HTML Source from a WebPage in String-format, using NetworkCredentials [modified]

Getting HTML Source from a WebPage in String-format, using NetworkCredentials [modified]

Scheduled Pinned Locked Moved C#
databasecsharphtmlsecurityquestion
2 Posts 1 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.
  • D Offline
    D Offline
    Dimitri Backaert
    wrote on last edited by
    #1

    Hi, In order to create a dashboard application, I need to read an HTML site's source code. This contains the values (integers) which I want to process. I don't have access to the SQL-database, so this is the only way I can achieve my goal, and deliver the application. The site uses a Username & Password as security, and I am able to access the login page. However, when passing the credentials, I'm not able to read the source code (it seems like the credentials aren't valid). However, when using the same credentials directly in Internet Explorer, I'm able to access the webpage. This is the code I have so far. Any of you experts see what I'm doing wrong? PS: I've changed the site-url, username & password, since it's a confidential site of my company.

        public static string GetHtmlPageSource(string url, string username, string password)
        {
            System.IO.Stream st = null;
            System.IO.StreamReader sr = null;
            try
            {
                // make a Web request
                System.Net.WebRequest req = System.Net.WebRequest.Create(url);
                // if the username/password are specified, use these credentials
                if (username != null && password != null)
                    req.Credentials = new System.Net.NetworkCredential(username, password);
                // get the response and read from the result stream
                System.Net.WebResponse resp = req.GetResponse();
                st = resp.GetResponseStream();
                sr = new System.IO.StreamReader(st);
                // read all the text in it
                return sr.ReadToEnd();
            }
            catch (Exception ex)
            {
                return string.Empty;
            }
            finally
            {
                // always close readers and streams
                sr.Close();
                st.Close();
            }
        }
    
        private void DataRefresh\_Tick(object sender, EventArgs e)
        {
            // Set the Refresh Interval to 15000 ms
            this.DataRefresh.Interval = 15000;
    
            // Declare the Variables, needed for this Application
            string FL, SL, UnFL, UnSL, ValueFL, ValueSL, ValueUnFL, ValueUnSL;
    
            //// Refresh the TopDesk Web Browser Object
            this.TopDesk.Refresh();
    
            //Read WebPage
            string page;
    
            page = GetHtmlPageSource("https://site", "user", "pass");
    
    
    
            //
    
    D 1 Reply Last reply
    0
    • D Dimitri Backaert

      Hi, In order to create a dashboard application, I need to read an HTML site's source code. This contains the values (integers) which I want to process. I don't have access to the SQL-database, so this is the only way I can achieve my goal, and deliver the application. The site uses a Username & Password as security, and I am able to access the login page. However, when passing the credentials, I'm not able to read the source code (it seems like the credentials aren't valid). However, when using the same credentials directly in Internet Explorer, I'm able to access the webpage. This is the code I have so far. Any of you experts see what I'm doing wrong? PS: I've changed the site-url, username & password, since it's a confidential site of my company.

          public static string GetHtmlPageSource(string url, string username, string password)
          {
              System.IO.Stream st = null;
              System.IO.StreamReader sr = null;
              try
              {
                  // make a Web request
                  System.Net.WebRequest req = System.Net.WebRequest.Create(url);
                  // if the username/password are specified, use these credentials
                  if (username != null && password != null)
                      req.Credentials = new System.Net.NetworkCredential(username, password);
                  // get the response and read from the result stream
                  System.Net.WebResponse resp = req.GetResponse();
                  st = resp.GetResponseStream();
                  sr = new System.IO.StreamReader(st);
                  // read all the text in it
                  return sr.ReadToEnd();
              }
              catch (Exception ex)
              {
                  return string.Empty;
              }
              finally
              {
                  // always close readers and streams
                  sr.Close();
                  st.Close();
              }
          }
      
          private void DataRefresh\_Tick(object sender, EventArgs e)
          {
              // Set the Refresh Interval to 15000 ms
              this.DataRefresh.Interval = 15000;
      
              // Declare the Variables, needed for this Application
              string FL, SL, UnFL, UnSL, ValueFL, ValueSL, ValueUnFL, ValueUnSL;
      
              //// Refresh the TopDesk Web Browser Object
              this.TopDesk.Refresh();
      
              //Read WebPage
              string page;
      
              page = GetHtmlPageSource("https://site", "user", "pass");
      
      
      
              //
      
      D Offline
      D Offline
      Dimitri Backaert
      wrote on last edited by
      #2

      Nobody? :-S This is what I found out: - The "page" variable gets a value, but when viewing this value using the built-in HTML viewer, I get the login-page. - At the bottom of this page, a message is displayed saying "Your browser is not supported". Could this cause the problem? If so, is there an alternative to make this work? Thanks in advance.

      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