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

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

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

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

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

查看: 13417|回复: 4

[其他] Post提交关于cookie的设置问题

[复制链接]
发表于 2013-5-17 18:35:39 | 显示全部楼层 |阅读模式
是这样的 ,我写了一个程序  是先登录 然后签到 。签到的时候需要把登录获取到的cookie设置到请求头才能提交的。request.CookieContainer = HttpResult.Request_Cookie;//设置发送Cookie   HttpResult.Request_Cookie包含了登录时的cookie
我登录成功了 但是签到的时候  一直返回信息说要我先登录。。。。然后我网上也搜索了一些问题  http://blog.csdn.net/smeller/article/details/7079726这个网页说使用到HttpWebRequest,在对子域名访问的时候,发现HttpWebRequest并不会提交与他域名不同的cookie值,其实这也是正确,毕竟只需要发送本域cookie就是了,不过HttpWebRequest不会判断是否是主域名的cookie值。 也不知道说的对不对   求解释


1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2013-5-17 20:29:19 | 显示全部楼层
和这个没有关系,还是你Cookie的问题,你发下代码吧,不看代码谁也不清楚是怎么回事
 楼主| 发表于 2013-5-17 21:04:22 | 显示全部楼层
站长苏飞 发表于 2013-5-17 20:29
和这个没有关系,还是你Cookie的问题,你发下代码吧,不看代码谁也不清楚是怎么回事

file:///C:\Documents and Settings\adimin\Application Data\Tencent\Users\1413902773\QQ\WinTemp\RichOle\O98%D}$39UF(ZGDZ63JGDA0.jpgprivate string Url = "http://www.37wan.com/";
        private string PictureUrl = "http://passport.7fgame.com/ValidateCode.aspx?94737654";
        HttpHelper httphelper = new HttpHelper();
        HttpResult httpresult = new HttpResult();
        HttpResult httpresult1 = new HttpResult();
        private string PostString = string.Empty;

        public Form1()
        {
            InitializeComponent();
        }


        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            httphelper.GetHtml(Url, null, null, -1, null, null, null, true, true, ref httpresult);//打开网站
            PostString = "login_account="+textBox1.Text+"&password=" +textBox2.Text +"&remember_me=false" ;
            httphelper.GetHtml("http://lhzs.37wan.com/login.php?action=login", "POST", null, -1, null, PostString, null, true, true, ref httpresult);//post登录
            richTextBox1.Text = httpresult.ResultHtml;//返回html显示登录成功

            httpresult1.Request_Cookie.Add(httpresult.Response_Cookie);//httpresult.Response_Cookie包含了所有的cookie

            PostString = "";

            httphelper.GetHtml("http://lhzs.37wan.com/login.php?action=to_sign", "POST", null, -1, null, PostString, null, true, true, ref httpresult1);//post签到

            richTextBox1.Text = httpresult1.ResultHtml;//返回html显示  请先登录  我怀疑没有带上cookie

下面是我自己写的 获取html部分代码
if (request.Method == "POST")
                    {
                        ASCIIEncoding encoding = new ASCIIEncoding();//设置提交信息长度和设置提交内容

                        byte[] byte1 = encoding.GetBytes(InputData);

                        request.ContentLength = byte1.Length;

                        request.GetRequestStream().Write(byte1, 0, byte1.Length);

                    }
                    request.CookieContainer = HttpResult.Request_Cookie;//设置发送Cookie

                    HttpResult.HeadersSent = request.Headers;//获取请求体

                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();//提交请求

                    HttpResult.HeadersReceived = response.Headers;//获取响应体

                    Stream stream = response.GetResponseStream();


                    StreamReader streamRead = new StreamReader(stream);

                    HttpResult.ResultHtml = streamRead.ReadToEnd();

                    HttpResult.Request_Cookie = request.CookieContainer;//请求体Cookie

                    HttpResult.All_Cookie = request.CookieContainer.GetCookies(HttpResult.Uri); //所有Cookie

                    HttpResult.Response_Cookie = response.Cookies;//响应体Cookie

                    stream.Close();

                    response.Close();

 楼主| 发表于 2013-5-17 21:06:13 | 显示全部楼层
站长苏飞 发表于 2013-5-17 20:29
和这个没有关系,还是你Cookie的问题,你发下代码吧,不看代码谁也不清楚是怎么回事

  private string Url = "http://www.37wan.com/";
        private string PictureUrl = "http://passport.7fgame.com/ValidateCode.aspx?94737654";
        HttpHelper httphelper = new HttpHelper();
        HttpResult httpresult = new HttpResult();
        HttpResult httpresult1 = new HttpResult();
        private string PostString = string.Empty;

        public Form1()
        {
            InitializeComponent();
        }


        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            httphelper.GetHtml(Url, null, null, -1, null, null, null, true, true, ref httpresult);//打开网站
            PostString = "login_account="+textBox1.Text+"&password=" +textBox2.Text +"&remember_me=false" ;
            httphelper.GetHtml("http://lhzs.37wan.com/login.php?action=login", "POST", null, -1, null, PostString, null, true, true, ref httpresult);//post登录
            richTextBox1.Text = httpresult.ResultHtml;//返回html显示登录成功

            httpresult1.Request_Cookie.Add(httpresult.Response_Cookie);//httpresult.Response_Cookie包含了所有的cookie

            PostString = "";

            httphelper.GetHtml("http://lhzs.37wan.com/login.php?action=to_sign", "POST", null, -1, null, PostString, null, true, true, ref httpresult1);//post签到

            richTextBox1.Text = httpresult1.ResultHtml;//返回html显示  请先登录  我怀疑没有带上cookie

发表于 2013-5-17 21:07:59 | 显示全部楼层
这老版本的Httphelper类,我真不清楚里面是怎么实现的了,记不清了,建议你去换成最新版的,只要带一个Cookie应该就行了。
http://www.sufeinet.com/thread-3-1-1.html这老版的我忘记是怎么实现的了,可能和我的实现有关系你这版太老了,你可以使用下我最新版的,里面有教程,怎么带Cookie都有说明,建议楼主使用最新版的试试
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

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

GMT+8, 2024-5-2 16:07

© 2014-2021

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