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

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

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

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

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

查看: 3641|回复: 8

[咨询站长] 万能的站长c#模拟登陆CP面板

[复制链接]
发表于 2017-5-26 15:41:42 | 显示全部楼层 |阅读模式
万能的站长,我这段时间一直在做一个CP面板的模拟登陆,登陆这一步可以登进去,但是登进去以后,抓取登陆后的个页面,却返回到登陆界面,一直在测试不成功,求万能的站长能不能帮忙解决一下,我感激不尽。
我的代码我就贴出来:
       public Form1()
        {
            InitializeComponent();
        }

        private void btn_start_Click(object sender, EventArgs e)
        {
            //secure,whostmgrsession[\s\S]*?(2087);
            string myhost =tb_url.Text;
            HttpResult loginwhm = gethttp(myhost, "get");
            string logincookie = loginwhm.Cookie;
            HttpResult postwhm = gethttp(myhost + "/login/?login_only=1", "post", "user=root&pass=*******&goto_uri=%2F", logincookie);
            string cookiec =regmatch("secure,whostmgrsession[\\s\\S]*?(2087);", postwhm.Cookie);
            cookiec = cookiec + "timezone=Asia/Shanghai;path=/;port=2087;";
            JsonParser jp = (JsonParser)JsonConvert.DeserializeObject<JsonParser>(postwhm.Html);
            string gurl = myhost + jp.redirect;
            HttpResult whmcon = gethttp(gurl, "get", "", cookiec);
            tb_result.Text = whmcon.Html;
        }

        public HttpResult gethttp(string url,string method,string postdata="",string cookiec=""){
            HttpHelper http = new HttpHelper();
            HttpItem item = new HttpItem()
            {
                URL = url,//URL     必需项   
                Method = method,//URL     可选项 默认为Get  
                Postdata = postdata,
                CerPath = "D:\\mypro\\证书登陆\\cp.cer",//证书绝对路径     可选项不需要证书时可以不写这个参数      
                ResultType = ResultType.Byte,
                Cookie = cookiec
            };
            HttpResult result = http.GetHtml(item);
            return result;
        }
        public string regmatch(string regs, string regcon)
        {
            Regex reg = new Regex(regs);
            var mat = reg.Match(regcon);
            if (mat.Length > 0)
            {
                return mat.Groups[0].ToString().Trim();
            }
            else
            {
                return "";
            }
        }
    }

    public class JsonParser
    {
        public int status;
        public string security_token;
        public string redirect;
    }
因为这个是管理服务器的,所以地址,账号密码我就不公开了,我私信给站长,希望求解决。




1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2017-5-26 15:47:32 | 显示全部楼层
应该是Cookie格式有问题,你直接简单 一下,或者把多余的去掉试试
 楼主| 发表于 2017-5-26 15:52:09 | 显示全部楼层
我试过很多次了,发现一个问题就是我用httpwatch发现有一个时间区域的COOKIE,但是用C#返回时没有这个COOKIE,不知道为啥。另外,我看了登陆后的COOKIE,就只有两个站,所以中间的COOKIE是我组合了的,其中一个是固定的,另一个是登陆后获取的。
 楼主| 发表于 2017-5-26 15:54:32 | 显示全部楼层
站长苏飞 发表于 2017-5-26 15:47
应该是Cookie格式有问题,你直接简单 一下,或者把多余的去掉试试

另外,COOKIE其实我不太熟悉C#中的格式,之前用HTTPHELP返回的直接引入都没问题,唯 有这个咋都不行。无论是我通过Httpresult返回后得到的还是返回后我用正则获取再组合都不行?这个问题已经搞了好几天了,没搞定。
发表于 2017-5-26 17:45:14 | 显示全部楼层
发表于 2017-5-26 17:46:02 | 显示全部楼层
我这边现不方便直接测试查看,你可以先试试,正常登录的情况下应该多半是Cookie中多出一些path=/这之类的问题,有些网站对Cookie格式要求比较严格
 楼主| 发表于 2017-6-5 16:37:32 | 显示全部楼层
站长苏飞 发表于 2017-5-26 17:46
我这边现不方便直接测试查看,你可以先试试,正常登录的情况下应该多半是Cookie中多出一些path=/这之类的问 ...

这个问题确的是你说的这个问题,那个COOKIE是我搞错了。
发表于 2017-6-5 16:42:38 | 显示全部楼层
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-20 23:02

© 2014-2021

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