The .NET Framework provides classes for performing HTTP requests. This HOWTO describes how to perform both GET and POST requests.
- Overview
- Simple GET Requests
- Simple POST Requests
- HTTP Authenticated Requests
- Error Handling
- Further Reading
Overview
The System.Net
namespace contains the HttpWebRequest
and HttpWebResponse
classes which fetch data from web servers and HTTP based web services. Often you will also want to add a reference to System.Web
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.
Simple GET Requests
The following example retrieves a web page and prints out the source.
C# GET SAMPLE 1
- using System;
- using System.IO;
- using System.Net;
- using System.Text;
- // Create the web request
- HttpWebRequest request = WebRequest.Create(“https://developer.yahoo.com/”) as HttpWebRequest;
- // Get response
- using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
- {
- // Get the response stream
- StreamReader reader = new StreamReader(response.GetResponseStream());
- // Console application output
- Console.WriteLine(reader.ReadToEnd());
- }
Simple POST Requests
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.
C# POST SAMPLE 1
- // We use the HttpUtility class from the System.Web namespace
- using System.Web;
- Uri address = new Uri(“http://api.search.yahoo.com/ContentAnalysisService/V1/termExtraction”);
- // Create the web request
- HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;
- // Set type to POST
- request.Method = “POST”;
- request.ContentType = “application/x-www-form-urlencoded”;
- // Create the data we want to send
- string appId = “YahooDemo”;
- string context = “Italian sculptors and painters of the renaissance”
- + “favored the Virgin Mary for inspiration”;
- string query = “madonna”;
- StringBuilder data = new StringBuilder();
- data.Append(“appid=” + HttpUtility.UrlEncode(appId));
- data.Append(“&context=” + HttpUtility.UrlEncode(context));
- data.Append(“&query=” + HttpUtility.UrlEncode(query));
- // Create a byte array of the data we want to send
- byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString());
- // Set the content length in the request headers
- request.ContentLength = byteData.Length;
- // Write data
- using (Stream postStream = request.GetRequestStream())
- {
- postStream.Write(byteData, 0, byteData.Length);
- }
- // Get response
- using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
- {
- // Get the response stream
- StreamReader reader = new StreamReader(response.GetResponseStream());
- // Console application output
- Console.WriteLine(reader.ReadToEnd());
- }
HTTP Authenticated requests
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 ofNetworkCredentials
to the request.
C# HTTP AUTHENTICATION
- // Create the web request
- HttpWebRequest request
- = WebRequest.Create(“https://api.del.icio.us/v1/posts/recent”) as HttpWebRequest;
- // Add authentication to request
- request.Credentials = new NetworkCredential(“username”, “password”);
- // Get response
- using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
- {
- // Get the response stream
- StreamReader reader = new StreamReader(response.GetResponseStream());
- // Console application output
- Console.WriteLine(reader.ReadToEnd());
- }