This is the fifth post on a series about the new Preview 4 of WCF Web API. The previous posts were:
- Elementary programming model
- Self-hosting, HTTPS and HTTP Basic Authentication
- IIS Hosting
In this post, I briefly present the HTTP message classes, which are at the core of the WCF Web API processing model.
- The two main classes are HttpRequestMessage, representing a HTTP request; and HttpResponseMessage, representing a HTTP response.
- Both HttpRequestMessage and HttpResponseMessage have a Content property, of type HttpContent (abstract class) that represents the message’s body. Some available concrete content classes are
- There are three different classes for message header representation
- HttpRequestHeaders – represents request headers, with properties such as Accept or Authorization;
- HttpResponseHeaders – represents response headers, with properties such as ETag or WwwAuthenticate;
- HttpContentHeaders – represents entity headers, with properties such as ContentyType or ContentLength.
- These classes are composed by a set of properties, one for each represented header. This allow for an easier access to headers (e.g. someRequest.Headers.Accept).
- Note that the entity-headers are referenced by the content class and not directly by the request or response classes.
- Note also that both the request’s Method and the response’s StatusCode are classes/enums and not just strings or integers.
- Both HttpRequestMessage and HttpResponseMessage are concrete classes with public constructors, allowing for their simple instantiation, namely in testing scenarios.
- Despite their core role in the HTTP Web API, these message classes belong to a different assembly – Microsoft.Net.Http.