• Just released my first library for C# - its made writing web clients a lot easier for me and I hope
    10 replies, posted
I've always found that consuming web resources in an asynchronous manner in C# was more difficult than expected. I've written a little library to make getting data, posting forms and uploading files much easier. [code] Request.get("http://google.com/", (result)=> { Console.Write(result); }); [/code] I've based a lot of it on snippets of code I've used for some of my previous projects (now in production) and its worked nicely for me. Just thought i'd share the love and publish it into a library. Any comments/criticisms/bug reports would be welcome. Code is hosted on: [URL]http://httplib.codeplex.com/[/URL]
A few things. You should make the Request class static. You should check your arguments, because it is a library you should provide proper error messages if clients call your methods with bogus data. Like if someone calls Request.get with parameters == null they should get a ArgumentNullException, instead of a NullReferenceExecption All your methods should start with a capital letter.
[QUOTE=asciid;38997903]Request.[b]g[/b]et[/QUOTE] ;_; also [url=http://msdn.microsoft.com/en-us/library/system.net.webclient.downloadstringasync.aspx]WebClient.DownloadString[b]Async[/b][/url] [editline]28th December 2012[/editline] oh and [url=http://msdn.microsoft.com/en-us/library/system.net.webrequest.getresponseasync.aspx]WebRequest.GetResponse[b]Async[/b][/url]
Thanks for the tips. I've made naming more consistent across the library. Also capitalized the first letter of all method names too. Perl, web clients a nice tool. I've used it before for projects - its very easy to use. The reason wrote this was because webclient doesnt support file upload in Silverlight applications. My library supports writing a stream (such as image stream from camera) to a web request. The other methods are there for completeness. However, it still adds a lot of "sugar" for web requests - doing the following request would take a lot more effort using Webclient [code] Request.Post("http://testing.local/post.php", new {name="james",username="Redslide"}, (result)=> { Console.Write(result); }); [/code]
[QUOTE=asciid;39003377] [code]new {name="james",username="Redslide"} [/code][/QUOTE] wat holy shit it works [editline]28th December 2012[/editline] Also, yeah, I see how your library can be useful, thanks.
wait if that creates an anonymous type, how do you get the fields, reflection?
[QUOTE=Perl;39005333]wait if that creates an anonymous type, how do you get the fields, reflection?[/QUOTE] Yes, you just loop the properties.
It was easier than i expected - just loop the anonymous type's properties like this [code] foreach (var property in Parameters.GetType().GetProperties()) { string name = property.Name string value = property.GetValue(Parameters, null).ToString(); } [/code]
I approve of this, because I already did stuff with HttpWebGetRequest and it is clumsy thing. This looks 100% more easier to use!
Looks useful when its to small content, I doubt you would use this sort of stuff when you deal with large files, either way nice one.
I've added another set of features. Now allows you to save the response stream directly to disk like this - - program won't grow in size for huge-ass files. [code]Request.Get("http://cachefly.cachefly.net/100mb.test", (headers, result) => { FileStream fs = new FileStream(@"C:\100mb.test", FileMode.OpenOrCreate); result.CopyTo(fs); fs.Close(); });[/code]
Sorry, you need to Log In to post a reply to this thread.