苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

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

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

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

求助,多次302后无法正确跳转

查看数: 26198 | 评论数: 12 | 收藏 0
关灯 | 提示:支持键盘翻页<-左 右->
    组图打开中,请稍候......
发布时间: 2019-8-1 02:26

正文摘要:

在做一个模拟登陆,我的流程是先获取图形验证码,然后再带用户名、密码和验证码参数去登陆,需要取得登陆后页面的html。但是实际请求时,发现一直会返回302,代码如下: [C#] 纯文本查看 复制代码using CsharpHttpH ...

回复

ABC536 发表于 2019-8-4 16:10:51
寻找iOS上架技术,要求简单,内容不限,长期合作,价格高,中介勿扰!有意者联系邮箱:make101love@126.com   QQ:1060088990
732570672 发表于 2019-8-2 09:58:35
732570672 发表于 2019-8-2 09:58:02
clrscr 发表于 2019-8-2 00:20
可能是成功了?
[mw_shl_code=csharp,true]private void GetCode()
        {

哈哈,厉害,应该是成功了,我也试好了,原来是他网站跳转了多次,跟着手动跳就好了,谢谢哈~
站长苏飞 发表于 2019-8-2 07:59:43
clrscr 发表于 2019-8-2 00:20:55
Snipaste_2019-08-02_00-19-09.png
可能是成功了?
[C#] 纯文本查看 复制代码
private void GetCode()
        {
            //获取Cookie
            HttpHelper http = new HttpHelper();
            HttpItem item = new HttpItem()
            {
                URL = "https://9714035371-ccgj.qq168.ws/ssid1?url=/js/jquery.js",
                Method = "GET",
                Referer = "https://9714035371-ccgj.qq168.ws/login",

                ProxyIp = "127.0.0.1:8888",
            };
            HttpResult result = http.GetHtml(item);

            string JSCookie = result.Cookie;


            //获取验证码图片
            item = new HttpItem()
            {
                URL = "https://9714035371-ccgj.qq168.ws/code?_=" + GetTimeC(),
                Method = "GET",
                UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36",
                Referer = "https://9714035371-ccgj.qq168.ws/login",
                ResultType = ResultType.Byte,
                ProxyIp = "127.0.0.1:8888",
            };
            result = http.GetHtml(item);
            imgCode.Source = LoadImage(result.ResultByte);

            string ImgCodeCode = result.Cookie;


            //获取Cookie
            item = new HttpItem()
            {
                URL = "https://9714035371-ccgj.qq168.ws/ssid1?url=/default/js/qrcode.min.js",
                Method = "GET",
                Referer = "https://9714035371-ccgj.qq168.ws/login",
                Cookie = JSCookie,
                ProxyIp = "127.0.0.1:8888",
            };
            result = http.GetHtml(item);
            string JSCookie2 = result.Cookie;

            MyCookie = ImgCodeCode + ";" + JSCookie2;

        }

        private string OneKeyLogin()
        {
            HttpHelper http = new HttpHelper();
            HttpItem item = new HttpItem()
            {
                URL = "https://9714035371-ccgj.qq168.ws/login",
                Method = "POST",
                Referer = "https://9714035371-ccgj.qq168.ws/login",
                Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
                UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36",
                ContentType = "application/x-www-form-urlencoded",
                KeepAlive = true,
                Cookie = MyCookie,
                ProxyIp = "127.0.0.1:8888",
                Postdata = "type=1&account=" + user + "&password=" + pass + "&code=" + txtCode.Text,

            };
            HttpResult result = http.GetHtml(item);
            string html = result.Html;


            string NewUrl = result.RedirectUrl;

            item = new HttpItem()
            {
                URL = NewUrl,
                Method = "GET",
                Referer = "https://9714035371-ccgj.qq168.ws/login",
                Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
                UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36",
                Cookie = MyCookie,

            };
            result = http.GetHtml(item);
            string Cookie = result.Cookie;

            Cookie = HttpHelper.GetSmallCookie(Cookie);


            item = new HttpItem()
            {
                URL = "https://9714035371-ccgj.qq168.ws/member/agreement",
                Method = "GET",
                Referer = "https://9714035371-ccgj.qq168.ws/login",
                Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
                UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36",
                Cookie = Cookie,

            };
            result = http.GetHtml(item);

            html = result.Html;
            Cookie = Cookie  + MyCookie;



            item = new HttpItem()
            {
                URL = "https://9714035371-ccgj.qq168.ws/member/index",
                Method = "GET",
                Referer = "https://9714035371-ccgj.qq168.ws/member/agreement",
                Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
                UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36",
                Cookie = Cookie,

            };
            result = http.GetHtml(item);
            html = result.Html;

            return html;
        }

        private System.Drawing.Image byteArrayToImage(byte[] Bytes)
        {
            MemoryStream ms = new MemoryStream(Bytes);
            return System.Drawing.Bitmap.FromStream(ms, true);
        }

        BitmapSource LoadImage(Byte[] imageData)
        {
            using (MemoryStream ms = new MemoryStream(imageData))
            {
                var decoder = BitmapDecoder.Create(ms,
                    BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
                return decoder.Frames[0];
            }
        }
    }

732570672 发表于 2019-8-1 23:48:53
问题解决了,是网站本身设计得厉害,会多次302,我一直手动追踪请求到第5次就能跳转到用户主页了,重复执行5次以下:
[C#] 纯文本查看 复制代码
  //获取302跳转URl            string redirectUrl = _httpResult.RedirectUrl;
            _httpItem = new HttpItem()
            {
                URL = redirectUrl,//URL     必需项    
                Accept = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36",
                Method = "GET",//URL     可选项 默认为Get   
                Cookie = _cookie,
                Referer = "https://9714035371-ccgj.qq168.ws/login",
            };
            _httpItem.Header.Add("Accept-Language", "zh-cn");
            //请求的返回值对象
            _httpResult = _http.GetHtml(_httpItem);
            //获取请请求的Html
            string html = _httpResult.Html;

感谢感谢~
732570672 发表于 2019-8-1 21:44:30
站长苏飞 发表于 2019-8-1 18:16
把Cookie的path=/和过期时间去掉

不太会,能否在代码上指导一下,万分感谢
站长苏飞 发表于 2019-8-1 18:16:53
把Cookie的path=/和过期时间去掉
732570672 发表于 2019-8-1 13:42:45

我私信您了,请看一下论坛消息

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

GMT+8, 2025-7-1 07:15

© 2014-2021

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