Пример #1
0
        /// <summary>
        /// 为请求准备参数
        /// </summary>
        ///<param name="item">参数列表</param>
        private void SetRequest(HttpItem item)
        {
            try
            {
                //初始化对像,并设置请求的URL地址
                request = (HttpWebRequest)WebRequest.Create(item.URL);
            }
            catch (Exception ex)
            {
                throw new Exception("创建请求错误:" + ex.Message);
            }

            try
            {
                // 验证证书
                SetCer(item);
            }
            catch (Exception ex)
            {
                throw new Exception("设置证书错误:" + ex.Message);
            }
            try
            {
                // 设置代理
                SetProxy(item);
                if (item.ProtocolVersion != null)
                {
                    request.ProtocolVersion = item.ProtocolVersion;
                }
                request.ServicePoint.Expect100Continue = item.Expect100Continue;
                //请求方式Get或者Post
                request.Method  = item.Method;
                request.Timeout = item.Timeout;

                request.ReadWriteTimeout = item.ReadWriteTimeout;
                if (item.IfModifiedSince != null)
                {
                    request.IfModifiedSince = Convert.ToDateTime(item.IfModifiedSince);
                }
                //Accept
                request.Accept = item.Accept;
                //ContentType返回类型
                if (item.ContentType != null && item.ContentType.Trim() != "")
                {
                    request.ContentType = item.ContentType;
                }
                //UserAgent客户端的访问类型,包括浏览器版本和操作系统信息
                request.UserAgent = item.UserAgent;
                // 编码
                encoding = item.Encoding;
                //设置安全凭证
                request.Credentials = item.ICredentials;

                //设置Cookie
                SetCookie(item);
                //来源地址
                if (item.Referer != null)
                {
                    request.Referer = item.Referer;
                }
                request.AllowAutoRedirect = false;
                request.Connection        = null;
                try
                {
                    foreach (string key in item.Headers.Keys)
                    {
                        try
                        {
                            request.Headers.Remove(key);
                            if (item.Headers[key] != null && item.Headers[key].Trim() != "")
                            {
                                if (key.ToLower() == "connection")
                                {
                                    request.Connection = item.Headers[key];
                                }
                                request.Headers.Add(key, item.Headers[key]);
                            }
                        }
                        catch
                        {
                        }
                    }
                }
                catch
                {
                }
            }
            catch (Exception ex)
            {
                throw new Exception("设置请求参数错误:" + ex.Message);
            }
            try
            {
                //设置Post数据
                SetPostData(item);
            }
            catch (Exception ex)
            {
                throw new Exception("与服务器通信失败:" + ex.Message);
            }
            //设置最大连接
            if (item.Connectionlimit > 0)
            {
                request.ServicePoint.ConnectionLimit = item.Connectionlimit;
            }
        }
