Events in HttpClient

Topics: Web Api
Feb 2, 2011 at 1:26 PM

The original HttpClient version supported a pipeline where you could inject some processing work. It looks like the pipeline was removed in the current bits, which I think it is fine to simplify the design but it would be great to have a place to hook up some code before the http request is sent to the server, o when the response is received for example. I am creating a prototype that uses delegated authentication for services (Think OAuth or SAML), and a thing that the client application must do before calling the services is to negotiate an authentication token. In the current bits, I solved that using a kind of factory that creates a new instance of the HttpClient and sets the token in the DefaultHeaders collection. However, it would make sense to have a way to inject that logic to negotiate the token in the HttpClient itself using an event for instance. The WCF Data Service proxy uses a similar approach, it provides an event BeforeSendRequest where you can add this kind of logic.

Thanks

Pablo.

 

Feb 2, 2011 at 5:05 PM

Pablo,

Create a new class derived from WebRequestChannel, override the Send method and then assign it to the Channel property of your HttpClient object and you will be able to intercept the request and response.

Feb 2, 2011 at 5:09 PM

Cool, I didn't know about the Channel Property. I will try that then.

Thanks
Pablo.

 

Feb 2, 2011 at 5:11 PM

Here is a channel I created that returns a fake response for testing with.

    public class FakeWebRequestChannel : WebRequestChannel {
        private readonly HttpContent _Content;

        public FakeWebRequestChannel(HttpContent content) {
            _Content = content;
        }
        protected override HttpResponseMessage Send(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) {
            var response = new HttpResponseMessage() {
                Content = _Content,
                RequestMessage = request,
                StatusCode = HttpStatusCode.OK

            };
            return response;
        }
    }
}

Coordinator
Feb 2, 2011 at 5:13 PM
Also if you look in codeplex in the queryable sample I have a tracing channel that takes an action.

Sent from my Windows Phone

From: DarrelMiller
Sent: Wednesday, February 02, 2011 9:12 AM
To: Glenn Block
Subject: Re: Events in HttpClient [wcf:244296]

From: DarrelMiller

Here is a channel I created that returns a fake response for testing with.

    public class FakeWebRequestChannel : WebRequestChannel {
        private readonly HttpContent _Content;

        public FakeWebRequestChannel(HttpContent content) {
            _Content = content;
        }
        protected override HttpResponseMessage Send(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) {
            var response = new HttpResponseMessage() {
                Content = _Content,
                RequestMessage = request,
                StatusCode = HttpStatusCode.OK

            };
            return response;
        }
    }
}