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

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

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

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

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

查看: 5063|回复: 5

[求助] 【webbrowser cookie 的錯誤訊息】

[复制链接]
发表于 2016-9-3 16:13:06 | 显示全部楼层 |阅读模式
我自己寫一個
用 webbrowser 登入後
把 cookie pass 給 httphelper
程式碼如下
[C#] 纯文本查看 复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections;
using System.Threading;
using CsharpHttpHelper;
using CsharpHttpHelper.Enum;
using System.Net;
using System.IO;


namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //等待網頁讀取完成
        public void loading()
        {
            while (webBrowser1.ReadyState != WebBrowserReadyState.Complete)
                Application.DoEvents();
        }

        //按鍵定義
        private void button1_Click(object sender, EventArgs e)
        {
            webBrowser1.Navigate("http://localhost:60080/member/index.php");
            loading();
            login();
            Thread.Sleep(100);
            HttpHelper http = new HttpHelper();
            HttpItem item = new HttpItem()
            {

            };
            string cookie = webBrowser1.Document.Cookie;
            // 第二次使用Cookie
            item = new HttpItem()
            {
                URL = "http://localhost:60080/member/data.php?iii=123456",
                Cookie = cookie,
            };
            HttpResult result = http.GetHtml(item);
            result = http.GetHtml(item);
            string html = result.Html;
            StreamWriter sw = new StreamWriter("log.html");
            sw.WriteLine(html);
            sw.Close();
            Console.WriteLine(html);
            Console.Read();
        }

        //自動登入
        private void login()
        {
            HtmlDocument doc = webBrowser1.Document;
            for (int i = 0; i < doc.All.Count; i++)
            {
                if (doc.All[i].TagName.ToUpper().Equals("INPUT"))
                {
                    switch (doc.All[i].Name)
                    {
                        case "username":
                            doc.All[i].InnerText = "123456";
                            break;
                        case "password":
                            doc.All[i].InnerText = "andyto202";
                            //doc.All[i].Focus();
                            //SendKeys.SendWait("{Enter}");
                            break;
                        case "Submit":
                            doc.All[i].InvokeMember("click");
                            break;
                    }
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //這行 ok,會顯示出搜尋結果的原始碼
            richTextBox1.AppendText(webBrowser1.DocumentText);
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            richTextBox1.AppendText(webBrowser1.Url.ToString() + "\n");
        }
    }
}


其中讓 httphelper 拿到 cookie 的關鍵是這行
string cookie = webBrowser1.Document.Cookie;
後來我有看了站上很多前輩都用
[C#] 纯文本查看 复制代码
//取当前webBrowser登录后的Cookie值   
        [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
        static extern bool InternetGetCookieEx(string pchURL, string pchCookieName, StringBuilder pchCookieData, ref int pcchCookieData, int dwFlags, object lpReserved);
        //取出Cookie,当登录后才能取    
        private static string GetCookieString(string url)
        {
            // Determine the size of the cookie      
            int datasize = 256;
            StringBuilder cookieData = new StringBuilder(datasize);
            if (!InternetGetCookieEx(url, null, cookieData, ref datasize, 0x00002000, null))
            {
                if (datasize < 0)
                    return null;
                // Allocate stringbuilder large enough to hold the cookie    
                cookieData = new StringBuilder(datasize);
                if (!InternetGetCookieEx(url, null, cookieData, ref datasize, 0x00002000, null))
                    return null;
            }
            return cookieData.ToString();
        }


於是我改成了
[C#] 纯文本查看 复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections;
using System.Threading;
using CsharpHttpHelper;
using CsharpHttpHelper.Enum;
using System.Net;
using System.IO;
using System.Runtime.InteropServices;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //等待網頁讀取完成
        public void loading()
        {
            while (webBrowser1.ReadyState != WebBrowserReadyState.Complete)
                Application.DoEvents();
        }

        //取当前webBrowser登录后的Cookie值   
        [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
        static extern bool InternetGetCookieEx(string pchURL, string pchCookieName, StringBuilder pchCookieData, ref int pcchCookieData, int dwFlags, object lpReserved);
        //取出Cookie,当登录后才能取    
        private static string GetCookieString(string url)
        {
            // Determine the size of the cookie      
            int datasize = 256;
            StringBuilder cookieData = new StringBuilder(datasize);
            if (!InternetGetCookieEx(url, null, cookieData, ref datasize, 0x00002000, null))
            {
                if (datasize < 0)
                    return null;
                // Allocate stringbuilder large enough to hold the cookie    
                cookieData = new StringBuilder(datasize);
                if (!InternetGetCookieEx(url, null, cookieData, ref datasize, 0x00002000, null))
                    return null;
            }
            return cookieData.ToString();
        }

        //按鍵定義
        private void button1_Click(object sender, EventArgs e)
        {
            webBrowser1.Navigate("http://localhost:60080/member/index.php");
            loading();
            login();
            Thread.Sleep(100);
            HttpHelper http = new HttpHelper();
            HttpItem item = new HttpItem()
            {

            };
            string cookie = GetCookieString("http://localhost:60080/member/index.php");
            // 第二次使用Cookie
            item = new HttpItem()
            {
                URL = "http://localhost:60080/member/data.php?iii=123456",
                Cookie = cookie,
            };
            HttpResult result = http.GetHtml(item);
            result = http.GetHtml(item);
            string html = result.Html;
            StreamWriter sw = new StreamWriter("log.html");
            sw.WriteLine(html);
            sw.Close();
            Console.WriteLine(html);
            Console.Read();
        }

        //自動登入
        private void login()
        {
            HtmlDocument doc = webBrowser1.Document;
            for (int i = 0; i < doc.All.Count; i++)
            {
                if (doc.All[i].TagName.ToUpper().Equals("INPUT"))
                {
                    switch (doc.All[i].Name)
                    {
                        case "username":
                            doc.All[i].InnerText = "123456";
                            break;
                        case "password":
                            doc.All[i].InnerText = "andyto202";
                            //doc.All[i].Focus();
                            //SendKeys.SendWait("{Enter}");
                            break;
                        case "Submit":
                            doc.All[i].InvokeMember("click");
                            break;
                    }
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //這行 ok,會顯示出搜尋結果的原始碼
            richTextBox1.AppendText(webBrowser1.DocumentText);
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            richTextBox1.AppendText(webBrowser1.Url.ToString() + "\n");
        }
    }
}


但是它錯誤訊息一直會停在
if (!InternetGetCookieEx(url, null, cookieData, ref datasize, 0x00002000, null))  這行
訊息是

發生 PInvokeStackImbalance
Message: Managed 偵錯助理 'PInvokeStackImbalance' 偵測到 'C:\Users\andyto202\Documents\Visual Studio 2015\Projects\WindowsFormsApplication2\WindowsFormsApplication2\bin\Debug\WindowsFormsApplication2.vshost.exe' 中發生問題。
其他資訊: 對 PInvoke 函式 'WindowsFormsApplication2!WindowsFormsApplication2.Form1::InternetGetCookieEx' 的呼叫已使堆疊失去平衡。這可能是因為 Managed PInvoke 簽章和 Unmanaged 目標簽章不相符。請確認 PInvoke 簽章的呼叫慣例及參數與目標 Unmanaged 簽章是否相符。


請大家指導
感恩



1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2016-9-3 23:38:00 | 显示全部楼层
这个方法不能在调试模式下运行,直接执行程序就行了,另外获取Cookie不能输入具体网址,只需要输入域名部分就行了。本地的网址没测试过是否可以。
 楼主| 发表于 2016-9-6 21:51:34 | 显示全部楼层
站长苏飞 发表于 2016-9-3 23:38
这个方法不能在调试模式下运行,直接执行程序就行了,另外获取Cookie不能输入具体网址,只需要输入域名部分 ...

站長謝謝
現在才看到您的回答
的確調試不行
但是直接執行是可以的
另外我測試的結果
獲取 cookie 是可以輸入具體的網址的
用 localhost 也沒問題喔
重點就是不能用調試去跑
再次謝謝站長
发表于 2016-9-7 08:17:23 | 显示全部楼层
andyto202 发表于 2016-9-6 21:51
站長謝謝
現在才看到您的回答
的確調試不行

这是一个多线程的问题,所以无法调试,只能直接执行。
发表于 2016-11-9 11:31:16 | 显示全部楼层
andyto202 发表于 2016-9-6 21:51
站長謝謝
現在才看到您的回答
的確調試不行

请问你是怎么弄好的我也卡在cookies上面了
 楼主| 发表于 2016-11-30 09:57:47 | 显示全部楼层
langbin 发表于 2016-11-9 11:31
请问你是怎么弄好的我也卡在cookies上面了

就是不要調試,直接跑執行檔啊
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

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

GMT+8, 2024-4-20 01:24

© 2014-2021

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