Пример #2
0
        private HttpResult _GetContent(HttpItem item)
        {
            //返回参数
            HttpResult result = new HttpResult();

            try
            {
                //准备参数
                SetRequest(item);
            }
            catch (Exception ex)
            {
                result.Cookie            = string.Empty;
                result.Header            = null;
                result.Content           = "";
                result.StatusDescription = ex.Message;
                //配置参数时出错
                return(result);
            }
            try
            { //请求数据
                response = (HttpWebResponse)request.GetResponse();
                //获取set-cookie
                if (response.Headers["set-cookie"] != null)
                {
                    item.Cookie = response.Headers["set-cookie"];
                }
                if (item.CookieContainer != null && response.Cookies != null && response.Cookies.Count > 0)
                {
                    item.CookieContainer.Add(response.ResponseUri, response.Cookies);
                }
                int redirectcount = 0;
                while (response.StatusCode == HttpStatusCode.Found || response.StatusCode == HttpStatusCode.Moved)
                {
                    if (!item.Allowautoredirect)
                    {
                        break;
                    }
                    if (redirectcount > item.MaximumAutomaticRedirections)
                    {
                        break;
                    }
                    redirectcount++;

                    try
                    {
                        response.Close();
                    }
                    catch
                    {
                    }

                    item.URL = new Uri(new Uri(item.URL), response.Headers["Location"]).AbsoluteUri;
                    SetRequest(item);
                    response = (HttpWebResponse)request.GetResponse();
                    //获取set-cookie
                    if (response.Headers["set-cookie"] != null)
                    {
                        item.Cookie = response.Headers["set-cookie"];
                    }
                    if (item.CookieContainer != null && response.Cookies != null && response.Cookies.Count > 0)
                    {
                        item.CookieContainer.Add(response.ResponseUri, response.Cookies);
                    }
                }


                GetData(item, result);
            }
            catch (WebException ex)
            {
                if (ex.Response != null)
                {
                    using (response = (HttpWebResponse)ex.Response)
                    {
                        GetData(item, result);
                    }
                }
                else
                {
                    result.StatusDescription = ex.Message;
                }
            }
            catch (Exception ex)
            {
                result.StatusDescription = ex.Message;
            }
            finally
            {
                if (response != null)
                {
                    try
                    {
                        response.Close();
                    }
                    catch
                    {
                    }
                }
            }
            if (item.IsToLower)
            {
                result.Content = result.Content.ToLower();
            }
            return(result);
        }
Пример #3
0
        /// <summary>
        /// 设置编码
        /// </summary>
        /// <param name="item">HttpItem</param>
        /// <param name="result">HttpResult</param>
        /// <param name="ResponseByte">byte[]</param>
        private void SetEncoding(HttpItem item, HttpResult result, byte[] ResponseByte)
        {
            //是否返回Byte类型数据
            if (item.ResultType == ResultType.Byte)
            {
                result.ResultByte = ResponseByte;
            }
            //从这里开始我们要无视编码了
            if (encoding == null)
            {
                string strEnc = string.Empty;
                if (response != null && response.ContentType != null && response.ContentType.Trim() != "")
                {
                    try
                    {
                        int charsetIndex = response.ContentType.IndexOf("charset", StringComparison.OrdinalIgnoreCase);
                        if (charsetIndex != -1)
                        {
                            strEnc = response.ContentType.Substring(charsetIndex + 7);
                            strEnc = strEnc.Replace("=", "").Replace(";", "").Trim();
                        }
                        if (strEnc != "")
                        {
                            strEnc = Encoding.GetEncoding(strEnc).WebName;
                        }
                        else
                        {
                            strEnc = "";
                        }
                    }
                    catch { strEnc = string.Empty; }
                }
                if (string.IsNullOrEmpty(strEnc))
                {
                    //<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
                    //<meta charset="gbk"/>
                    //<meta charset=gbk/>
                    //<meta charset=gbk>
                    Match meta = Regex.Match(Encoding.Default.GetString(ResponseByte), "<meta[^<>]*charset[\\s]*?=[\\s]*?[\"']{0,1}(?<result>[-\\w]+)[\"'\\s/>]", RegexOptions.IgnoreCase, TimeSpan.FromMinutes(1));
                    if (meta != null && meta.Groups.Count > 0)
                    {
                        strEnc = meta.Groups["result"].Value.ToLower().Trim();
                    }
                    else
                    {
                        strEnc = "GB18030";
                    }
                }


                if (strEnc != null && strEnc.Trim() != "")
                {
                    try
                    {
                        strEnc = strEnc.Replace("\"", string.Empty).Replace("'", "").Replace(";", "").Trim();
                        if (strEnc.ToLower().Equals("iso-8859-1") || strEnc.ToLower().Equals("gbk") || strEnc.ToLower().Equals("gb2312"))
                        {
                            strEnc = "GB18030";
                        }
                        encoding = Encoding.GetEncoding(strEnc);
                    }
                    catch
                    {
                        encoding = null;
                    }
                }

                if (encoding == null)
                {
                    encoding = Encoding.UTF8;
                }
            }
        }