苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

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

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

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

查看: 11702|回复: 6

[咨询站长] 关于路由器存取cookies问题,为什么第二次请求带上cookies请求无效

[复制链接]
发表于 2014-7-14 00:09:52 | 显示全部楼层 |阅读模式
1金钱
[C#] 纯文本查看 复制代码
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
        }

        public static CookieContainer mycookies;

        private void btnlogin_Click(object sender, EventArgs e)
        {
            string userName = txtuser.Text.Trim();
            string password = txtpassword.Text.Trim();
            string html=login("http://192.168.1.1",userName,password);
            textBox1.Text = html;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string html=get("http://192.168.1.1/userRpm/StatusRpm.htm?Disconnect=断 线&wan=1");
            textBox1.Text = html;
        }

        public string login(string url,string userName,string password)
        {            
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = "GET";
            req.UserAgent = "Mozilla/5.0 (Windows NT 5.1; rv:28.0) Gecko/20100101 Firefox/28.0";                       
            req.KeepAlive = true;
            req.Accept = "*/*";
            req.PreAuthenticate = true;
            CredentialCache myCache = new CredentialCache();
            myCache.Add(new Uri(url), "Basic", new NetworkCredential(userName, password));//添加Basic认证
            req.Credentials = myCache;
            mycookies = new CookieContainer(); 
            req.CookieContainer = mycookies;
            HttpWebResponse resp;
            try
            {
                resp= (HttpWebResponse)req.GetResponse();
                resp.Cookies = mycookies.GetCookies(req.RequestUri);
                StreamReader sr = new StreamReader(resp.GetResponseStream(), System.Text.Encoding.Default);
                String content = sr.ReadToEnd();                
                sr.Close();
                resp.Close();
                return content;
            }
            catch(WebException e)
            {
                resp = (HttpWebResponse)e.Response;
                StreamReader sr = new StreamReader(resp.GetResponseStream(), System.Text.Encoding.Default);
                String content = sr.ReadToEnd();
                sr.Close();
                return content;
            }
        }


        public string get(string url)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);            
            request.UseDefaultCredentials = true;
            request.ContentType = "text/html";
            request.UserAgent = "Mozilla/5.0 (Windows NT 5.1; rv:28.0) Gecko/20100101 Firefox/28.0";
            request.Method = "get";
            request.CookieContainer = mycookies;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream stream = response.GetResponseStream();
            StreamReader responseReader = new StreamReader(stream, Encoding.Default);
            string content = responseReader.ReadToEnd();
            stream.Close();
            return content;
        }



1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2014-7-14 09:43:31 | 显示全部楼层
我试过的路由是不用带COOKIE,每个请求都带上Authorization,能直接请求到里面的某项。如重启 断线
回复

使用道具 举报

发表于 2014-7-14 11:19:56 | 显示全部楼层
抓包看看都需要什么,还有你说的不行是什么原因,报的什么错,
回复

使用道具 举报

 楼主| 发表于 2014-7-14 23:17:14 | 显示全部楼层
本帖最后由 放逐(··) 于 2014-7-14 23:57 编辑
站长苏飞 发表于 2014-7-14 11:19
抓包看看都需要什么,还有你说的不行是什么原因,报的什么错,

这是我用httpwatch抓的包,麻烦你帮我看看么,我不怎么看得懂

里面有连接的和断开的,怎么在登陆的情况下,带上cookies再次请求,继续模拟其它操作,谢谢

我按上面的写法来做,那样带cookies返回的还是401没有权限,也就是没有登陆,说明那样带cookies是不对的,是不是路由这种跟其它的网页方式不一样?

好像是http头里的 Authorization:这个属性比较特殊,不会处理

抓包.rar

125.08 KB, 下载次数: 24, 下载积分: 金钱 -1

回复

使用道具 举报

 楼主| 发表于 2014-7-15 09:08:21 | 显示全部楼层
inghot 发表于 2014-7-14 09:43
我试过的路由是不用带COOKIE,每个请求都带上Authorization,能直接请求到里面的某项。如重启 断线

要怎么带,请指点一下
回复

使用道具 举报

发表于 2014-7-15 09:41:26 | 显示全部楼层
[C#] 纯文本查看 复制代码
string html = "";
           [color=Red] string pass = Convert.ToBase64String(Encoding.Default.GetBytes(登录用户名 + ":" + 登录密码));[/color]
            HttpItem item = new HttpItem();
            HttpResult result;

            item.URL = "http://192.168.1.1";
            item.Method = "get";
            item.KeepAlive = true;
[color=Red]            item.Header.Add("Authorization", "Basic " + pass);[/color]
            item.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
            item.UserAgent = "Mozilla/5.0 (Windows NT 6.1; rv:24.0) Gecko/20100101 Firefox/24.0";
            item.Header.Add("Accept-Encoding", "gzip,deflate,sdch");
            item.Header.Add("Accept-Language", "zh-CN,zh;q=0.8");


我的请求就是都带红色的那个,不用COOKIES
回复

使用道具 举报

 楼主| 发表于 2014-7-15 21:13:21 | 显示全部楼层
本帖最后由 放逐(··) 于 2014-7-15 21:15 编辑


[C#] 纯文本查看 复制代码
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
            userName = txtuser.Text.Trim();
            password = txtpassword.Text.Trim();
            userNamepassword = userName + ":" + password;
        }

        public static string userName;
        public static string password;
        public static string userNamepassword;
private void button2_Click(object sender, EventArgs e)
        {
            string html=get("http://192.168.1.1/userRpm/StatusRpm.htm?Disconnect=断 线&wan=1",userNamepassword);
            textBox1.Text = html;
        }

public string get(string url,string userNamepassword)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);            
            request.UseDefaultCredentials = true;
            request.KeepAlive = true;
            request.ContentType = "text/html";
            request.UserAgent = "Mozilla/5.0 (Windows NT 5.1; rv:28.0) Gecko/20100101 Firefox/28.0";
            request.Method = "get";
            request.Headers.Add("Authorization", "Basic" + Convert.ToBase64String(new ASCIIEncoding().GetBytes(userNamepassword)));
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream stream = response.GetResponseStream();
            StreamReader responseReader = new StreamReader(stream, Encoding.Default);
            string content = responseReader.ReadToEnd();
            stream.Close();
            return content;


这样还是不行
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-3-1 20:05

© 2014-2021

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