12
是否必须在请求之间处理HttpClient和HttpClientHandler?
.NET Framework 4.5中的System.Net.Http.HttpClient和System.Net.Http.HttpClientHandler实现IDisposable(通过System.Net.Http.HttpMessageInvoker)。 该using声明文件说: 通常,使用IDisposable对象时,应在using语句中声明并实例化它。 此答案使用以下模式: var baseAddress = new Uri("http://example.com"); var cookieContainer = new CookieContainer(); using (var handler = new HttpClientHandler() { CookieContainer = cookieContainer }) using (var client = new HttpClient(handler) { BaseAddress = baseAddress }) { var content = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("foo", "bar"), new …