The following example shows how to create a webrequest instance and return a response.
MS-help: // Ms. msdnqtr. v80.chs/ms. msdn. v80/ms. netdevfx. v1_chs/cpref10/html/t_system_net_webrequest.htm
C #
Using system;
Using system. IO;
Using system. net;
Using system. text;
Namespace examples. system. net
{
Public class webrequestgetexample
{
Public static void main ()
{
// Create a request for the URL.
Webrequest request = webrequest. Create ("http://www.contoso.com/default.html ");
// If required by the server, set the credentials.
Request. Credentials = credentialcache. defaultcredentials;
// Get the response.
Httpwebresponse response = (httpwebresponse) request. getresponse ();
// Display the status.
Console. writeline (response. statusdescription );
// Get the Stream Containing content returned by the server.
Stream datastream = response. getresponsestream ();
// Open the stream using a streamreader for easy access.
Streamreader reader = new streamreader (datastream );
// Read the content.
String responsefromserver = reader. readtoend ();
// Display the content.
Console. writeline (responsefromserver );
// Cleanup the streams and the response.
Reader. Close ();
Datastream. Close ();
Response. Close ();
}
}
}
Ironpython
From system import *
From system. Io import *
From system. net import *
From system. Text import *
Request = webrequest. Create ("http://www.contoso.com/default.html ")
Response = request. getresponse ()
Console. writeline (response. statusdescription)
Datastream = response. getresponsestream ()
Reader = streamreader (datastream)
Responsefromserver = reader. readtoend ()
Console. writeline (responsefromserver)
Reader. Close ()
Datastream. Close ()
Response. Close ()
Powershell
$ Request = [system. net. webrequest]: Create ("http://www.contoso.com/default.html ");
$ Request. Credentials = [system. net. credentialcache]: defaultcredentials
$ Response = $ request. getresponse ()
[System. Console]: writeline ($ response. statusdescription)
$ Datastream = $ response. getresponsestream ()
$ Reader = new-Object System. Io. streamreader ($ datastream)
$ Responsefromserver = $ reader. readtoend ()
[System. Console]: writeline ($ responsefromserver)
$ Reader. Close ()
$ Datastream. Close ()
$ Response. Close ()