The WebHelper class is a new feature of the BizArk framework. It’s primary purpose is to replace System.Web.WebClient. The WebClient is a great class that makes it pretty easy to do simple web requests.
Unfortunately WebClient only supports a small set of requests such as simple gets, form value posts, single file uploads, and a few others. If you need to upload multiple files in the same request, you are out of luck. If you have a request that might take more than 100 seconds, you are out of luck. If you need to do anything special at all, you are pretty much out of luck and will have to use the HttpRequest object to perform the request.
HttpRequest allows you to do a lot more, but you are stuck implementing the different protocols for the content body. These are very specific and technical and easy to miss something, not to mention that it doesn’t support progress reporting (you have to build that yourself).
The Redwerb.BizArk.Core.Web.WebHelper class is intended to provide you full access to making requests without bothering you with all the mundane details. It includes the following features:
- Supports no content type, multipart/form-data, and application/x-www-form-urlencoded. Will automatically pick the correct content type based on the properties you set. It can also support custom content types. Just inherit from Redwerb.BizArk.Core.Web.ContentType and implement the two required methods.
- Can set the timeout for the request.
- Simple multiple file upload with form values. WebClient makes you send values in the query string.
- Supports in-memory file uploads. The file doesn’t actually have to exist on disk.
- Handles compressed responses with a simple property set.
- Supports progress. You can set an estimated response length to get an approximate total progress before a response is received. If not set, the request/response will be 50/50.
- Supports asynchronous requests.
- Event to modify the request before it is sent to the server. Provides you with full control over the request that is sent.
- Event to process the response instead of having the WebHelper handle it.
- The response is returned as a WebHelperResponse object. This object provides access to the result as well as status of the response. It also allows you to convert the response to another type other than a byte[] (such as string, image, etc).
To use the WebHelper, just set the url and call MakeRequest.
var helper = new WebHelper();
helper.Url = "http://localhost:57492/Test/SimpleTest";
var response = helper.MakeRequest();
var result = response.ConvertResult<bool>();
If you want to upload a file and some form variables, that’s easy too!
var helper = new WebHelper();
helper.Url = "http://localhost:57492/Test/UploadFileTest";
helper.FormValues.Add("test", "Hello");
helper.Files.Add(new UploadFile("file1", @"text\plain", "file1.txt", Encoding.UTF8.GetBytes("Hello World")));
helper.Files.Add(new UploadFile("file2", @"text\plain", "file2.txt", Encoding.UTF8.GetBytes("Goodbye World")));
var response = helper.MakeRequest();
var result = response.ResultToString();
I’m sure there is still a lot of work to do on this component. Let me know if you find any problems!
I would like to give a special thanks to aspnetupload.com. WebHelper essentially started out as a copy of UploadHelper from this site (though I don’t think there is too much in common with it anymore).
BizArk is available for download on the CodePlex website, http://bizark.codeplex.com.