Http client for .Net 4.0 framework (without bcl)
PM> Install-Package TinyClient
- No bcl
- Batching
- Fluent style
var client = new HttpClient("http://myHost.io");
var received = client.PostAndReceiveJson<MyAnswerVm>(
query: "getMyAnswer",
jsonSerializeableContent: new MyRequestVM { Name = "Bender"});
Console.WriteLine(received.Name);
//need no to close connection
var answer = client
.Create("localhost:8080")
.Build()
.SendJsonPost("users", new MyRequestVM {Name = "Bender"})
.ThrowIfNot2xx()
.GetJsonObject<MyAnswerVm>();
Console.WriteLine(answer.Name);
var customClient = HttpClient
.Create("http://myHost.io")
.WithKeepAlive(true)
.WithCustomDecoder(ClientEncoders.Deflate)
.WithRequestTimeout(TimeSpan.FromSeconds(1))
.WithRequestMiddleware((r) => r.AddCustomHeader("sentBy", "MasterOfHardcore"))
.WithResponseMiddleware((r) => {
if (r.StatusCode != HttpStatusCode.OK)
throw new FormatException("Request failed with error: " + r.StatusCode);
})
.Build();
//request uri is http://myHost.io/search?text=What+up&attributes=all
var customRequest = HttpClientRequest
.Create(HttpMethod.Post, "/search")
.AddUriParam("text", "What up")
.AddUriParam("attributes", "all")
.AddCustomHeader("nugetPackage", "tinyClient")
.AddCustomHeader("_SessionId", "42")
.AddContentEncoder(ClientEncoders.Deflate)
.AddAcceptEncoding(ClientEncoders.Deflate)
.SetKeepAlive(true)
.SetTimeout(TimeSpan.FromSeconds(5))
.SetContent(new JsonContent(new MyRequestVM {Name = "Cartman"}));
var textResponse = customClient
.Send(customRequest) as HttpResponse<string>;
Console.WriteLine(textResponse?.Content);
Request-response process produces different results:
- Response is succesfully ([200-299] http codes)
- Server error: (non succesfully http codes)
- You can catch an exception here
- Or do you want to get response with bad status in usual flow?
- Serialization or deserialization error
- Connection errors (host not found, invalid url etc...)
- Tiny client timeout error
try
{
var response = client
//throws WebException (connection errors) or TinyTimeoutException:WebException
.SendGet("users")
//throws TinyWebException: WebException (status errors)
.ThrowIfNot2xx()
//throws InvalidDataException
.GetJsonObject<MyAnswerVm>();
//handle the response
}
catch(WebException e){
if (e is TinyHttpException){
var errorResponse = e.HttpResponse;
//Do something with errorResponse
}
else if(e is TinyTimeoutException){
//...
}
else{
//Do something with connection problems
}
}
catch(InvalidDataException e) {
//omg, the server is going crazy!
}
try
{
var response = client
//throws WebException (connection errors) or TimeoutException
.SendGet("users");
if (response.IsNot5xx()) {
//handle the response
} else {
//handle bad status code
}
}
catch(Exception e){
//...
}