How to Get Data Through WebRequest
- 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);