HttpClient类实例化的时候内部会创建一个HttpBaseProtocolFilter的对象,用于建立TCP连接,接收返回字节,管理缓存,cookie,证书等:
public sealed class HttpBaseProtocolFilter : IHttpFilter, IDisposable {
public HttpBaseProtocolFilter();
// Gets an object allowing you to set cache read and write behavior
public HttpCacheControl CacheControl { get; }
// Returns the cookie manager allowing you to get and set cookies
public HttpCookieManager CookieManager { get; }
// Allows you to set the maximum connections from filter to an HTP server
public UInt32 MaxConnectionsPerServer { get; set; }
// Can filter follow a redirect response (default = true)
public Boolean AllowAutoRedirect { get; set; }
// Can filter automatically decompress response data (default = true)
public Boolean AutomaticDecompression { get; set; }
// Ignorable SSL errors
public IList<ChainValidationResult> IgnorableServerCertificateErrors { get; }
// Can filter use a proxy to send request
public Boolean UseProxy { get; set; }
// Sets HTP proxy credentials
public PasswordCredential ProxyCredential { get; set; }
// Sets client certificate to send to server (if requested)
public Certificate ClientCertificate { get; set; }
// Sets HTP server credentials
public PasswordCredential ServerCredential { get; set; }
// Can filter prompt for user credentials at server's request (default = true)
public Boolean AllowUI { get; set; }
// Sends HTP request over the wire (SendRequestAsync is IHttpFilter's only member)
public IAsyncOperationWithProgress<HttpResponseMessage, HttpProgress>
SendRequestAsync(HttpRequestMessage request);
// Releases unmanaged resources associated with the HttpBaseProtocolFilter
public void Dispose();
}
检查返回的cookie:
using (HttpBaseProtocolFilter filter = new HttpBaseProtocolFilter())
using (HttpClient client = new HttpClient(filter)) {
HttpRequestMessage request =
new HttpRequestMessage(HttpMethod.Get, new Uri("http://Bing.com/"));
HttpResponseMessage response = await client.SendRequestAsync(request);
// See the returned cookies (HttpCookieManager also has SetCookie and DeleteCookie methods)
foreach (HttpCookie cookie in filter.CookieManager.GetCookies(request.RequestUri)) {
String cookieInfo = String.Format(
"Domain={0}, Expires={1}, HttpOnly={2}, Name={3}, Path={4}, Secure={5}, Value={6}",
cookie.Domain, cookie.Expires, cookie.HttpOnly, cookie.Name, cookie.Path,
cookie.Secure, cookie.Value);
}
}