Automatic Web Page Use With .NET
Written by Ian Elliot   
Wednesday, 28 August 2013
Article Index
Automatic Web Page Use With .NET
Accessing the data
Complete Program Listing

The Complete Program

This program isn't included in the codebin because it is very specific to the website it targets and therefore can't actually be run. 

The full listing of the program as described is:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace NetUse
{
 public partial class Form1 : Form
 {
  private int page;
  private WebBrowser webBrowser1;
  public Form1()
  {
   InitializeComponent();

  }

  private void makeBrowser()
  {
   webBrowser1 = new System.Windows.
                       Forms.WebBrowser();
   webBrowser1.Name = "webBrowser1";
  }

 

  private void showBrowserInForm()
  {
   webBrowser1.Location = new    
           System.Drawing.Point(12, 300);
   webBrowser1.MinimumSize = new
           System.Drawing.Size(20, 20);
   webBrowser1.Size = new
           System.Drawing.Size(783, 338);
   webBrowser1.TabIndex = 1;
   Controls.Add(webBrowser1);
  }

  private void button1_Click(
           object sender,
           EventArgs e)

  {
   page = 1;
   makeBrowser();
   showBrowserInForm();
   webBrowser1.DocumentCompleted += new    
    WebBrowserDocumentCompletedEventHandler
                      (DocumentCompleted);
   webBrowser1.Navigate("login URL");
  }
 

  private void login()
  {
   HtmlElement name = webBrowser1.Document.
             GetElementById("txtUserName");
   if (name != null)
   {
     name.InnerText = "User name";
   }

   HtmlElement password =webBrowser1.
     Document.GetElementById("pasPassword");
   if (password != null)
   {
     password.InnerText = "password";
   }
   HtmlElement submit = webBrowser1.
    Document.GetElementById("login-submit");
   if (submit != null)
   {
    submit.InvokeMember("click");
   }

  }


  private void gotoData()
  {
   webBrowser1.Navigate("URL of Data Page");
  }


  private void getData()
  {
   string text = webBrowser1.DocumentText;
   Regex ex1 = new
                Regex(@"used\s+(\d+.\d+)");
   string used = ex1.Match(text).
                         Groups[1].ToString();
   textBox1.Text = used;
   webBrowser1.Dispose();
  }

 

  void DocumentCompleted(
           object sender,    
    WebBrowserDocumentCompletedEventArgs e)
  {
   if ( webBrowser1.ReadyState!=
     WebBrowserReadyState.Complete) return;
   switch (page)
   {
    case 1:
     page = 2;
     login();
     break;
    case 2:
     page = 3;
     gotoData();
     break;
    case 3:
     page = 4;
     getData();
     break;
   }

  }
 }
}

 

Related Articles:
Hit Highlighting with dtSearch
Automating applications using messages

 

blog comments powered by Disqus

 

To be informed about new articles on I Programmer, subscribe to the RSS feed, follow us on Google+Twitter, Linkedin or Facebook or sign up for our weekly newsletter.

 

Banner


Deep Monitor Your Site

How can you find out how your dynamic web site is working? The answer is to add some sort of monitoring and the temptation to do it yourself should be resisted at all costs. You have better things to  [ ... ]



Dealing With Forum & Mailing List Spam

Forum spam is a big problem and very difficult to combat. There are some solutions, none perfect, but this one might help reduce the burden on spam in your forum or mailing list.


Other Projects

 

 

 



Last Updated ( Thursday, 16 January 2014 )
 
 

   
RSS feed of all content
I Programmer - full contents
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.