How to Get Data Through WebRequest
The ASP.NET "WebRequest" control opens a Web page and retrieves information about the page from the host server. You use this control to scrape data from a website, extract information and display it on your application. The website can be an external public page or an internal website within a company intranet. WebRequest works with WebResponse to store the data in an ASP.NET variable.
Instructions
-
-
1
Click the Windows "Start" button and type "visual studio" in the text box. Press "Enter" to open the Visual Studio software. Open your ASP.NET project.
-
2
Double-click the Web form in Solution Explorer. Double-clicking a form displays the form in the Visual Studio designer. Right-click the form and click "View Code."
-
-
3
Create the "WebRequest" control. The WebRequest class requires a website to which the application connects. The following code connects to a website named "mydomain.com":
WebRequest website = WebRequest.Create ("http://mydomain.com");
-
4
Get the response from the website and assign the response to a "WebResponse" control. The following code shows you how to retrieve the response from the website:
WebResponse server = website.GetResponse ();
-
5
Display the response. The data from the website stores in the "server" variable in step four. The following code shows you how to display the server response:
Stream sitedata = server.GetResponseStream ();
StreamReader stream = new StreamReader (sitedata);
string serverdata = stream.ReadToEnd ();
Console.WriteLine (serverdata);
-
1