Fluent Interface for WebRequest class
All source codes can be found as http://fluentwebrequest.codeplex.com..
In past, I usually used WebRequest for request some data from remote host. I also used it for request JSON data from Google APIs, some RSS, ATOM data from a few hosts. Everything is good for me. I only put some codes like this:
var buffer = Encoding.ASCII.GetBytes("q=Queen Elizabeth II&video=on&audio=on&text=on"); var webReq = (HttpWebRequest)WebRequest.Create("http://www.bbc.co.uk/search/news/"); webReq.Method = "POST"; webReq.ContentType = "application/x-www-form-urlencoded"; webReq.ContentLength = buffer.Length; var postData = webReq.GetRequestStream(); postData.Write(buffer, 0, buffer.Length); postData.Close(); var webResp = (HttpWebResponse)webReq.GetResponse(); var answer = webResp.GetResponseStream(); var result = string.Empty; if (answer != null) { using (var tempStream = new StreamReader(answer)) { result = tempStream.ReadToEnd(); } }
This code for POST method, and with GET method, we usually use it like this:
const string getVars = "?q=Queen Elizabeth II&video=on&audio=on&text=on"; var webReq = (HttpWebRequest)WebRequest.Create(string.Format("http://www.bbc.co.uk/search/news/{0}", getVars)); webReq.Method = "GET"; var webResp = (HttpWebResponse)webReq.GetResponse(); var answer = webResp.GetResponseStream(); var result = string.Empty; if (answer != null) { var tempStream = new StreamReader(answer); result = tempStream.ReadToEnd(); }
It will work well, no problems. WebRequest class is very cool. But we can do it better with Fluent Interface, easy to read and more semantic. Fluent Interface is a good choice for this case, we can make this code really interesting in. After took 4 hours to consider and finally I also work on it. This is just for fun, I promise with you about that. I love beautiful code and how to make the code can express itself. How do you feel about this code?
var result = RequestSubmitter.GetInstance() .WithUrl("http://www.bbc.co.uk/search/news/") .Item.WithNameIs("q").ValueIs("Queen Elizabeth II") .ConnectWith.Item.WithNameIs("video").ValueIs("on") .ConnectWith.Item.WithNameIs("audio").ValueIs("on") .ConnectWith.Item.WithNameIs("text").ValueIs("on") .WithRequestTypeIs(RequestMethodType.Post) .BuildUrl() .GetDataFromUrl();
It is quite Fluent Interface. You can config all things by code and you can read it well. It is only a tiny project. For your curious about this project, I will show you some classes inside it. So for easy to you to imagine, I will paste the class diagram at here:
All a red arrows is a flow for Fluent Interface that I made. As you see, it flow from IRequestSubmitter => IRequestDataBuilder => IRequestItem => IRequestItemName => IRequestType => IAndConnection => IRequestDataBuilder and finally it will loop between IRequestDataBuilder and IRequestItem. It is simple, isn't it? Don't have any secret at here.
And the GET and POST method request, I built it like this:
I made a builder for GET and POST, and finally create a factory class for create these objects. This is enough simple and easy to understand, I think so.
Conclusion, I want to talk once again "it is only for fun and better code". You can find some examples and source code for this project at http://fluentwebrequest.codeplex.com/. And don't forget put some your comments about this project. Thanks for your read and happy coding.