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

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

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

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

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

查看: 4740|回复: 2

[其他] 谁能帮忙看看,为什么这个登录验证获取的cookies不能再次访问啊

[复制链接]
发表于 2015-12-17 10:19:13 | 显示全部楼层 |阅读模式
本帖最后由 yi_shu 于 2015-12-17 10:22 编辑

第一次提交用户名密码后获取的 cookie好像是正确的,但是第二次提交的时候,却不能返回正常页面,而是空白页,why?  
[C#] 纯文本查看 复制代码
private void WorkJisilu()
        {
            HttpHelper httpPost = new HttpHelper();
            HttpItem itempost = new HttpItem
            {
                URL = "http://www.jisilu.cn/account/ajax/login_process/",
                Method = "post",
                Host = "www.jisilu.cn",
                Accept = "application/json, text/javascript, */*; q=0.01",
                UserAgent = " Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 BIDUBrowser/8.1 Safari/537.36",
                ContentType = "application/x-www-form-urlencoded; charset=UTF-8",
                Referer="http://www.jisilu.cn/login/",
                Postdata = "return_url=http%3A%2F%2Fwww.jisilu.cn%2F&user_name=[color=red]username[/color]&password=[color=red]userpass[/color]&net_auto_login=1&_post_type=ajax",
            };
//上面红字改为自己的用户名和密码
            HttpResult resultPost = httpPost.GetHtml(itempost);
            string htmlPost = resultPost.Html;
            string cookies = resultPost.Cookie;
            
            richTextBox1.Text = cookies;          
            string url = "http://www.jisilu.cn/data/sfnew/arbitrage_vip_list/?___t=";
            url = url + ConvertDateTimeToInt(DateTime.Now).ToString();
            HttpHelper http = new HttpHelper();
            HttpItem item = new HttpItem()
            {
                URL = url,
                Cookie = cookies,
                Method = "get",               
            };
            HttpResult result = http.GetHtml(item);
            string html = result.Html;                    
            richTextBox1.Text += html;          
        }
       
        /// <summary>  
        /// 将c# DateTime时间格式转换为Unix时间戳格式  
        /// </summary>  
        /// <param name="time">时间</param>  
        /// <returns>long</returns>  
        public static long ConvertDateTimeToInt(System.DateTime time)
        {
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0));
            long t = (time.Ticks - startTime.Ticks) / 10000;   //除10000调整为13位      
            return t;
        }


1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
 楼主| 发表于 2016-1-14 11:48:08 | 显示全部楼层
本帖最后由 yi_shu 于 2016-1-14 11:54 编辑

没人回复啊!我自己解决了,不用万能能框架就可以了,我估计可能是万能每次提交完页面后关闭了会话,所以导致session过期,所以登录总是不成功@站长苏飞  ,是这原因吗?我用下面的代码,取值就是正常的!

[C#] 纯文本查看 复制代码
 
public CookieContainer cookie;
private void WorkJisilu()
        {            
            string html = GetJisiluHtml();
            if (html == "")
            {
                GetCookie();
                html = GetJisiluHtml();
            }
            JisiluJson jslJson = (JisiluJson)HttpHelper.JsonToObject<JisiluJson>(html);
        }

private string GetJisiluHtml()
        {
            string url = "http://www.jisilu.cn/data/sfnew/arbitrage_vip_list/?___t=";
            url = url + ConvertDateTimeToInt(DateTime.Now).ToString();
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
            request.Proxy = null;
            request.Method = "get";
            request.Referer = "http://www.jisilu.cn/";
            request.CookieContainer = cookie;
            request.UserAgent = "User-Agent:Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.202 Safari/535.1";
            request.KeepAlive = true;
            request.ContentType = "application/x-www-form-urlencoded";
            Encoding encoding = Encoding.UTF8;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream responseStream = response.GetResponseStream();
            StreamReader streamReader = new StreamReader(responseStream, encoding);
            string retString = streamReader.ReadToEnd();
            streamReader.Close();
            responseStream.Close();

            return retString;
        }

 private void GetCookie()
        {           
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://www.jisilu.cn/account/ajax/login_process/");
            request.CookieContainer = new CookieContainer();            
            request.Proxy = null;
            request.Method = "post";
            request.Referer = "http://www.jisilu.cn/login/";
            request.Accept = "application/json, text/javascript, */*; q=0.01";
            request.UserAgent = "User-Agent:Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.202 Safari/535.1";
            request.KeepAlive = true;
            request.ContentType = "application/x-www-form-urlencoded";
            Encoding encoding = Encoding.UTF8;
            string postStr = "return_url=http%3A%2F%2Fwww.jisilu.cn%2F&user_name=" + textBox1.Text + "&password=" + textBox2.Text + "&_post_type=ajax";
            byte[] postData = encoding.GetBytes(postStr);
            request.ContentLength = postData.Length;
            Stream requestStream = request.GetRequestStream();
            requestStream.Write(postData, 0, postData.Length);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream responseStream = response.GetResponseStream();
            StreamReader streamReader = new StreamReader(responseStream, encoding);
            string retString = streamReader.ReadToEnd();
            cookie = request.CookieContainer;
            streamReader.Close();
            responseStream.Close();     
        }



发表于 2016-1-14 12:44:11 | 显示全部楼层
原理应该是一样的,应该是还是参数输入的不对应问题,你可以抓一下两种方法发送的包有什么不同
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

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

GMT+8, 2024-11-23 18:49

© 2014-2021

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