http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 45763|回复: 11

[HttpHelper] 服务器提交了协议冲突. Section=ResponseStatusLine

[复制链接]
发表于 2014-9-1 19:36:32 | 显示全部楼层 |阅读模式
20金钱
    最近小弟使用在帮朋友写一个注册软件,用的是.net framework 4.0,调用get方法是返回正常(包括获取验证码图片),可在进行post的使用就出现问题。开始的时候返回的html内容为“服务器提交了协议冲突. Section=ResponseHeader Detail=CR 后面必须是 LF”,于是在app.config文件中加入了“<httpWebRequest useUnsafeHeaderParsing="true" />”,可html内容却成了“服务器提交了协议冲突. Section=ResponseStatusLine”。一天了,程序就卡在这个地方了,将代码贴出来,兄弟们给看看是不是哪个地方设置有问题。代码如下:

    基于HttpHelper的Common.cs
   
[C#] 纯文本查看 复制代码
using RKRG.Utils;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace RKRG.Utils
{
    class Common
    {
        #region 全局变量部分

        //http请求参数
        //用户的浏览器类型,版本,操作系统
        string userAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:30.0) Gecko/20100101 Firefox/30.0";
        //返回类型
        string contentType = "application/x-www-form-urlencoded";
        //是否根据301跳转
        bool allowautoredirect = false;
        //连接超时时间
        int timeout = 10000;
        //写入Post数据超时时间
        int readWriteTimeout = 100000;
        //创建Http访问类对象
        HttpHelper http = new HttpHelper();

        public CookieCollection cc_gb = new CookieCollection();

        public string location = ""; 
        
        public string htmlContent = "";

        string cookieStr = "";

        #endregion

        #region httpMethod

        /// <summary>
        /// post方法的http请求
        /// </summary>
        /// <param name="url"></param>
        /// <param name="referer"></param>
        /// <param name="postDate"></param>
        public string postMethod_HttpRquest(string url, string refer, string postData)
        {
            HttpHelper http = new HttpHelper();
            HttpItem item = new HttpItem()
            {
                URL = url,   
                Method = "post",   
                IsToLower = false,//得到的HTML代码是否转成小写     可选项默认转小写                   
                Referer = refer,//来源URL     可选项   
                Postdata = postData,//Post数据     可选项GET时不需要写   
                Timeout = timeout,//连接超时时间     可选项默认为100000 
                Encoding = Encoding.UTF8,
                ReadWriteTimeout = readWriteTimeout,//写入Post数据超时时间     可选项默认为30000   
                UserAgent = userAgent,//用户的浏览器类型,版本,操作系统     可选项有默认值   
                ContentType = contentType,//返回类型    可选项有默认值   
                Allowautoredirect = allowautoredirect,//是否根据301跳转     可选项   
                ResultCookieType = ResultCookieType.CookieCollection,
                CookieCollection = cc_gb,
                KeepAlive = false
                //Cookie = cookieStr
            };
            HttpResult result = http.GetHtml(item);
            if (result != null)
            {
                if (result.CookieCollection != null)
                {
                    for (int i = 0; i < result.CookieCollection.Count; i++)
                    {
                        cc_gb.Add(result.CookieCollection[i]);
                    }
                    cookieStr = result.CookieCollection.ToString();
                }
                //if (result.Cookie != null)
                //{
                //    cookieStr = result.Cookie;
                //}
                WebHeaderCollection header = result.Header;
                if (header != null)
                {
                    if (header.GetValues("Location") != null)
                    {
                        this.location = header.GetValues("Location")[0];
                    }
                    else
                    {
                        this.location = "";
                    }
                }
                return result.Html;
            }
            return "";
        }

        /// <summary>
        /// get方法的http请求
        /// </summary>
        /// <param name="url"></param>
        /// <param name="referer"></param>
        /// <param name="postDate"></param>
        public string getMethod_HttpRquest(string url, string refer)
        {
            HttpHelper http = new HttpHelper();
            HttpItem item = new HttpItem()
            {
                URL = url,   
                Method = "get",   
                IsToLower = false,//得到的HTML代码是否转成小写     可选项默认转小写                   
                Referer = refer,//来源URL     可选项   
                Timeout = timeout,//连接超时时间     可选项默认为100000    
                ReadWriteTimeout = readWriteTimeout,//写入Post数据超时时间     可选项默认为30000   
                UserAgent = userAgent,//用户的浏览器类型,版本,操作系统     可选项有默认值   
                ContentType = contentType,//返回类型    可选项有默认值   
                Allowautoredirect = allowautoredirect,//是否根据301跳转     可选项   
                //CerPath = "d:\123.cer",//证书绝对路径     可选项不需要证书时可以不写这个参数   
                //Connectionlimit = 1024,//最大连接数     可选项 默认为1024    
                //ProxyIp = "",//代理服务器ID     可选项 不需要代理 时可以不设置这三个参数    
                //ProxyPwd = "123456",//代理服务器密码     可选项    
                //ProxyUserName = "administrator",//代理服务器账户名     可选项   
                ResultCookieType = ResultCookieType.CookieCollection,
                CookieCollection = cc_gb,
                KeepAlive = true
                //Cookie = cookieStr
            };
            HttpResult result = http.GetHtml(item);
            if (result != null)
            {
                if (result.CookieCollection != null)
                {
                    for (int i = 0; i < result.CookieCollection.Count; i++)
                    {
                        cc_gb.Add(result.CookieCollection[i]);
                    }
                }
                //if (result.Cookie != null)
                //{
                //    cookieStr = result.Cookie;
                //}
                WebHeaderCollection header = result.Header;
                this.htmlContent = result.Html;
                if (header != null)
                {
                    if (header.GetValues("Location") != null)
                    {
                        this.location = header.GetValues("Location")[0];
                    }
                    else
                    {
                        this.location = "";
                    }
                }
                return result.Html;
            }
            return "";
        }

        /// <summary>
        /// 获取图片
        /// </summary>
        public Image getImage(string url, string referer)
        {
            HttpItem item = new HttpItem()
            {
                URL = url,
                Method = "GET",
                Accept = "image/png, image/svg+xml, image/*;q=0.8, */*;q=0.5",
                Referer = referer,//来源URL     可选项   
                Timeout = timeout,//连接超时时间     可选项默认为100000    
                ReadWriteTimeout = readWriteTimeout,//写入Post数据超时时间     可选项默认为30000   
                UserAgent = userAgent,//用户的浏览器类型,版本,操作系统     可选项有默认值   
                ContentType = contentType,//返回类型    可选项有默认值   
                Allowautoredirect = allowautoredirect,//是否根据301跳转     可选项 
                //CerPath = "d:\123.cer",//证书绝对路径     可选项不需要证书时可以不写这个参数   
                //Connectionlimit = 1024,//最大连接数     可选项 默认为1024    
                //ProxyIp = this.proxy,//代理服务器ID     可选项 不需要代理 时可以不设置这三个参数    
                //ProxyPwd = "123456",//代理服务器密码     可选项    
                //ProxyUserName = "administrator",//代理服务器账户名     可选项   
                ResultType = ResultType.Byte,
                //ResultCookieType = ResultCookieType.CookieCollection,
                //CookieCollection = cc_gb
                KeepAlive = true,
                Cookie = cookieStr
            };
            HttpResult result = http.GetHtml(item);
            //if (result.CookieCollection != null)
            //{
            //    cc_gb.Add(result.CookieCollection);
            //}
            if (result.Cookie != null)
            {
                cookieStr = result.Cookie;
            }
            MemoryStream ms = new MemoryStream(result.ResultByte);
            Image img = Image.FromStream(ms);
            //流用完要及时关闭  
            ms.Close();
            return img;  //得到验证图片

        }
        #endregion
    }
}


    配置文件app.config
