-
Notifications
You must be signed in to change notification settings - Fork 1
CompressedRequestsHandler
The compressed request handler/filter uses GZip compression to send compressed request bodies to the server. This (usually) reduces the size of data sent, and can reduce transmission time. Content is only compressed if the ContentEncoding header does not already contain gzip or deflate. Requests containing a null content object will be left unmodified. The handler will set the ContentEncoding header to gzip on any request that it compresses the content for.
Content is compressed only when the request is sent, and the compressed output is streamed to the server. As a result, the system does not know the length of the compressed content before sending it. Therefore, in the rare case where the compressed content is larger than the original, the compressed version is still used.
Conditional logic can be applied to the handler so only some requests are compressed. Conditions should be applied so only requests containing content types that are not already compressed are processed. Not all servers support compressed request bodies, so it may be neccesary to apply conditions based on the request authority or URI.
The following sample will configure a new HttpClient which sends all requests with compressed content.
// Configure the http client
var innerHandler = new System.Net.Http.HttpClientHandler()
var handler = new Yort.Http.ClientPipeline.CompressedRequestHandler(innerHandler)
var client = new System.Net.Http.HttpClient(handler);
// Send a sample request, whose content will be compressed for us
var result = await client.PostAsync("http://www.someserver.com/SomeEndpoint",
new StringContent("This content will be compressed."));The following sample will configure a new HttpClient which compresses request content only for the requests to the 'someserver' authority where the content type is 'text/plain'.
// Setup the conditionals
var authorityCondition = new AuthorityRequestCondition();
authorityCondition.AddAuthority("someserver");
var contentMediaTypeCondition = new RequestContentMediaTypeCondition();
contentMediaTypeCondition.AddContentMediaType(MediaTypes.TextPlain);
var andCondition = new AndRequestCondition(new IRequestCondition[] { condition1, condition2 });
// Configure the http client
var innerHandler = new System.Net.Http.HttpClientHandler()
var handler = new Yort.Http.ClientPipeline.CompressedRequestHandler(innerHandler)
var client = new System.Net.Http.HttpClient(handler, andCondition);
// Send a sample request, whose content will be compressed for us
var result = await client.PostAsync("http://www.someserver.com/SomeEndpoint",
new StringContent("This content will be compressed."));
// Send a sample request, whose content will NOT be compressed for us
var result = await client.PostAsync("http://www.myotherserver.com/SomeEndpoint",
new StringContent("This content will be compressed."));