Page Inclusion (Screen Scraping)





7
Date Submitted Thu. Jan. 11th, 2007 8:47 AM
Revision 1 of 1
Scripter Casper42
Tags ASPX | C | CSharp
Comments 3 comments
This little number will allow the contents of other pages to be included or embedded into the rendered HTML of an aspx page. Using the WebClient class from the .NET framework, you can conditionally load pages, even manipulate the HTML before it is rendered and in this example, fill in forms and execute javascript actions.

The following example shows how to auto-login to an Exchange Server - this was something requested for inclusion into our portal, but I left out the logic that pulls/decrypts the real password - for obvious reasons.

Enjoy!
~Jeremy

<%@ Import Namespace="System.Net" %>
<script language="c#" runat="server" Debug="true">
  private void Page_Load(object sender, System.EventArgs e)
  {
    string LOGIN_URL = "https://mail.myserver.com/exchweb/bin/auth/owalogon.asp";
    string strUser = "MyUsername";
    string strPass = "MyPassword";
   
    byte[] response;
    WebClient webClient = new WebClient();
    response = webClient.DownloadData(LOGIN_URL);
    string strHTML = Encoding.Default.GetString(response);   
   
    // Fill in the form-based authentication - OPTIONAL
      // Fix relative paths
      strHTML = strHTML.Replace("/exchweb",
                "https://mail.myserver.com/exchweb");   
      // Fill in the form
      strHTML = strHTML.Replace("id=\"username\"",
                "id=\"username\" value=\"" + strUser + "\"");
      strHTML = strHTML.Replace("id=\"password\"",
                "id=\"password\" value=\"" + strPass + "\"");
      // Change the onload behavior to auto-submit the form
      strHTML = strHTML.Replace("onload=\"setFocus()\"",
                "onload=\"document.forms[0].submit()\"");

    // Render HTML
    Response.Write(strHTML);
  }

</script>

 

Jeremy Edmiston

Comments

Comments < ! -- Include -- >
Thu. Jan. 11th, 2007 8:54 AM    Scripter Casper42
Comments Simple inclusion...
Thu. Jan. 11th, 2007 8:58 AM    Scripter Casper42
Comments C# != C
Wed. Apr. 2nd, 2008 2:56 PM    Scripter sehrgut

Voting