VisualStudioFeeds

All Visual Studio blogs in one place

AddThis Social Bookmark Button
 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...(Read whole news on source site)

Search

Subscribe

Enter your email address:

Delivered by FeedBurner

Increase your website traffic with Attracta.com

Contact

Email:
Subject:
Message:
Anti-spam: How many eyes has a typical person?

Follow us on FB

Home : Blog List : Context is King : Fluent Interface for WebRequest class