C# Basic HTTP Request Class
Most of my clients, want me to do things with a Graphical User Interface. For this, I only have on language in mind, C#. And many of my client-related projects includes getting information from webpages, logging in, gathering data, etc. It’s very hard to repeat code over and over again with the HttpWebRequest and HttpWebResponse class. For this, I created a simple class that does GET and POST neatly and can handle cookies. Please do note that I didn’t reinvent the wheel on this one. This is just a collection of mostly used functions to make them reusable in many of my (and could be your) projects.
A simple HTTP GET request looks like this:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://ruel.me");
request.CookieContainer = cJar;
request.UserAgent = UserAgent;
request.KeepAlive = false;
request.Method = "GET";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream());
string response = sr.ReadToEnd();
And I think it’s fairly unacceptable to repeat this code over and over again in a single project. So yes, there should be a class made for this (together with the POST request).
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
namespace BasicReq
{
/// <summary>
/// A simple basic class for HTTP Requests.
/// </summary>
class BReq
{
/// <summary>
/// UserAgent to be used on the requests
/// </summary>
public string UserAgent = @"Mozilla/5.0 (Windows; Windows NT 6.1) AppleWebKit/534.23 (KHTML, like Gecko) Chrome/11.0.686.3 Safari/534.23";
/// <summary>
/// Cookie Container that will handle all the cookies.
/// </summary>
private CookieContainer cJar;
/// <summary>
/// Performs a basic HTTP GET request.
/// </summary>
/// <param name="url">The URL of the request.</param>
/// <returns>HTML Content of the response.</returns>
public string HttpGet(string url)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.CookieContainer = cJar;
request.UserAgent = UserAgent;
request.KeepAlive = false;
request.Method = "GET";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream());
return sr.ReadToEnd();
}
/// <summary>
/// Performs a basic HTTP POST request
/// </summary>
/// <param name="url">The URL of the request.</param>
/// <param name="post">POST Data to be passed.</param>
/// <param name="refer">Referrer of the request</param>
/// <returns>HTML Content of the response.</returns>
public string HttpPost(string url, string post, string refer = "")
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.CookieContainer = cJar;
request.UserAgent = UserAgent;
request.KeepAlive = false;
request.Method = "POST";
request.Referer = refer;
byte[] postBytes = Encoding.ASCII.GetBytes(post);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postBytes.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(postBytes, 0, postBytes.Length);
requestStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream());
return sr.ReadToEnd();
}
/// <summary>
/// Creates an HTML file from the string.
/// </summary>
/// <param name="html">HTML String.</param>
public void DebugHtml(string html)
{
StreamWriter sw = new StreamWriter("debug.html");
sw.Write(html);
sw.Close();
}
/// <summary>
/// Initializes a new instance of the <see cref="BReq"/> class.
/// </summary>
public BReq()
{
cJar = new CookieContainer();
}
/// <summary>
/// Releases unmanaged resources and performs other cleanup operations before the
/// <see cref="BReq"/> is reclaimed by garbage collection.
/// </summary>
~BReq()
{
// Nothing here
}
}
}
Of course the UserAgent is a public variable, so you can change it anytime upon initialization. The cookie container is used only inside the class. Yes, that’s a major con, but you can freely modify it. And that means, you can only use this class on one function if you would like to retain the cookies.
There’s also a DebugHtml function that can help you log the last request (by passing the response as a parameter, outside the class).
That’s it for now, and I highly suggest we help each other improve this class by forking it. This way improvements and ideas can be added. The more the merrier! Thank you.
Forks
These people made wonderful forks!