How Do I...Make a GET request?This sample illustrates how to use the WebRequest and WebResponse classes to make a GET request on a URI. The function getPage is where the specific details of making the request can be found. The rest of the code in this sample is for taking command-line parameters as well as displaying help usage for the parameters. This sample program is a command-line utility that runs at the command prompt. The getPage function takes a string parameter, which is the URL (or URI) of the web page you are requesting. This URI is then included as a parameter in a call to WebRequest.Create which creates a WebRequest object. The GetResponse function of the WebRequest object is then used to get a WebResponse object. This object can be used to get the status code of the response, as well as the actual response stream (a web page, for instance). Writing out the stream can take several different forms. This example reads a byte array (of 512 bytes) into the read Byte[] variable with the Read function. It then writes out the 512 bytes with the function: Console.Write(System.Text.Encoding.ASCII.GetString(read, 0, bytes)); When you are done with the response stream you must make sure to call the Close method of the WebResponse object to avoid leaking valuable system resources. Note: Behind a proxy server, only internal URI's will work with this sample. Example
|