5

I have a HTTP class that has a Get, and Post, method. It's a simple class I created to encapsulate Post and Get requests so I don't have to repeat the get/post code throughout the application.

In C#:

class HTTP
{
    private CookieContainer cookieJar;
    private String userAgent = "...";

    public HTTP()
    {
        this.cookieJar = new CookieContainer();
    }

    public String get(String url)
    {
        // Make get request. Return the JSON
    }

    public String post(String url, String postData)
    {
        // Make post request. Return the JSON
    }
}

I've made the CookieJar a property because I want to preserve the cookie values throughout the session. If the user is logged into Twitter with my application, each request I make (be it get or post) I want to use the cookies so they remain logged in.

That's the basics of it anyway. But, I don't want to return a string in all instances. Sometimes I may want the cookie, or a header value, or something else from the request.

Ideally I'd like to be able to do this in my code:

Cookie cookie = http.get("http://google.com").cookie("g_user");
String g_user = cookie.value;

or

String source = http.get("http://google.com").body;

My question - To do this, would I need to have a Get class, and a Post class, that are included within the HTTP class and are accessible via accessors?

Within the Get and Post class I would then have the Cookie method, and the body property, and whatever else is needed.
Should I also use an interface, or create a Request class and have Post and Get extend it so that common methods and properties are available to both classes?

Or, am I thinking totally wrong?

6
  • Thinking about it, I could just have Get and Post return a HTTPResponse, and any caller of Get or Post could accesses cookies, headers, body etc from that. That would save reinventing the wheel maybe, but I'd still like to know how to do something like this purely to learn. Jul 22, 2013 at 0:33
  • What does this have to do with design patterns?
    – Caleb
    Jul 22, 2013 at 6:33
  • 8
    Why are you reinventing the wheel? There's got to be a good HTTP library for C# already, and it's quite hard to get right. (It's grown to be a very complicated spec over the years.) Jul 22, 2013 at 16:41
  • 3
    Is there a reason to not use ASP.Net that already supports a lot of this stuff already?
    – JB King
    Aug 9, 2013 at 15:16
  • 1
    are you trying to build a fluent style request constructor or what? Knowing the end goal would be helpful. There is built in functionality for all of this and there has to be a reason you are avoiding / encapsulating it.
    – Bill
    Aug 13, 2013 at 14:44

1 Answer 1

2

For C#, can you use System.Net.HttpWebRequest? That class also seems to support cookies in HTTP requests, thought they appear to be turned off by default (you can turn them on), as noted here:

For security reasons, cookies are disabled by default. If you want to use cookies, use the CookieContainer property to enable cookies.

If you are trying to build your own class for learning purposes, then that class might still serve as a valuable reference point.

1
  • Also, its sister HttpWebResponse also does what the OP suggested in the comments to the question; provide generalized access to the response.
    – bzlm
    Feb 19, 2014 at 20:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.