c# HttpWebRequest Cookie 设置到 webBrowser 控件
[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool InternetSetCookie(string lpszUrlName, string lbszCookieName, string lpszCookieData);
void Form3_Load(object sender, EventArgs e)
{
InternetSetCookie("http://localhost/1.asp",null,Common.cookie );
// webBrowser1.Navigate("http://localhost/1.asp");
webBrowser1.Navigate("http://localhost/1.asp");
}
//get cookie from HttpWebRequest
string s= HttpHelper.httpGet("http://localhost/login.asp");
string cookie= Common.cookie= HttpHelper.myCookieContainer.GetCookieHeader(new Uri("http://localhost/"));
MessageBox.Show(cookie);
public static class HttpHelper
{
public static CookieContainer myCookieContainer = new CookieContainer();
public static void initCookie()
{
myCookieContainer = new CookieContainer();
string cookieStr = "accessID=20170817112826535103; tempID=4007780019; OnceLoginWEB=91919420; lastLoginDate=Thu%20Sep%2020%202018%2014%3A27%3A55%20GMT+0800%20%28China%20Standard%20Time%29; orderSource=10130301; NTKF_T2D_CLIENTID=guestFE5FC01D-72B3-4E96-7D6F-D08ACEE9E294; Hm_lvt_5caa30e0c191a1c525d4a6487bf45a9d=1537424876; Hm_lpvt_5caa30e0c191a1c525d4a6487bf45a9d=1537425679; Hm_lvt_8bcb99d7b6c2fba606d7089f6310d8bc=1537425835; Hm_lpvt_8bcb99d7b6c2fba606d7089f6310d8bc=1537427219; accessToken=baihe-1537427221699-e5a06bb78317b; _fmdata=9YMB%2BufU%2Fi4Gm2L72UeCAckBNQ5bzOuzmv9VtK7zki05zclzrqEhzWoPzAi7UJki9ekiKy4WxkI%2FWQ%2FuZlEqN9vHyrZ%2F9Nh5olgJUqhyjEY%3D";
string[] cookstr = cookieStr.Split(";".ToCharArray());
foreach (string str in cookstr)
{
string[] cookieNameValue = str.Split('=');
Cookie ck = new Cookie(cookieNameValue[0].Trim().ToString(), cookieNameValue[1].Trim().ToString());
ck.Domain = "localhost";//必须写对
myCookieContainer.Add(ck);
}
}
public static string httpGet(string url)
{
string s = "";
try
{
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
req.CookieContainer = myCookieContainer;
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
// Common.cookie = resp.Headers["Set-Cookie"];
//MessageBox.Show(resp.Headers["Set-Cookie"]);
myCookieContainer.Add(resp.Cookies);
StreamReader r = new StreamReader(resp.GetResponseStream());
s = r.ReadToEnd();
r.Close();
resp.Close();
req.Abort();
}
catch (Exception ex)
{
s = ex.Message;
}
return s;
}
public static string httpPost(string url, string post)
{
string s = "";
try
{
byte[] data = Encoding.ASCII.GetBytes(post);
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
req.ContentLength = data.Length;
req.GetRequestStream().Write(data, 0, data.Length);
req.CookieContainer = myCookieContainer;
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
myCookieContainer.Add(resp.Cookies);
StreamReader r = new StreamReader(resp.GetResponseStream());
s = r.ReadToEnd();
r.Close();
resp.Close();
req.Abort();
}
catch (Exception ex)
{
s = ex.Message;
}
return s;
}
}
c# HttpWebRequest Cookie 设置到 webBrowser 控件的更多相关文章
- WPF 设置WebBrowser控件不弹脚本错误提示框
using System.Reflection; using System.Windows; using System.Windows.Controls; using System.Windows.N ...
- 完美解决C#Webbrowser控件设置Cookie问题
完美解决C#Webbrowser控件设置Cookie问题由于个人项目需求,需要把从抓包里面的Cookie数据写入到webbrowser空控件里,经过百度白百般折腾,结果还是失败,搜索到的答案基本上都是 ...
- Winfrom 中完美设置webbrowser控件内核版本
前言 .NET 版本更新了一代又一代,winform中的webbrowser控件的IE内核版本却始终用的IE7,好多网站都对IE7已经不支持.webbrowser这个控件就显得有些鸡肋,经过查找大佬门 ...
- .net 设置webbrowser控件使用的IE版本
当我们在使用.net的Webbrowser控件时,发现webbrowser展示的内容和我们用浏览器展示的内容不一致,甚至出现:'JSON' is undefined. 原因是webbrowser使用的 ...
- C# WebBrowser控件使用教程与技巧收集
常用的方法 Navigate(string urlString):浏览urlString表示的网址 Navigate(System.Uri url):浏览url表示的网址 Navigate(strin ...
- C#WebBrowser控件使用教程与技巧收集--苏飞收集
C#WebBrowser控件使用教程与技巧收集--苏飞收集 先来看看常用的方法 Navigate(string urlString):浏览urlString表示的网址 Navigate(System. ...
- C#WebBrowser控件使用教程与技巧
获取非input控件的值 webBrowser1.Document.All["控件ID"].InnerText;或webBrowser1.Document.GetElementBy ...
- Delphi WebBrowser控件的使用(大全 good)
Delphi WebBrowser控件的使用 WebBrowser控件属性:1.Application 如果该对象有效,则返回掌管WebBrowser控件的应用程序实现的自动化对象(IDis ...
- WebBrowser控件使用技巧分享
原文:WebBrowser控件使用技巧分享 在发布“淘宝登货员”时发现不少朋友对WebBrowser控件比较感兴趣,故在此分享一下使用心得. 首先分享一个WebBrowser的扩展类(此类所需的dll ...
随机推荐
- css 单行/多行文字垂直居中问题
例子可以直接看这里http://www.zhangxinxu.com/study/200911/line-height-text-v-center.html 这篇文章中有一点点解释http://blo ...
- php对文件的操作
如何让自己磁盘中的文件夹和目录显示在网页上?那就来看一下,用php是怎么来操作他们的吧 php中文件,一般包含两块内容,文件和目录先来一句一句的看代码,及他的作用 运行后看一下结果 file 指的是文 ...
- Django组件扩展 总结
1. Form组件扩展: 验证用户输入 obj = Form(reuest,POST,request.FILES) if obj.is_valid(): obj.clean_data else: ob ...
- sql Server插不进数据,以及Id自增的教程及注意事项
参考于:https://jingyan.baidu.com/article/fec4bce244f902f2608d8b7a.html 使用SQL Server 2014 数据库做web的项目出现错误 ...
- 转:.Net内存泄露原因及解决办法
1. 什么是.Net内存泄露 (1).NET 应用程序中的内存 您大概已经知道,.NET 应用程序中要使用多种类型的内存,包括:堆栈.非托管堆和托管堆.这里我们需要简单回顾一下. 以运行库为目标 ...
- 在 Virtual Box 中为 CentOS7 mini 配置双网卡
1. 配置过程 1.1 需求分析 要同时满足虚拟机访问互联网和远程连接,需要配置两块网卡. 一块为 NAT 网络,这块用来访问互联网. 另一块为 Host-Only 网络,进行远程连接. 1.2 ...
- 乘风破浪:LeetCode真题_022_Generate Parentheses
乘风破浪:LeetCode真题_022_Generate Parentheses 一.前言 关于括号的题目,我们已经遇到过了验证正确性的题目,现在让我们生成合法的括号列表,怎么办呢?想来想去还是递归比 ...
- Laravel 执行过程核心
protected function sendRequestThroughRouter($request){ $this->app->instance('request', $reques ...
- 深入浅出SharePoint2013——Search Schema配置
如果增加了新的Metadata,请上传文件到文档库后,并确保每个字段都填充了数据.然后执行全文爬网. 这样才会产生对应新的管理属性.
- November 17th, 2017 Week 46th Friday
If you shut the door to all errors, truth will be shut out. 你如果拒绝面对错误,真相也会被挡在门外. Sometimes being a f ...