PostRequestFactory
This is a.Net library which facilitates manually creation of a web request body. The web request body structure respects all the standards specified in
HTML FORMS and it supports generation of the web request body of
application/x-www-form-urlencoded and
multipart/form-data content types.
PostRequestFactory was created by using .Net Framework 4.0 .
Basically for being able to access functionalities provided by this library you should use
RequestFactory class which exposes a static method
CreateRequest that will return an instance of the
PostRequest class which contains all the data about web request. A
PostRequest is build based on the provided parameters which can be of the following types:
- BasicRequestParameter - used only for application/x-www-form-urlencoded content types
- TextRequestParameter - used only for multipart/form-data content types and it can be mixed with BinaryRequestParameter
- BinaryRequestParameter - used only for multipart/form-data content types, used for larger binary data transport
Usage flow is the following: You should encapsulate web request data into corresponding request parameters and based on these parameters will be generated a web request, content of which you can use in various forms.
Download
PostRequestFactory from CodePlex or install using
NuGet.
Usage examples (C#)
- Creates an application/x-www-form-urlencoded web request
var paramList =
new List<IRequestParameter>
{
new BasicRequestParameter("value", "xxxȘȘȘșșaaaȚȚȚbbbÎÎ"),
new BasicRequestParameter("value2", "AbcȘaBc")
};
var request = RequestFactory.CreateRequest(paramList);
webClient.Headers.Add("Content-Type", request.ContentTypeHeader);
var response = webClient.UploadData("http://TestUrl/TestAction", request.BodyAsBytes);
- Creates a multipart/form-data web request
var webClient = new WebClient();
var paramList =
new List<IRequestParameter>
{
//this parameter's content will be transported as base64
new TextRequestParameter("value", "xxxȘȘȘșșaaaȚȚȚbbbÎÎ",true, Encoding.UTF8),
new TextRequestParameter("value2", "cccĂĂĂ")
};
var request = RequestFactory.CreateRequest(paramList);
webClient.Headers.Add("Content-Type", request.ContentTypeHeader);
var response = webClient.UploadData("http://TestUrl/TestAction", request.BodyAsBytes);
- Creates a multipart/form-data web request by sending a bigger quantity of binary information
var webClient = new WebClient();
var paramList =
new List<IRequestParameter>
{
new BinaryRequestParameter("value", "xxxȘȘȘșșaaaȚȚȚbbbÎÎ"),
new BinaryRequestParameter("value2", "cccĂĂĂ"),
new BinaryRequestParameter("value3",File.ReadAllBytes("BigFile.data"),"BigFile.data")
};
var request = RequestFactory.CreateRequest(paramList);
webClient.Headers.Add("Content-Type", request.ContentTypeHeader);
var response = webClient.UploadData("http://TestUrl/TestAction", request.BodyAsBytes);