Beispiel #1
0
        public static MyWebClient createWebClient(CookieContainer ccntr=null, string referer=null)
        {
            MyWebClient wbclnt = new MyWebClient();

            // Header 字段可以在发出请求报文后丢失,因此必须重新设置
            wbclnt.Headers.Add(HttpRequestHeader.ContentType, "application/x-www-form-urlencoded");
            wbclnt.Headers.Add(HttpRequestHeader.Accept, "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
            wbclnt.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate");
            wbclnt.Headers.Add(HttpRequestHeader.AcceptLanguage, "zh-CN,zh;q=0.8,en;q=0.6,zh-TW;q=0.4");
            wbclnt.Headers.Add(HttpRequestHeader.Referer, referer);
            wbclnt.Encoding = new UTF8Encoding();

            if (ccntr != null)
            {
                /// wbclnt.Headers.Add(HttpRequestHeader.Cookie, cookie);
                wbclnt.CookieContainer = ccntr;
                _cookie = ccntr.GetCookieHeader(new Uri(referer));
                LogHelper.info("create webclient with cookie : " + _cookie);
            }

            if (referer != null) _referer = referer;

            return wbclnt;
        }
Beispiel #2
0
        public MyWebClient getWebClient(CookieContainer ccntr = null, string referer = null)
        {
            if (_wc == null)
            {
                _wc = new MyWebClient();
            }
            else // Update cookie
            {
                if (_wc.ResponseHeaders != null && _wc.ResponseHeaders.AllKeys.Contains("Set-Cookie"))
                {
                    _cookie = _wc.ResponseHeaders["Set-Cookie"];
                    if (_cookie != null)
                    {
                        _wc.Headers.Add(HttpRequestHeader.Cookie, _cookie);
                        LogHelper.info("getWebClient : _cookie - " + _cookie);
                    }
                }
            }

            if (referer != null) _referer = referer;

            // Header 字段可以在发出请求报文后丢失,因此必须重新设置
            _wc.Headers.Clear();
            _wc.Headers.Add(HttpRequestHeader.ContentType, "application/x-www-form-urlencoded");
            _wc.Headers.Add(HttpRequestHeader.Accept, "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
            _wc.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate");
            _wc.Headers.Add(HttpRequestHeader.AcceptLanguage, "zh-CN,zh;q=0.8,en;q=0.6,zh-TW;q=0.4");
            _wc.Headers.Add(HttpRequestHeader.Referer, referer);
            _wc.Encoding = new UTF8Encoding();

            return _wc;
        }