[XML] 纯文本查看 复制代码
<?xml version="1.0" ?>
<configuration>
  <system.net>
    <settings>
      <httpWebRequest useUnsafeHeaderParsing="true" />
    </settings>
  </system.net>
</configuration>


    调用post的函数
[C#] 纯文本查看 复制代码
/// <summary>
        /// 进入检测页面
        /// </summary>
        /// <returns></returns>
        public bool intoCheckPage(string authCode)
        {
            string postData = "captchaText=" + authCode + "&locationCode=peki&realmId=12&categoryId=100&openingPeriodId=&date=&dateStr=&action%3Aappointment_showDay=Further";            
            string html = this.com.postMethod_HttpRquest(this.url_checkPage, this.referer, postData);
            if (html.Contains("Please select an appointment"))
            {
                return true;
            }
            return false;
        }



1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
 楼主| 发表于 2014-9-1 21:14:33 | 显示全部楼层
对了我访问的是https的网站链接,不知道这个是不是有什么不同的设置!
回复

使用道具 举报

发表于 2014-9-2 10:13:28 | 显示全部楼层
Allowautoredirect = false这个不要设置,直接设置为False或者不设置,默认也是False

Https一般没有什么特别。
回复

使用道具 举报

 楼主| 发表于 2014-9-2 10:23:47 | 显示全部楼层
站长苏飞 发表于 2014-9-2 10:13
Allowautoredirect = false这个不要设置,直接设置为False或者不设置,默认也是False

Https一般没有什 ...

我看他的网站需要证书, httpHelper可以无视证书不是么,在get的时候没问题就post的时候是这样,网站是这个https://service2.diplo.de/rkterm ... &categoryId=100
回复

使用道具 举报

发表于 2014-9-2 10:27:49 | 显示全部楼层
证书需要指定一个正确格式的,无视也不能什么都不管,路径还是要配置一下的,毕竟里面没有带证书 只是一个类
回复

使用道具 举报

 楼主| 发表于 2014-9-2 10:38:54 | 显示全部楼层
站长苏飞 发表于 2014-9-2 10:27
证书需要指定一个正确格式的,无视也不能什么都不管,路径还是要配置一下的,毕竟里面没有带证书 只是一个 ...

怎么才能知道证书的正确格式 昨天我从网站上导出了他们的证书格式是crt的 我看你的《HttpWebRequest 绝技 》中用的都是cer
回复

使用道具 举报

发表于 2014-9-2 11:00:54 | 显示全部楼层
cer的就行,直接在HttpItem里设置一下参数就行了
回复

使用道具 举报

 楼主| 发表于 2014-9-2 11:15:47 | 显示全部楼层
站长苏飞 发表于 2014-9-2 11:00
cer的就行,直接在HttpItem里设置一下参数就行了

我把post相关代码调整成了下面这样,证书是用火狐从页面属性中导出的,还是提示同样的信息,这个还可能是由什么引起的?
[C#] 纯文本查看 复制代码
        //回调验证证书问题
        public bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
        {
            // 总是接受    
            return true;
        }

        /// <summary>
        /// post方法的http请求
        /// </summary>
        /// <param name="url"></param>
        /// <param name="referer"></param>
        /// <param name="postDate"></param>
        public string postMethod_HttpRquest(string url, string refer, string postData)
        {
            //string cerPathStr = AppDomain.CurrentDomain.BaseDirectory + "GeoTrustGlobalCA.crt";
            // 这一句一定要写在创建连接的前面。使用回调的方法进行证书验证。
            ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
            HttpHelper http = new HttpHelper();
            HttpItem item = new HttpItem()
            {
                URL = url,   
                Method = "post",   
                IsToLower = false,//得到的HTML代码是否转成小写     可选项默认转小写                   
                Referer = refer,//来源URL     可选项   
                Postdata = postData,//Post数据     可选项GET时不需要写   
                Timeout = timeout,//连接超时时间     可选项默认为100000 
                Encoding = Encoding.UTF8,
                ReadWriteTimeout = readWriteTimeout,//写入Post数据超时时间     可选项默认为30000   
                UserAgent = userAgent,//用户的浏览器类型,版本,操作系统     可选项有默认值   
                ContentType = contentType,//返回类型    可选项有默认值   
                //Allowautoredirect = allowautoredirect,//是否根据301跳转     可选项 
                //CerPath = cerPathStr,//证书绝对路径     可选项不需要证书时可以不写这个参数
                ResultCookieType = ResultCookieType.CookieCollection,
                CookieCollection = cc_gb,
                KeepAlive = false
                //Cookie = cookieStr
            };
            try
            {
                item.ClentCertificates = new X509CertificateCollection();
                item.ClentCertificates.Add(new X509Certificate(AppDomain.CurrentDomain.BaseDirectory + "GeoTrustGlobalCA.cer"));
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);
            }
            HttpResult result = http.GetHtml(item);
            if (result != null)
            {
                if (result.CookieCollection != null)
                {
                    for (int i = 0; i < result.CookieCollection.Count; i++)
                    {
                        cc_gb.Add(result.CookieCollection[i]);
                    }
                    cookieStr = result.CookieCollection.ToString();
                }
                //if (result.Cookie != null)
                //{
                //    cookieStr = result.Cookie;
                //}
                WebHeaderCollection header = result.Header;
                if (header != null)
                {
                    if (header.GetValues("Location") != null)
                    {
                        this.location = header.GetValues("Location")[0];
                    }
                    else
                    {
                        this.location = "";
                    }
                }
                return result.Html;
            }
            return "";
        }
回复

使用道具 举报

发表于 2014-9-2 11:20:08 | 显示全部楼层
    //CerPath = cerPathStr,//证书绝对路径     可选项不需要证书时可以不写这个参数


直接指定就行了,不用你添加
回复

使用道具 举报

 楼主| 发表于 2014-9-2 11:25:03 | 显示全部楼层
站长苏飞 发表于 2014-9-2 11:20
//CerPath = cerPathStr,//证书绝对路径     可选项不需要证书时可以不写这个参数

我用google导出证书是提示没有安全证书,用火狐却能导出,我直接指定了证书路经还是不行,现在是不能确定是有什么引起的,
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-5-5 23:08

© 2014-2021

快速回复 返回顶部 返回列表