webBrowser获取完整cookie,并把cookie传给httpHelper
webBrowser获取完整cookie,并把cookie传给httpHelper源码using System;using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using DotNet.Utilities;
namespace webBrowser获取完整cookie
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
HttpHelper http = new HttpHelper();
HttpItem item = null;
string cookie = "";
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.Url = new Uri("http://www.baidu.com/");
}
private void button1_Click(object sender, EventArgs e)
{
string str = textBox1.Text.Trim();
if (str.ToLower().Contains("https://") || str.ToLower().Contains("http://"))
{
webBrowser1.Url = new Uri(textBox1.Text.Trim());
}
else
{
webBrowser1.Url = new Uri("http://" + textBox1.Text.Trim());
}
}
private void button2_Click(object sender, EventArgs e)
{
//把webBrowser1登陆的cookie传给HttpHelper
item = new HttpItem()
{
URL = webBrowser1.Url.AbsoluteUri,//URL
Method = "Get",//URL 可选项 默认为Get
Cookie = cookie,//调用webBrowser1登陆的cookie
ContentType = "application/x-www-form-urlencoded",//返回类型 可选项有默认值
};
HttpResult result = http.GetHtml(item);
string html = result.Html;
richTextBox1.AppendText(html + "\r\n\r\n");
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
try
{
cookie=GetCookieString(e.Url.ToString());
richTextBox1.Text=cookie;
}
catch
{
}
}
//取当前webBrowser登录后的Cookie值
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();
}
}
}工程源码下载:http://pan.baidu.com/share/link?shareid=744310193&uk=1929914336
本文摘自:http://www.ie81.com/Technology/154.html
C#利用浏览器获取完整COOKIE示例
使用WebBrowser获取一个网站的COOKIE,在调用Document.Cookie时,发现无法完整获取其COOKIe1.原因:
此 Cookie,加入了。httponly
2.解决方法1: 使用WIN API获取
就可以使用InternetGetCookieEx这个Win API来取得,下面是API的方法宣告
static extern bool InternetGetCookieEx(string pchURL, string pchCookieName, StringBuilder pchCookieData, ref
System.UInt32 pcchCookieData, int dwFlags, IntPtr lpReserved);
static extern int InternetSetCookieEx(string lpszURL, string lpszCookieName, string lpszCookieData, int dwFlags,
IntPtr dwReserved);
private static string GetCookies(string url)
{
uint datasize = 256;
StringBuilder cookieData = new StringBuilder((int)datasize);
if (!InternetGetCookieEx(url, null, cookieData, ref datasize, 0x2000, IntPtr.Zero))
{
if (datasize < 0)
return null;
cookieData = new StringBuilder((int)datasize);
if (!InternetGetCookieEx(url, null, cookieData, ref datasize, 0x00002000, IntPtr.Zero))
return null;
}
return cookieData.ToString();
}
实际上它是通过本地COOKIE文件的读取,来取得COOIE的
3.解决方法2:自已写方法读取本地COOKIE文件
public string Cookie()
2 { www.2cto.com
3 if (this.Url == null)
4 return null;
5 string dir = this.Url.Host;
6 FileStream fr = new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.Cookies) + "
index.dat", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
************
代码比较长,省略
本文摘在:http://soft.zdnet.com.cn/software_zone/2013/0226/2145722.shtml
页:
[1]