六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 856|回复: 0

webBrowser获取完整cookie,并把cookie传给httpHelper

[复制链接]
 楼主| 发表于 2013-11-3 23:12:16 | 显示全部楼层 |阅读模式
webBrowser获取完整cookie,并把cookie传给httpHelper源码
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Text;  
  7. using System.Windows.Forms;  
  8. using System.Runtime.InteropServices;  
  9. using DotNet.Utilities;  
  10.       
  11. namespace webBrowser获取完整cookie  
  12. {  
  13.     public partial class Form1 : Form  
  14.     {  
  15.         public Form1()  
  16.         {  
  17.             InitializeComponent();  
  18.         }  
  19.       
  20.         HttpHelper http = new HttpHelper();  
  21.         HttpItem item = null;  
  22.         string cookie = "";  
  23.       
  24.         private void Form1_Load(object sender, EventArgs e)  
  25.         {  
  26.             webBrowser1.Url = new Uri("http://www.baidu.com/");              
  27.         }  
  28.       
  29.         private void button1_Click(object sender, EventArgs e)  
  30.         {  
  31.             string str = textBox1.Text.Trim();  
  32.             if (str.ToLower().Contains("https://") || str.ToLower().Contains("http://"))  
  33.             {  
  34.                 webBrowser1.Url = new Uri(textBox1.Text.Trim());  
  35.             }  
  36.             else
  37.             {  
  38.                 webBrowser1.Url = new Uri("http://" + textBox1.Text.Trim());  
  39.             }  
  40.         }  
  41.       
  42.         private void button2_Click(object sender, EventArgs e)  
  43.         {  
  44.             //把webBrowser1登陆的cookie传给HttpHelper  
  45.             item = new HttpItem()  
  46.             {  
  47.                 URL = webBrowser1.Url.AbsoluteUri,//URL   
  48.                 Method = "Get",//URL     可选项 默认为Get     
  49.                 Cookie = cookie,//调用webBrowser1登陆的cookie   
  50.                 ContentType = "application/x-www-form-urlencoded",//返回类型    可选项有默认值     
  51.             };              
  52.       
  53.             HttpResult result = http.GetHtml(item);  
  54.             string html = result.Html;                        
  55.             richTextBox1.AppendText(html + "\r\n\r\n");  
  56.         }  
  57.                
  58.         private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)  
  59.         {  
  60.             try
  61.             {  
  62.                 cookie=GetCookieString(e.Url.ToString());  
  63.                 richTextBox1.Text=cookie;  
  64.             }  
  65.             catch
  66.             {  
  67.             }  
  68.         }  
  69.       
  70.         //取当前webBrowser登录后的Cookie值     
  71.         [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]  
  72.         static extern bool InternetGetCookieEx(string pchURL, string pchCookieName, StringBuilder pchCookieData, ref int pcchCookieData, int dwFlags, object lpReserved);  
  73.         //取出Cookie,当登录后才能取  
  74.         private static string GetCookieString(string url)  
  75.         {  
  76.             // Determine the size of the cookie        
  77.             int datasize = 256;  
  78.             StringBuilder cookieData = new StringBuilder(datasize);  
  79.             if (!InternetGetCookieEx(url, null, cookieData, ref datasize, 0x00002000, null))  
  80.             {  
  81.                 if (datasize < 0)  
  82.                     return null;  
  83.                 // Allocate stringbuilder large enough to hold the cookie      
  84.                 cookieData = new StringBuilder(datasize);  
  85.                 if (!InternetGetCookieEx(url, null, cookieData, ref datasize, 0x00002000, null))  
  86.                     return null;  
  87.             }  
  88.             return cookieData.ToString();  
  89.         }              
  90.     }  
  91. }
复制代码
工程源码下载: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的方法宣告
[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern bool InternetGetCookieEx(string pchURL, string pchCookieName, StringBuilder pchCookieData, ref
System.UInt32 pcchCookieData, int dwFlags, IntPtr lpReserved);
[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
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
该会员没有填写今日想说内容.
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

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