Ejemplo n.º 1
0
        public WebNetObj(CookieContainer cliCookie = null, WebRequestMethodEnum method = WebRequestMethodEnum.POST, Encoding encoding = null)
        {
            if (encoding == null)
            {
                encoding = Encoding.GetEncoding("GB2312");
            }

            _encoding = encoding;

            clientCookie = cliCookie;

            _cookies = new List <Cookie>();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// post方法发送数据
        /// </summary>
        /// <param name="url"></param>
        /// <param name="data"></param>
        /// <param name="saveCookie">是否保存cookie</param>
        /// <param name="getContent">是否读取内容</param>
        /// <returns></returns>
        public HttpResponseEx DoRequest(string url, string data, WebRequestMethodEnum method = WebRequestMethodEnum.GET, bool saveCookie = true, bool getContent = true,
                                        string contentType = "application/x-www-form-urlencoded;charset=UTF-8;")
        {
            byte[] buff = null;

            if (!string.IsNullOrEmpty(data))
            {
                buff = this.WebEncoding.GetBytes(data);
            }

            var response = DoRequest(url, buff, method, saveCookie, getContent, contentType);

            if (!response.Successed)
            {
                return(response);
            }

            try
            {
                WebEncoding = Encoding.GetEncoding(response.CharacterSet);
            }
            catch
            {
                WebEncoding = DefaultEncoding;
            }

            if (response.ResponseBytes != null && response.ResponseBytes.Length > 0)
            {
                response.ResponseContent = WebEncoding.GetString(response.ResponseBytes);
                if (SupportCompression)
                {
                    var htmlEncoding = GetHTMLMetaEncoding(response.ResponseContent);
                    if (htmlEncoding != null && htmlEncoding != WebEncoding)
                    {
                        response.ResponseContent = htmlEncoding.GetString(response.ResponseBytes);
                        WebEncoding = htmlEncoding;
                    }
                }

                response.ResponseBytes = null;
            }
            return(response);
        }
Ejemplo n.º 3
0
        ///// <summary>
        ///// post方法发送数据
        ///// </summary>
        ///// <param name="url"></param>
        ///// <param name="data"></param>
        ///// <param name="saveCookie">是否保存cookie</param>
        ///// <param name="getContent">是否读取内容</param>
        ///// <returns></returns>
        //public HttpResponseEx DoRequest(string url, string data, WebRequestMethodEnum method = WebRequestMethodEnum.GET, bool saveCookie = true, bool getContent = true,
        //    string contentType = "application/x-www-form-urlencoded;charset=UTF-8;")
        //{
        //    var ret = new HttpResponseEx();
        //    try
        //    {
        //        if (!CheckIsInDomain(url))
        //            throw new Exception(string.Format("拒绝访问,地址{0}不在可访问的域名之列。", url));

        //        string result = string.Empty;

        //        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
        //        if (data != null && data.Length > 0)
        //            webRequest.Method = WebRequestMethodEnum.POST.ToString();

        //        webRequest.Accept = Accept;
        //        webRequest.AllowAutoRedirect = true;
        //        webRequest.Headers.Add(HttpRequestHeader.AcceptLanguage, this.acceptLanguage);
        //        webRequest.KeepAlive = true;
        //        webRequest.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);
        //        webRequest.UserAgent = UserAgent;
        //        if (SupportCompression)
        //        {
        //            //webRequest.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate");
        //            webRequest.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip");
        //        }
        //        if (!string.IsNullOrEmpty(Referer))
        //            webRequest.Referer = Referer;

        //        NetworkCredential credential = GetCredential();
        //        if (credential != null)
        //            webRequest.Proxy.Credentials = credential;

        //        if (serverCookie != null)
        //            webRequest.CookieContainer = serverCookie;
        //        else
        //            webRequest.CookieContainer = new CookieContainer();

        //        if (_certificate != null)
        //        {
        //            webRequest.UserAgent = "Client Cert Sample";
        //            webRequest.ClientCertificates.Add(_certificate);
        //        }

        //        if (!string.IsNullOrWhiteSpace(data))
        //        {
        //            byte[] buff = this.WebEncoding.GetBytes(data);
        //            //webRequest.ContentType = "application/x-www-form-urlencoded;charset=UTF-8;";
        //            webRequest.ContentType = contentType;
        //            webRequest.ContentLength = buff.Length;

        //            using (Stream requestStream = webRequest.GetRequestStream())
        //            {
        //                requestStream.Write(buff, 0, buff.Length);
        //            }
        //        }

        //        using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
        //        {
        //            if (webResponse.StatusCode == HttpStatusCode.OK)
        //            {
        //                ret.PraseHeader(webResponse);

        //                if (getContent)
        //                {
        //                    byte[] contentBuffer = null;
        //                    using (MemoryStream ms = new MemoryStream())
        //                    {
        //                        using (Stream s = webResponse.GetResponseStream())
        //                        {

        //                            byte[] buffer = new byte[2048];
        //                            int len = 0;
        //                            while ((len = s.Read(buffer, 0, 1024)) > 0)
        //                            {
        //                                ms.Write(buffer, 0, len);
        //                            }
        //                        }

        //                        if (webResponse.ContentEncoding.Equals("gzip", StringComparison.OrdinalIgnoreCase))
        //                        {
        //                            contentBuffer = Comm.GZip.Decompress(ms.ToArray());
        //                        }
        //                        else
        //                        {
        //                            contentBuffer = ms.ToArray();
        //                        }

        //                        try
        //                        {

        //                            WebEncoding = Encoding.GetEncoding(webResponse.CharacterSet);
        //                        }
        //                        catch
        //                        {
        //                            WebEncoding = DefaultEncoding;
        //                        }
        //                    }

        //                    result = WebEncoding.GetString(contentBuffer);


        //                    if (SupportCompression)
        //                    {
        //                        var htmlEncoding = GetHTMLMetaEncoding(result);
        //                        if (htmlEncoding != null && htmlEncoding != WebEncoding)
        //                        {
        //                            result = htmlEncoding.GetString(contentBuffer);
        //                            WebEncoding = htmlEncoding;
        //                        }
        //                    }
        //                }

        //                if (saveCookie && webResponse.Cookies.Count > 0)
        //                {
        //                    if (serverCookie == null)
        //                        serverCookie = new CookieContainer();
        //                    serverCookie.PerDomainCapacity = 255;
        //                    for (int i = 0; i < webResponse.Cookies.Count; i++)
        //                    {
        //                        if (!_cookies.Exists(p => p.Name.Equals(webResponse.Cookies[i].Name)))
        //                        {
        //                            _cookies.Add(webResponse.Cookies[i]);
        //                            serverCookie.Add(webResponse.Cookies[i]);
        //                        }
        //                        else
        //                        {
        //                            _cookies.RemoveAll(p => p.Name.Equals(webResponse.Cookies[i].Name));
        //                            _cookies.Add(webResponse.Cookies[i]);
        //                            serverCookie.Add(webResponse.Cookies[i]);
        //                        }
        //                    }
        //                }
        //            }
        //        }

        //        ret.ResponseContent = result;
        //        ret.Successed = true;
        //    }
        //    catch (Exception ex)
        //    {
        //        ret.ResponseContent = string.Empty;
        //        ret.Successed = false;
        //        ret.ErrorMsg = ex;
        //    }
        //    return ret;
        //}

        /// <summary>
        /// post方法发送数据
        /// </summary>
        /// <param name="url"></param>
        /// <param name="data"></param>
        /// <param name="saveCookie">是否保存cookie</param>
        /// <param name="getContent">是否读取内容</param>
        /// <returns></returns>
        public HttpResponseEx DoRequest(string url, byte[] buff, WebRequestMethodEnum method = WebRequestMethodEnum.GET, bool saveCookie = true, bool getContent = true,
                                        string contentType = "application/x-www-form-urlencoded;charset=UTF-8;")
        {
            var ret = new HttpResponseEx();

            ret.RequestMills = Environment.TickCount & Int32.MaxValue;

            try
            {
                if (!CheckIsInDomain(url))
                {
                    throw new Exception(string.Format("拒绝访问,地址{0}不在可访问的域名之列。", url));
                }

                //string result = string.Empty;

                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);

                if (buff != null && buff.Length > 0 && method == WebRequestMethodEnum.GET)
                {
                    webRequest.Method = WebRequestMethodEnum.POST.ToString();
                }
                else
                {
                    webRequest.Method = method.ToString();
                }

                webRequest.Accept            = Accept;
                webRequest.AllowAutoRedirect = true;
                webRequest.Headers.Add(HttpRequestHeader.AcceptLanguage, this.acceptLanguage);
                webRequest.KeepAlive   = true;
                webRequest.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);
                webRequest.UserAgent   = UserAgent;
                if (_timeOut > 0)
                {
                    webRequest.Timeout = _timeOut;
                }
                if (SupportCompression)
                {
                    //webRequest.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate");
                    webRequest.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip");
                }
                foreach (var kv in Headers)
                {
                    webRequest.Headers.Add(kv.Key, kv.Value);
                }
                if (!string.IsNullOrEmpty(Referer))
                {
                    webRequest.Referer = Referer;
                }

                NetworkCredential credential = GetCredential();
                if (credential != null)
                {
                    webRequest.Proxy.Credentials = credential;
                }

                if (serverCookie != null)
                {
                    webRequest.CookieContainer = serverCookie;
                }
                else
                {
                    webRequest.CookieContainer = new CookieContainer();
                }


                if (buff != null && buff.Length > 0)
                {
                    //byte[] buff = this.WebEncoding.GetBytes(data);
                    //webRequest.ContentType = "application/x-www-form-urlencoded;charset=UTF-8;";
                    webRequest.ContentType   = contentType;
                    webRequest.ContentLength = buff.Length;

                    using (Stream requestStream = webRequest.GetRequestStream())
                    {
                        requestStream.Write(buff, 0, buff.Length);
                    }
                }

                using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
                {
                    //int statusCode = (int)webResponse.StatusCode;
                    //if (statusCode >= 200 && statusCode < 400)
                    {
                        ret.PraseHeader(webResponse);

                        if (getContent)
                        {
                            byte[] contentBuffer = null;
                            using (MemoryStream ms = new MemoryStream())
                            {
                                using (Stream s = webResponse.GetResponseStream())
                                {
                                    byte[] buffer = new byte[2048];
                                    int    len    = 0;
                                    while ((len = s.Read(buffer, 0, 1024)) > 0)
                                    {
                                        ms.Write(buffer, 0, len);
                                    }
                                }

                                if (webResponse.ContentEncoding.Equals("gzip", StringComparison.OrdinalIgnoreCase))
                                {
                                    contentBuffer = Comm.GZip.Decompress(ms.ToArray());
                                }
                                else
                                {
                                    contentBuffer = ms.ToArray();
                                }

                                ret.CharacterSet = webResponse.CharacterSet;

                                ret.ResponseBytes = contentBuffer;
                            }
                        }

                        if (saveCookie && webResponse.Cookies.Count > 0)
                        {
                            if (serverCookie == null)
                            {
                                serverCookie = new CookieContainer();
                            }
                            serverCookie.PerDomainCapacity = 255;
                            for (int i = 0; i < webResponse.Cookies.Count; i++)
                            {
                                if (!_cookies.Exists(p => p.Name.Equals(webResponse.Cookies[i].Name)))
                                {
                                    _cookies.Add(webResponse.Cookies[i]);
                                    serverCookie.Add(webResponse.Cookies[i]);
                                }
                                else
                                {
                                    _cookies.RemoveAll(p => p.Name.Equals(webResponse.Cookies[i].Name));
                                    _cookies.Add(webResponse.Cookies[i]);
                                    serverCookie.Add(webResponse.Cookies[i]);
                                }
                            }
                        }
                    }
                }

                ret.Successed = true;
            }
            catch (WebException ex)
            {
                ret.PraseHeader((HttpWebResponse)ex.Response);
                ret.Successed       = false;
                ret.ErrorMsg        = ex;
                ret.ResponseContent = string.Empty;
            }
            catch (Exception ex)
            {
                ret.ResponseContent = string.Empty;
                ret.Successed       = false;
                ret.ErrorMsg        = ex;
            }

            ret.RequestMills = Environment.TickCount & Int32.MaxValue - ret.RequestMills;
            return(ret);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 取网页源码
        /// </summary>
        /// <returns></returns>
        public string GetWebContent(string url, string data, WebRequestMethodEnum method = WebRequestMethodEnum.GET)
        {
            string result = string.Empty;
            //string requestUrl=CombURL(_baseUrl, url);

            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);

            if (!string.IsNullOrWhiteSpace(data) && method == WebRequestMethodEnum.GET)
            {
                webRequest.Method = WebRequestMethodEnum.POST.ToString();
            }
            else
            {
                webRequest.Method = method.ToString();
            }

            webRequest.Accept            = Accept;
            webRequest.AllowAutoRedirect = true;
            webRequest.KeepAlive         = true;
            webRequest.CachePolicy       = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);
            webRequest.UserAgent         = UserAgent;
            if (!string.IsNullOrEmpty(Referer))
            {
                webRequest.Referer = Referer;
            }


            NetworkCredential credential = GetCredential();

            if (credential != null)
            {
                webRequest.Proxy.Credentials = credential;
            }

            if (clientCookie != null)
            {
                webRequest.CookieContainer = clientCookie;
            }
            else
            {
                webRequest.CookieContainer = new CookieContainer();
            }

            if (!string.IsNullOrWhiteSpace(data))
            {
                byte[] buff = this.WebEncoding.GetBytes(data);
                webRequest.ContentType   = "application/x-www-form-urlencoded";
                webRequest.ContentLength = buff.Length;

                using (Stream requestStream = webRequest.GetRequestStream())
                {
                    requestStream.Write(buff, 0, buff.Length);
                }
            }

            using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
            {
                if (webResponse.StatusCode == HttpStatusCode.OK)
                {
                    using (Stream s = webResponse.GetResponseStream())
                    {
                        using (StreamReader sr = new StreamReader(s, WebEncoding))
                        {
                            result = sr.ReadToEnd();
                        }
                    }

                    if (webResponse.Cookies.Count > 0)
                    {
                        if (clientCookie == null)
                        {
                            clientCookie = new CookieContainer();
                        }
                        for (int i = 0; i < webResponse.Cookies.Count; i++)
                        {
                            //serverCookie.Add(webResponse.Cookies[i]);
                            if (!_cookies.Exists(p => p.Name.Equals(webResponse.Cookies[i].Name)))
                            {
                                _cookies.Add(webResponse.Cookies[i]);
                                clientCookie.Add(webResponse.Cookies[i]);
                            }
                            else
                            {
                                _cookies.RemoveAll(p => p.Name.Equals(webResponse.Cookies[i].Name));
                                clientCookie.Add(webResponse.Cookies[i]);
                            }
                        }
                    }
                }
            }


            return(result);
        }