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

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

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

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

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

查看: 12766|回复: 12

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

[复制链接]
发表于 2019-8-1 02:26:55 | 显示全部楼层 |阅读模式
在做一个模拟登陆,我的流程是先获取图形验证码,然后再带用户名、密码和验证码参数去登陆,需要取得登陆后页面的html。但是实际请求时,发现一直会返回302,代码如下:
[C#] 纯文本查看 复制代码
using CsharpHttpHelper;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WebKit.DOM;
using static AutoPlaceOrder.MONILogin;

namespace AutoPlaceOrder
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public static CookieContainer container = null; //存储验证码cookie
        Hashtable param = new Hashtable();//this is for keep post data.
        TestCookie sc = new TestCookie();
        private void button1_Click(object sender, EventArgs e)
        {
            WebsiteLogin.getUrl("https://9714035371-ccgj.qq168.ws/login");
        }
        public static HttpResult _httpResult=new HttpResult();
        public static HttpHelper _http = new HttpHelper();
        public static string _cookie;
        public static HttpItem _httpItem = null;

        private void button2_Click(object sender, EventArgs e)
        {

              _httpItem = new HttpItem
            {
                URL = "https://9714035371-ccgj.qq168.ws/code",
                Referer = "https://9714035371-ccgj.qq168.ws/",
                Accept = "image/png, image/svg+xml, image/*;q=0.8, */*;q=0.5",
                ResultType = CsharpHttpHelper.Enum.ResultType.Byte
            };
              _httpResult = _http.GetHtml(_httpItem);
            _cookie = _httpResult.Cookie;
     


            if (_httpResult != null && _httpResult.ResultByte != null && _httpResult.ResultByte.Length > 0)
            {
                var stream = new MemoryStream(_httpResult.ResultByte);
                pictureBox1.Image = Image.FromStream(stream);
            }

        }

        private void button3_Click(object sender, EventArgs e)
        { 
              _httpItem = new HttpItem
            {
                URL = "https://9714035371-ccgj.qq168.ws/login",
                Referer = "https://9714035371-ccgj.qq168.ws/",
                Accept = "image/png, image/svg+xml, image/*;q=0.8, */*;q=0.5",
                ResultType = CsharpHttpHelper.Enum.ResultType.String,
                Method = "post",
                Postdata = "account=" + textBox_username.Text.Trim() + "&password=" + textBox_password.Text.Trim() + "&code=" + textBox_verify.Text.Trim() + "&type=1",
                Cookie = _cookie,
                ContentType = "application/x-www-form-urlencoded",//返回类型    可选项有默认值 
              
            };
            _httpItem.Header.Add("Accept-Language", "zh-cn");
            _httpResult = _http.GetHtml(_httpItem);

            _cookie = _httpResult.Cookie;
            textBox_note.Text += "\r\n"+_cookie;

            //获取302跳转URl
            string redirectUrl = _httpResult.RedirectUrl;

            _httpItem = new HttpItem()
            {
                URL = redirectUrl,//URL     必需项    
                Method = "GET",//URL     可选项 默认为Get   
                Cookie = _cookie, 
                
            };
            _httpItem.Header.Add("Accept-Language", "zh-cn");
            //请求的返回值对象
            _httpResult = _http.GetHtml(_httpItem);
            //获取请请求的Html
            string html = _httpResult.Html;
            //获取请求的Cookie
            _cookie = _httpResult.Cookie;
            textBox_note.Text += "\r\n" + _cookie;

            //获取302跳转URl
            redirectUrl = _httpResult.RedirectUrl;
            _httpItem = new HttpItem()
            {
                URL = redirectUrl,//URL     必需项    
                Method = "GET",//URL     可选项 默认为Get   
                Cookie = _cookie, 
            };
            _httpItem.Header.Add("Accept-Language", "zh-cn");
            //请求的返回值对象
            _httpResult = _http.GetHtml(_httpItem);
            //获取请请求的Html
             html = _httpResult.Html;
            //获取请求的Cookie
            _cookie = _httpResult.Cookie;
            textBox_note.Text += _cookie;
             


            textBox_note.Text += "\r\n" + html; 
        }
             
            }
    
}
 


页面设计如下 :
20190801022120.png

实际运行效果如下:
QQ截图20190801022239.jpg

然后我按正常360浏览器访问,发现截取到的请求参数如下:
1.jpg

2.jpg

3.jpg

是什么原因呢?求教。


1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2019-8-1 09:28:22 | 显示全部楼层
把Cookie格式化一下试试
发表于 2019-8-1 09:42:47 | 显示全部楼层
有测试账号吗
 楼主| 发表于 2019-8-1 12:52:42 | 显示全部楼层
站长苏飞 发表于 2019-8-1 09:28
把Cookie格式化一下试试

怎么格式化呢?
 楼主| 发表于 2019-8-1 13:42:45 | 显示全部楼层

我私信您了,请看一下论坛消息
发表于 2019-8-1 18:16:53 | 显示全部楼层
把Cookie的path=/和过期时间去掉
 楼主| 发表于 2019-8-1 21:44:30 | 显示全部楼层
站长苏飞 发表于 2019-8-1 18:16
把Cookie的path=/和过期时间去掉

不太会,能否在代码上指导一下,万分感谢
 楼主| 发表于 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;

感谢感谢~
发表于 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];
            }
        }
    }

发表于 2019-8-2 07:59:43 | 显示全部楼层
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-11-15 14:38

© 2014-2021

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