public ClientRequestLogEventArgs(WebClientRequestMessage request, WebClientResponseMessage response,
                                  Exception thrownException)
 {
     if (request == null) throw new ArgumentNullException("request");
     this.request = request;
     this.response = response;
     this.thrownException = thrownException;
 }
        public WebClientResponseMessage Send(WebClientRequestMessage request)
        {
            Func<string, Action<BrowserContext>, BrowserResponse> browserMethod;

            switch (request.Method.ToUpper())
            {
                case "POST":
                    browserMethod = browser.Post;
                    break;
                case "PATCH":
                    browserMethod = browser.Patch;
                    break;
                case "GET":
                    browserMethod = browser.Get;
                    break;
                default:
                    throw new NotImplementedException();
            }

            var uri = new Uri(request.Uri);
            var creds = Credentials;

            var browserResponse = browserMethod(request.Uri, bc =>
                {
                    bc.HttpRequest();
                    if (creds != null)
                    {
                        bc.BasicAuth(creds.UserName, creds.Password);
                    }
                    ((IBrowserContextValues) bc).QueryString = uri.Query;
                    foreach (var kvp in request.Headers)
                    {
                        foreach (var v in kvp.Value)
                            bc.Header(kvp.Key, v);
                    }
                    if (request.Data != null)
                    {
                        bc.Body(new MemoryStream(request.Data));
                    }
                });

            var responseHeaders = new HttpHeaders(
                browserResponse
                .Headers
                .Select(x => new KeyValuePair<string, IEnumerable<string>>(x.Key, x.Value.WrapAsEnumerable())));

            if (browserResponse.Context.Response != null &&
                (!string.IsNullOrEmpty(browserResponse.Context.Response.ContentType)))
            {
                responseHeaders.Add("Content-Type", browserResponse.Context.Response.ContentType);
            }

            return new WebClientResponseMessage(request.Uri, browserResponse.Body.ToArray(),
                                                (HttpStatusCode) browserResponse.StatusCode,
                                                responseHeaders,
                                                "1.1");
        }
        public WebClientResponseMessage Send(WebClientRequestMessage request)
        {
            var webRequest = (HttpWebRequest)WebRequest.Create(request.Uri);

            if (Credentials != null)
            {
                webRequest.Credentials = Credentials;
                webRequest.PreAuthenticate = true;
            }

            foreach (var h in request.Headers.Concat(defaultHeaders.EmptyIfNull()))
            {
                if (h.Value.Count == 0)
                    continue;

                if (!WebHeaderCollection.IsRestricted(h.Key))
                {
                    foreach (var v in h.Value)
                    {
                        webRequest.Headers.Add(h.Key, v);
                    }
                }
                else
                {
                    switch (h.Key)
                    {
                        case "Accept":
                            webRequest.Accept = h.Value.Last();
                            break;

                        case "Content-Type":
                            webRequest.ContentType = h.Value.Last();
                            break;

                        default:
                            throw new NotImplementedException("Setting restricted header " + h.Key + " not implemented.");
                    }
                }
            }

            webRequest.ProtocolVersion = Version.Parse(request.ProtocolVersion);
            webRequest.Method = request.Method;

            if (request.Data != null)
            {
                webRequest.ContentLength = request.Data.Length;
                using (var requestStream = webRequest.GetRequestStream())
                {
                    requestStream.Write(request.Data, 0, request.Data.Length);
                }
            }

            Exception innerException;
            using (var webResponse = GetResponseNoThrow(webRequest, out innerException))
            using (var responseStream = webResponse.GetResponseStream())
            {
                var responseBytes = responseStream.ReadAllBytes();

                var responseUri = webResponse.ResponseUri ?? webRequest.RequestUri;
                var protocolVersion = webResponse.ProtocolVersion ?? new Version(1, 1);

                return new WebClientResponseMessage(responseUri.ToString(), responseBytes,
                                                    (HttpStatusCode)webResponse.StatusCode,
                                                    new HttpHeaders(ConvertHeaders(webResponse.Headers)),
                                                    protocolVersion.ToString());
            }
        }