Requestor : the heart of REST

2007-12-11 @ 01:09#

heart several months back i dawned on me that the most important single class i could use in any web application was one that mimicked the client browser. a Requestor class that allowed me to control request parameters including cookies, authentication, and headers. and returned the results of the request including any body, altered cookies, response headers, etc.

so i started working on various versions of this class. finally, i had one that i liked. and i use it *alot.* it's my stand-in proxy when i want my client-side scripts to have access to remote Ajax/RSS data on other servers. it's my server-side utility for maintaining the most recent cache entries for delivery to all web clients. and i use it to even do resource validation work against my own data stores.

so here's what the class method/property signature looks like. several properties and only one method:

public class Requestor
{
    // public properties
    public WebHeaderCollection RequestHeaders = new WebHeaderCollection();
    public WebHeaderCollection ResponseHeaders = new WebHeaderCollection();
    public CookieContainer CookieCollection = new CookieContainer();
    public HttpStatusCode ResponseStatusCode = HttpStatusCode.OK;
    public string ResponseDescription = string.Empty;
    public DateTime ResponseLastModified = System.DateTime.MaxValue;
    public NetworkCredential Credentials = new NetworkCredential();
    public long ResponseLength = 0;

    public Requestor() { }

    // method that makes the call
    public string Execute(string url)
    public string Execute(string url, string method)
    public string Execute(string url, string method, string contentType)
    public string Execute(string url, string method, string contentType, string body)
}

now that i've started using it, i can't imagine working without it. it's so much easier to use this high-level class than it is to wrestle with HttpWebRequest and HttpWebResponse classes.

code