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