欢乐捕鱼之海底极乐

蘇飛論壇

 找回密碼
 馬上注冊

QQ登錄

只需一步,快速開始

掃一掃,訪問微社區

分布式系統框架(V1.3) 輕松承載百億數據,千萬流量!討論專區 - 源碼下載 - 官方教程

HttpHelper萬能框架(V2.3-含.netcore) HttpHelper官方出品,無敵框架討論區 - 源碼下載 - 在線測試和代碼生成

HttpHelper爬蟲類(V2.0) 最牛的爬蟲類,沒有爬不到只有想不到 源碼 - 代碼生成器 - 討論區 - 教程- 例子

查看: 256|回復: 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 | 只看該作者
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 馬上注冊

本版積分規則

QQ|手機版|小黑屋|手機版|聯系我們|關于我們|廣告合作|蘇飛論壇 ( 豫ICP備17001017號-1)

GMT+8, 2019-8-12 07:56

© 2017-2018

快速回復 返回頂部 返回列表
欢乐捕鱼之海底极乐 刮刮乐中奖图片100万 北京pk赛车7码计划规律 欢乐二人雀神怎么玩 重庆时时彩2.1版本安卓 赛车pk10技巧论坛 四码倍投6期计划 网贷火龙果APP下载 永富g娱乐彩票 抢庄牌九游戏app下载 新时时彩票开奖查询