The .NET Framework provides classes for performing HTTP requests. This HOWTO describes how to perform both GET and POST requests.
System.Net namespace contains the
classes which fetch data from web servers and HTTP based web services. Often you will also want to add a reference to
which will give you access to the
HttpUtility class that provides methods to HTML and URL encode and decode text strings.
Yahoo! Web Services return XML data. While some web services can also return the data in other formats, such as JSON and Serialized PHP, it is easiest to utilize XML since the .NET Framework has extensive support for reading and manipulating data in this format.
The following example retrieves a web page and prints out the source.
Some APIs require you to make POST requests. To accomplish this we change the request method and content type and then write the data into a stream that is sent with the request.
The del.icio.us API requires you to make authenticated requests,
passing your del.icio.us username and password using HTTP authentication. This is easily accomplished by adding
an instance of
NetworkCredentials to the request.
Yahoo! offers many REST based web services but they don't all use the same error handling. Some web services return status code 200 (OK) and a detailed error message in the returned XML data while others return a standard HTTP status code to indicate an error. Please read the documentation for the web services you are using to see what type of error response you should expect. Remember that HTTP Authentication is different from the Yahoo! Browser-Based Authentication.
HttpRequest.GetResponse() will raise an exception if the server does not
return the status code 200 (OK), the request times out or there is a network error.
Redirects are, however, handled automatically.
Here is a more full featured sample method that prints the contents of a web page and has basic error handling for HTTP error codes.
Related information on the web.