Before web services came along, screen scraping was a popular technique for grabbing the output from another application by examining the text it displays on the screen. For web applications, this meant making a request to a URL and examining the HTML the server returns. You .
With RSS, XML, And Web Services, The NEED TO SCREEN SCRAPE HAS DIMINISHED, BUT IS NOT Extinct. In this article we will examine a few methods to grab the html from another url and for display in your OWN Page.
Httpserverutility
If the page you need to fetch is part of the current web application, you can use the execute method on the Server object of the current page. The Server object is of type HttpServerUtility, which also includes the well-known methods Transfer and MapPath. Using Execute Is StraightForward:
Textwriter textwriter = new stringwriter ();
Server.execute ("My PortPage.aspx", TextWriter;
Response.Output.write (TextWriter.toString ());
You can use Server.Execute to add content to frames, or devise print friendly pages. We generally would not want to write the entire contents of the resulting string into the response as we have in this sample, but instead would parse select content from myOtherPage .aspx. of course, We Are Not Always So Lucky to Have The Resource We need INSIDE OF THE Web Application, and this is where classes from the system.net namespace comin.
WebClient
The WebClient Class Presents The Simplest API Possible for Retrieving Content from a URL, As Seeen Below.
Using (WebClient WebClient = new WebClient ())
{
Byte [] response = WebClient.downloaddata (Tur); response.outputstream.write (response, 0, response.length);
}
We need only three lines of code, but this time instead of passing the name of an ASPX page inside of our application, we can pass the URL to a remote resource, like http://www.OdeToCode.com/default.aspx.
The next hurdle you might face is retrieving content from a web site requiring forms authentication. Forms authentication usually requires a user to enter credentials into a form and press a submit button. Pressing submit will cause the browser to perform an HTTP "POST" and send .
AS An Example, Consider The Source Code for the Following Login Form: