如何获取webbrowser的cookie

问题如下:

 

I am logging into a site using a WebBrowser, then i want use regex to get some data , but webRequest didnt use web Browse cookie ,

my webBrowser is in public , is there any way to useing webbrowser cookie in webrequest ?

 

(想要HttpWebResponse取得webbrowser的cookie。
比如webbrowser已经在某网站登录了,那使用HttpWebResponse取得该网站内容时也要显示是登录的状态。

 

解决方法:

public CookieContainer GetCookieContainer() 
   
{ 
       
CookieContainer container = new CookieContainer(); 
 
       
foreach (string cookie in webBrowser1.Document.Cookie.Split(';')) 
       
{ 
           
string name = cookie.Split('=')[0]; 
           
string value = cookie.Substring(name.Length + 1); 
           
string path = "/"; 
           
string domain = ".google.com"; //change to your domain name 
            container
.Add(new Cookie(name.Trim(), value.Trim(), path, domain)); 
       
} 
 
       
return container; 
   
}

 

如下方法不可用:(本人以试多次,不知何原因,愿高深的您能赐教)

 

//String 的Cookie 要转成 Cookie型的 并放入CookieContainer中


  string cookieStr = webBrowser1.Document.Cookie;
  string[] cookstr = cookieStr.Split(';');


  foreach (string str in cookstr)
  {
        string[] cookieNameValue = str.Split('=');
        Cookie ck = new Cookie(cookieNameValue[0].Trim().ToString(), cookieNameValue[1].Trim().ToString());
        ck.Domain = "www.abc.com";//必须写对
        myCookieContainer.Add(ck);
  }

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值