I just started to write a Class Library in C# that will implement Gets, Posts (Http Requests of Post and Gets) and oAuth (Authentication method using tokens logic).
Currently, every aplication me and my colleagues write have its own “HttpMethods” class that is responsible for executing Gets and Posts.
Here is a quick Example of a Get That we have :
public string Get(string url, string refererPage = "")
{
string response = null;
try
{
// Web request
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Timeout = m_timeout;
request.Method = "GET";
request.Referer = refererPage;
request.CookieContainer = m_CookieJar;
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
request.ServicePoint.ConnectionLimit = Consts.CONNECTIONS_LIMIT;
request.UserAgent = Consts.URI_USER_AGENT;
request.AllowAutoRedirect = true;
request.Host = Consts.API_HOST;
// Web response
using (HttpWebResponse resp = (HttpWebResponse)request.GetResponse())
response = new StreamReader(resp.GetResponseStream()).ReadToEnd();
}
catch (Exception ex)
{
LogWriter.Error(ex);
}
return response;
}
My question is : Which will be the best approach for us to addopt ?
We want to have our own lib that will use the HttpWebRequest object,ending to something just like this:
MyOwnDll.MyOwnClassResponsibleForWebRequests.Get (Parameters)
Same for a Post Method.
What should i do ?
Thanks in advance
I think you should follow Factory Pattern.
Something like this:
or you can make the request itself and return the result instead of HttpRequest object.