Beispiel #1
0
        /// <summary>
        /// <see cref="HttpClientHandler"/>'s GetRequestStreamCallback uses an
        /// overload of EndGetRequestStream that <see cref="HttpWebRequestWrapperRecorder"/>
        /// can't intercept (method isn't virtual). So we need to intercept the call
        /// and force using <see cref="HttpWebRequestWrapperRecorder.EndGetRequestStream(System.IAsyncResult)"/>
        /// (which is intercepted)
        /// </summary>
        private static void CustomGetRequestStreamCallback(IAsyncResult ar, HttpClientHandler httpClientHandler)
        {
            // build a wrapper around the Request State stored in AsyncState
            var requestStateWrapper = new HttpClientHandlerRequestStateWrapper(ar.AsyncState);

            // get the HttpRequestMessage we've intercepted
            var requestMessage = requestStateWrapper.GetHttpRequestMessage();

            // load the HttpWebRequest we've already intercepted and replaced
            var httpWebRequest = requestStateWrapper.GetHttpWebRequest();

            // get a copy of the request streams
            var requestStream = httpWebRequest.EndGetRequestStream(ar);

            // copy the request message content to the request stream
            requestMessage.Content.CopyToAsync(requestStream).Wait();

            // save the request stream to the request state
            requestStateWrapper.SetRequestStream(requestStream);

            // continue on with StartGettingResponse
            httpClientHandler.StartGettingResponse(ar.AsyncState);
        }