Skip to content

AndRequestCondition

Troy Willmot edited this page Sep 25, 2016 · 2 revisions

And Request Condition

Summary

The AndRequestCondition object is used to combine multiple existing IRequestCondition implementations together, where only requests matching ALL of the provided conditions are processed.

Samples

This sample uses the AndRequestCondition to combine the AuthorityRequestCondition
and RequestContentMediaTypeCondition. The 'and condition' is then used with the Compressed Requests Handler so only requests to www.mydomain.com with a plain text content are compressed. Requests to any other authority and/or with any other media type are not compressed.

// Construct an authority condition
var authorityCondition = new AuthorityRequestCondition();
authorityCondition.AddAuthority("www.sometestsite.com");

// Construct a media type condition
var mediaTypeCondition = new RequestContentMediaTypeCondition();
mediaTypeCondition.AddContentMediaType(MediaTypes.TextPlain);

// Create the 'and condition' requiring both previous conditions
// to be true.
var andCondition = new AndRequestCondition(new IRequestCondition[] { authorityCondition, mediaTypeCondition });

// Construct the compressed request handler with the and condition object attached.
var handler = new CompressedRequestHandler(mh, andCondition);
var client = new System.Net.Http.HttpClient(handler);

// Now send a request that will be compressed because the request 
// authority & content media type match the conditions.
var uncompressedContent = "A compressible string. A compressible string. A compressible string. A compressible string.";
var result = await client.PostAsync("http://www.mydomain.com/SomeEndPoint", 
    new System.Net.Http.StringContent
    (
        uncompressedContent, 
        System.Text.UTF8Encoding.UTF8, 
        MediaTypes.TextPlain
    )
).ConfigureAwait(false);

Clone this wiki locally