Click here to Skip to main content
15,908,776 members
Home / Discussions / C#
   

C#

 
QuestionRe: MS Office Outllok - VSTO - Host Windows.Forms control within an Outlook 2003 form region Pin
Programm3r1-Oct-09 2:31
Programm3r1-Oct-09 2:31 
QuestionHost Windows.Forms control within an Outlook 2003 form region using VS2008 Pin
Programm3r1-Oct-09 2:54
Programm3r1-Oct-09 2:54 
AnswerRe: Host Windows.Forms control within an Outlook 2003 form region using VS2008 Pin
Programm3r1-Oct-09 3:15
Programm3r1-Oct-09 3:15 
AnswerRe: Host Windows.Forms control within an Outlook 2003 form region using VS2008 Pin
Programm3r1-Oct-09 3:26
Programm3r1-Oct-09 3:26 
QuestionFeedback Mechanisms Pin
satsumatable1-Oct-09 1:36
satsumatable1-Oct-09 1:36 
AnswerRe: Feedback Mechanisms Pin
stancrm1-Oct-09 1:40
stancrm1-Oct-09 1:40 
QuestionC# vs Powershell in querying 64bit registry from 32bit system Pin
Randy Grugan1-Oct-09 1:36
Randy Grugan1-Oct-09 1:36 
QuestionGetting HTML Source from a WebPage in String-format, using NetworkCredentials [modified] Pin
Dimitri Backaert1-Oct-09 1:24
Dimitri Backaert1-Oct-09 1:24 
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");



        // Set Values to "Nothing"
        ValueFL = null;
        ValueSL = null;
        ValueUnFL = null;
        ValueUnSL = null;
        FL = null;
        SL = null;
        UnFL = null;
        UnSL = null;

        // Read & Analyse the Webpage shown in the Web Browser Object, and use certain values displayed here
        try
        {
            FL = page.Substring(5510, 7);
            SL = page.Substring(6361, 7);
            UnFL = page.Substring(5721, 7);
            UnSL = page.Substring(6572, 7);
        }
        catch
        {
            DialogResult buttonResult = MessageBox.Show("Website not accessible. Please check the network connection, and check your login credentials on the TopDesk-website.", "Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            if (buttonResult == DialogResult.OK)
            {
                this.Close();
            }
        }

        // Loop through the obtained data, and check if the characters are Numeric
        for (int X = 0; X < 8; X++)
        {
            int valueA = int.Parse(FL.Substring(X, 1));
            ValueFL = ValueFL + FL.Substring(X, 1);

            int valueB = int.Parse(SL.Substring(X, 1));
            ValueSL = ValueSL + SL.Substring(X, 1);

            int valueC = int.Parse(UnSL.Substring(X, 1));
            ValueUnSL = ValueUnSL + UnSL.Substring(X, 1);

            int valueD = int.Parse(UnFL.Substring(X, 1));
            ValueUnFL = ValueUnFL + UnFL.Substring(X, 1);


            try
            {

            }
            catch
            {
                Debug.WriteLine("ValueFL: " + FL.Substring(X, 1) + " is not a valid Integer.");
            }

            try
            {
            }
            catch
            {
                Debug.WriteLine("ValueSL: " + SL.Substring(X, 1) + " is not a valid Integer.");
            }

            try
            {
            }
            catch
            {
                Debug.WriteLine("ValueUnSL: " + UnSL.Substring(X, 1) + " is not a valid Integer.");
            }

            try
            {
            }
            catch
            {
                Debug.WriteLine("ValueUnFL: " + UnFL.Substring(X, 1) + " is not a valid Integer.");
            }
        }

        // Calculate the Total number of Calls, FL + SL
        int TotalCalls;
        TotalCalls = Convert.ToInt32(ValueFL) + Convert.ToInt32(ValueSL);

        // Fill Values in the DashBoard
        this.aGaugeSL.Value = Convert.ToInt32(ValueSL);
        this.aGaugeFL.Value = Convert.ToInt32(ValueFL);
        this.txtBoxFLOpen.Text = ValueFL;
        this.txtBoxSLOpen.Text = ValueSL;
        this.lblOpenCallsFLNbr.Text = ValueFL;
        this.lblOpenCallsSLNbr.Text = ValueSL;
        this.lblUnassignedCallsFLNbr.Text = ValueUnFL;
        this.lblUnassignedCallsSLNbr.Text = ValueUnSL;
        this.lblTotalCallsFL_SL.Text = "Total Number of Open Calls (FL + SL): " + Convert.ToString(TotalCalls);

        // Refresh all Objects
        aGaugeFL.Refresh();
        aGaugeSL.Refresh();
        txtBoxFLOpen.Refresh();
        txtBoxSLOpen.Refresh();
        lblTotalCallsFL_SL.Refresh();
        lblUnassignedCallsFLNbr.Refresh();
        lblUnassignedCallsSLNbr.Refresh();
        lblOpenCallsFLNbr.Refresh();
        lblOpenCallsSLNbr.Refresh();
        pictureBoxStatusFL.Refresh();
        pictureBoxStatusSL.Refresh();
    }

}


modified on Monday, October 5, 2009 8:32 AM

AnswerRe: Getting HTML Source from a WebPage in String-format, using NetworkCredentials Pin
Dimitri Backaert2-Oct-09 2:18
Dimitri Backaert2-Oct-09 2:18 
QuestionOptional Parameter Pin
ksss_maheshece1-Oct-09 1:23
ksss_maheshece1-Oct-09 1:23 
AnswerRe: Optional Parameter Pin
stancrm1-Oct-09 1:27
stancrm1-Oct-09 1:27 
AnswerRe: Optional Parameter Pin
Pete O'Hanlon1-Oct-09 1:37
mvePete O'Hanlon1-Oct-09 1:37 
GeneralRe: Optional Parameter Pin
ksss_maheshece1-Oct-09 2:23
ksss_maheshece1-Oct-09 2:23 
GeneralRe: Optional Parameter Pin
Mirko19801-Oct-09 2:44
Mirko19801-Oct-09 2:44 
GeneralRe: Optional Parameter Pin
ksss_maheshece1-Oct-09 2:47
ksss_maheshece1-Oct-09 2:47 
GeneralRe: Optional Parameter Pin
Mirko19801-Oct-09 2:53
Mirko19801-Oct-09 2:53 
AnswerRe: Optional Parameter Pin
Vasudevan Deepak Kumar1-Oct-09 3:02
Vasudevan Deepak Kumar1-Oct-09 3:02 
AnswerRe: Optional Parameter Pin
Dave Kreskowiak1-Oct-09 4:07
mveDave Kreskowiak1-Oct-09 4:07 
QuestionHi Pin
pallavi091-Oct-09 0:24
pallavi091-Oct-09 0:24 
AnswerRe: -De-Hi Pin
Henry Minute1-Oct-09 1:01
Henry Minute1-Oct-09 1:01 
GeneralRe: -De-Hi Pin
Luc Pattyn1-Oct-09 3:37
sitebuilderLuc Pattyn1-Oct-09 3:37 
GeneralRe: -De-Hi Pin
Henry Minute1-Oct-09 5:33
Henry Minute1-Oct-09 5:33 
Questionhi friend's Pin
mr.mohsen30-Sep-09 23:44
mr.mohsen30-Sep-09 23:44 
AnswerRe: hi friend's Pin
stancrm30-Sep-09 23:48
stancrm30-Sep-09 23:48 
GeneralRe: hi friend's Pin
mr.mohsen30-Sep-09 23:54
mr.mohsen30-Sep-09 23:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.