Page Inclusion (Screen Scraping)
7
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
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>






This can be a problem when rendering .NET controls - where some objects like the main form cannot be duplicated. I have run into this when re-writing the main page for our portal - this little snippet has allowed me to build a dynamic templating engine for a 'locked' portal from our ERP vendor and completely re-work the way in which the portal functions.
So yes, I know about < ! -- Include -- > but it does have limitations...
<%@ Import Namespace="System.Net" %>
<script language="c#" runat="server" Debug="true">
private void Page_Load(object sender, System.EventArgs e)
{
WebClient webClient = new WebClient();
byte[] response = webClient.DownloadData("http://www.someserver.com");
Response.Write(Encoding.Default.GetString(response));
}
</script>
I've got good news, and I've got bad news:
The universe is merely a figment of my imagination.
Now are you ready for the bad news?