我自己寫一個
用 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 簽章是否相符。
請大家指導
感恩
|