Scheme://Host/Path?Query
The scheme is the protocol that will be used to transfer data.
The host specifies to which server the HTTP request is to be directed. There are several
different formats in which the URL host can be represented.
First, it can be in the typical
domain form, such as:
www.httprecipes.com
Second, it can be expressed as an IP address, such as:
127.0.0.1
Finally, it can be expressed as a symbol that is resolved in the “hosts” file on the computer,
such as:
localhost
The path specifies which file to retrieve, or which script to run on the server. The “query”
specifies parameters to be passed to the URL. The query immediately follows the path
delimited by a question mark.If you are passing multiple parameters, separate them with the
ampersand symbol (&).
URLs can contain a wide variety of characters; however, there are only certain characters
that you can put directly into a URL. For example, you cannot put a space into a URL.
You need to conduct three basic steps to read data from a URL. A summary
of these steps is:
• Create a Uri Object
• Open a stream
• Read data from the stream
try
{
WebRequest http = HttpWebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)http.GetResponse();
Stream stream = response.GetResponseStream();
}
catch(UriFormatException e)
{
Console.WriteLine(“Invalid URL”);
}
catch(IOException e)
{
Console.WriteLine(“Could not connect to URL”);
}
StreamReader stream =
new StreamReader(response.GetResponseStream(),
System.Text.Encoding.ASCII );
String result = stream.ReadToEnd();
Console.WriteLine( result );
response.Close();
stream.Close();
return result;