[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 控件的更多相关文章

  1. WPF 设置WebBrowser控件不弹脚本错误提示框

    using System.Reflection; using System.Windows; using System.Windows.Controls; using System.Windows.N ...

  2. 完美解决C#Webbrowser控件设置Cookie问题

    完美解决C#Webbrowser控件设置Cookie问题由于个人项目需求,需要把从抓包里面的Cookie数据写入到webbrowser空控件里,经过百度白百般折腾,结果还是失败,搜索到的答案基本上都是 ...

  3. Winfrom 中完美设置webbrowser控件内核版本

    前言 .NET 版本更新了一代又一代,winform中的webbrowser控件的IE内核版本却始终用的IE7,好多网站都对IE7已经不支持.webbrowser这个控件就显得有些鸡肋,经过查找大佬门 ...

  4. .net 设置webbrowser控件使用的IE版本

    当我们在使用.net的Webbrowser控件时,发现webbrowser展示的内容和我们用浏览器展示的内容不一致,甚至出现:'JSON' is undefined. 原因是webbrowser使用的 ...

  5. C# WebBrowser控件使用教程与技巧收集

    常用的方法 Navigate(string urlString):浏览urlString表示的网址 Navigate(System.Uri url):浏览url表示的网址 Navigate(strin ...

  6. C#WebBrowser控件使用教程与技巧收集--苏飞收集

    C#WebBrowser控件使用教程与技巧收集--苏飞收集 先来看看常用的方法 Navigate(string urlString):浏览urlString表示的网址 Navigate(System. ...

  7. C#WebBrowser控件使用教程与技巧

    获取非input控件的值 webBrowser1.Document.All["控件ID"].InnerText;或webBrowser1.Document.GetElementBy ...

  8. Delphi WebBrowser控件的使用(大全 good)

    Delphi WebBrowser控件的使用 WebBrowser控件属性:1.Application      如果该对象有效,则返回掌管WebBrowser控件的应用程序实现的自动化对象(IDis ...

  9. WebBrowser控件使用技巧分享

    原文:WebBrowser控件使用技巧分享 在发布“淘宝登货员”时发现不少朋友对WebBrowser控件比较感兴趣,故在此分享一下使用心得. 首先分享一个WebBrowser的扩展类(此类所需的dll ...

随机推荐

  1. CentOS7下 将django工程部署到Apache2.4上

    因为需要写一个网站,考虑到也没写过其他的语言,就直接采用了python,说起python的框架,就是大名鼎鼎的Django啦. 工程所采用的版本是python 2.7,django 是1.8,wind ...

  2. phpstudy集成下Apache配置部署https安全证书

    一..先申请到安全证书.(腾讯云或者阿里云申请免费1年的安全证书),怎么申请这里也说下(以腾讯云为例): 1.登录腾讯云QQ或微信登录都行,第一次登录要通过实名认证,点击[产品]---[ss证书l] ...

  3. Maven学习(三)maven原理概念详述

    maven相关概念 maven坐标 Maven世界拥有大量构建,当我们需要引用依赖包是,需要用一个用来唯一标识去确定唯一的一个构建.如果拥有了统一规范,就可以把查找工作交给机器. 类似于空间找点的坐标 ...

  4. singleInstance和singleTask导致startActivityForResult回调失败

    先来了解下这两种启动模式: 1.singleInstance,全局唯一,它的实例在全局(即在众多任务栈中)是唯一的,它单独地存在于属于自己的任务栈中,而且这个任务栈没有其他实例. 2.singleTa ...

  5. Ionic 启动及应用图标

    1.在项目的根目录下创建resources文件夹. 2.在文件夹中都放入icon.png(应用图标,最小192x192px,不带圆角),splash.png(启动屏幕,最小2208x2208px,中间 ...

  6. .NET笔试题集(五)

    转载于:http://www.cnblogs.com/ForEvErNoME/archive/2012/09/15/2684938.html 1.什么是受管制的代码? 答:unsafe:非托管代码.不 ...

  7. 准备好要上传到 Azure 的 Windows VHD 或 VHDX

    在将 Windows 虚拟机 (VM) 从本地上传到 Azure 之前,必须准备好虚拟硬盘(VHD 或 VHDX). Azure 仅支持采用 VHD 文件格式且具有固定大小磁盘的第 1 代 VM. V ...

  8. Oracle EBS AR 收款调整取值

    SELECT ct.trx_number ,adj.adjustment_number ,ad.amount_dr ,ad.amount_cr ,ad.source_table ,ad.source_ ...

  9. .Net 环境

    更多系统版本下载:https://www.microsoft.com/net/download VSCode :https://code.visualstudio.com/

  10. OpenResty 安装配置

    0. 说明 1. Windows 下安装 下载软件包 openresty-1.13.6.1-win32.zip ,解压即可食用. [开启] 直接运行 nginx.exe 在 Windows 的命令窗口 ...