Exemplo n.º 1
0
 public AsyncHttpClient Cookies(AsyncCookieContainer cookies)
 {
     if (cookies != null)
     {
         _cookieContainerBuilder = CookieContainerBuilder.Create(cookies);
     }
     return(this);
 }
Exemplo n.º 2
0
        private HttpClient DoBuildHttpClient()
        {
            var clientHandler = new HttpClientHandler
            {
                UseCookies = true
            };

            if (_cookieContainerBuilder != null)
            {
                //需要用到Uri来确定Domain,否则会出错~
                _cookieContainer        = _cookieContainerBuilder.Builder(_uri);
                _cookieContainerBuilder = null;//使用一次就不用了
            }

            if (_cookieContainer == null)
            {
                _cookieContainer = new CookieContainer();
            }
            clientHandler.CookieContainer = _cookieContainer;

            if (_automaticDecompression.HasValue)
            {
                clientHandler.AutomaticDecompression = _automaticDecompression.Value;
            }
            if (_allowAutoRedirect.HasValue)
            {
                clientHandler.AllowAutoRedirect = _allowAutoRedirect.Value;
            }
            var client = new HttpClient(clientHandler);

            if (_timeout.HasValue)
            {
                client.Timeout = _timeout.Value;
            }
            if (_expectContinue.HasValue)
            {
                client.DefaultRequestHeaders.ExpectContinue = _expectContinue.Value;
            }
            if (_headers != null)
            {
                foreach (var kv in _headers)
                {
                    client.DefaultRequestHeaders.Add(kv.Key, kv.Value);
                }
            }
            return(client);
        }
Exemplo n.º 3
0
 //这里的参数表示示是否需要释放那些实现IDisposable接口的托管对象
 protected virtual void Dispose(bool disposing)
 {
     if (_disposed)
     {
         return; //如果已经被回收,就中断执行
     }
     if (disposing)
     {
         //TODO:释放那些实现IDisposable接口的托管对象
     }
     //释放非托管资源,设置对象为null
     _headers = null;
     _cookieContainerBuilder = null;
     _automaticDecompression = null;
     _uri      = null;
     _disposed = true;
 }
Exemplo n.º 4
0
 public AsyncHttpClient Cookies(CookieContainer cookies)
 {
     _cookieContainerBuilder = CookieContainerBuilder.Create(cookies);
     return(this);
 }