苏飞论坛

 找回密码
 马上注册

QQ登录

pc蛋蛋官方网址只需一步,快速开始

扫一扫,访问微社区

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

HttpHelper万能框架(V2。3-含。netcore) HttpHelper官方出品,无敌框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 最牛的爬虫类,没有爬不到只有想不到 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 232|回复: 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; 
        }
             
            }
    
}
 


页面设计如下 :


实际运行效果如下:


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






是什么原因呢?求教。
沙发
发表于 2019-8-1 09:28:22 | 只看该作者
板凳
发表于 2019-8-1 09:42:47 | 只看该作者
地板
 楼主| 发表于 2019-8-1 12:52:42 | 只看该作者
5
 楼主| 发表于 2019-8-1 13:42:45 | 只看该作者
6
发表于 2019-8-1 18:16:53 | 只看该作者
7
 楼主| 发表于 2019-8-1 21:44:30 | 只看该作者
8
 楼主| 发表于 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;

感谢感谢~
9
发表于 2019-8-2 00:20:55 | 只看该作者

可能是成功了?
[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];
            }
        }
    }

10
发表于 2019-8-2 07:59:43 | 只看该作者
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

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

GMT+8, 2019-8-10 08:58

© 2017-2018

快速回复 返回顶部 返回列表
pc蛋蛋网址 pc蛋蛋官方网址 pc蛋蛋官方网站 pc蛋蛋官网 pc蛋蛋官方网站 pc蛋蛋官方网站 pc蛋蛋网址 pc蛋蛋平台 pc蛋蛋网址 pc蛋蛋官方网站