/// <summary> /// Instantiates a <see cref="DocumentLoader"/> /// </summary> public DocumentLoader() { HttpClient = new HttpClient(); // add accept headers foreach (var acceptHeaderVal in Accept.Select(MediaTypeWithQualityHeaderValue.Parse)) { HttpClient.DefaultRequestHeaders.Accept.Add(acceptHeaderVal); } }
private WebRequest CreateRequest(string uri, out MediaType?contentType, out object content) { bool methodOverride; if (Method == HttpMethod.Get && uri.Length > MaxGetUriLength) { methodOverride = true; contentType = MediaType.Text; content = uri; uri = new SDataUri(uri) { Query = null }.ToString(); } else { methodOverride = (UseHttpMethodOverride && Method != HttpMethod.Get && Method != HttpMethod.Post); contentType = ContentType; content = Content; } var request = WebRequest.Create(uri); if (methodOverride) { request.Method = "POST"; request.Headers["X-HTTP-Method-Override"] = Method.ToString().ToUpperInvariant(); } else { request.Method = Method.ToString().ToUpperInvariant(); } #if PCL try { var preAuthProp = request.GetType().GetProperty("PreAuthenticate"); if (preAuthProp != null && preAuthProp.CanWrite) { preAuthProp.SetValue(request, true, null); } } catch { } #endif #if !PCL && !NETFX_CORE && !SILVERLIGHT request.Timeout = Timeout; request.PreAuthenticate = true; #endif #if !PCL && !SILVERLIGHT if (_proxySet) { request.Proxy = Proxy; } #endif var httpRequest = request as HttpWebRequest; if (httpRequest != null) { #if !PCL && !NETFX_CORE && !SILVERLIGHT httpRequest.AllowAutoRedirect = false; httpRequest.ReadWriteTimeout = Timeout; httpRequest.KeepAlive = false; httpRequest.ProtocolVersion = HttpVersion.Version10; if (UserAgent != null) { httpRequest.UserAgent = UserAgent; } #endif if (Accept != null) { httpRequest.Accept = string.Join(",", Accept.Select(MediaTypeNames.GetMediaType).ToArray()); } if (Cookies != null) { httpRequest.CookieContainer = Cookies; } } if (AcceptLanguage != null) { request.Headers[HttpRequestHeader.AcceptLanguage] = AcceptLanguage; } if (Authenticator != null) { Authenticator.Authenticate(request); } else if (Credentials != null) { request.Credentials = Credentials; } else if (UserName != null || Password != null) { request.Credentials = new NetworkCredential(UserName, Password); } else { request.UseDefaultCredentials = true; } if (ETag != null) { var header = Method == HttpMethod.Get ? HttpRequestHeader.IfNoneMatch : HttpRequestHeader.IfMatch; request.Headers[header] = ETag; } return(request); }