如何在C# WinForm 程序中使用WebBrowser控件时设置COOKIE的值。

.NET 中提供的WebBrowser 控件中的COOKIE无法直接设置,无奈之下只好使用API去设置COOKIE的值了。希望MS可以尽快提供在WebBrowser控件中任意设置Cookie的值。可惜的是在VS2010中也不能直接设置这个值。

 

ExpandedBlockStart.gif 代码
using  System;
using  System.Runtime.InteropServices;

public   class  CookieHelper
{
  
private   string  url;
  [DllImport(
" wininet.dll " , CharSet  =  CharSet.Auto, SetLastError  =   true )]

  
public   static   extern   bool  InternetSetCookie( string  lpszUrlName,  string  lbszCookieName,  string  lpszCookieData);

  
public   static   void  SetCookie( string  path, string  cookieName, string  cookieValue)
  {
    
this .url  =  path;

    
//  设置Cookie的值
    InternetSetCookie(url, cookieName, cookieValue);
  }
}

 

 

 

ExpandedBlockStart.gif 代码
//  清除所有COOKIE
using  System;
using  System.IO; 

void  clearIECache()
{
  ClearFolder (
new  DirectoryInfo (Environment.GetFolderPath(Environment.SpecialFolder.InternetCache)));
}
 
void  ClearFolder (DirectoryInfo folder)
{
  
foreach  (FileInfo file  in  folder.GetFiles())
  {
    file.Delete(); 
  }

  
foreach  (DirectoryInfo subfolder  in  folder.GetDirectories())
  {
    ClearFolder(subfolder); 
  }
}

 

  

 

 

转载于:https://www.cnblogs.com/leeairw/archive/2010/05/26/1744602.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值