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 ...
随机推荐
- [基础架构]PeopleSoft Web Server 重要文件说明
我们都知道PeopleSoft是由几个不同的服务组成的,他们在PeopleSoft体系结构中扮演着自己的角色.这些服务具有不同的文件结构并包含重要的可执行文件和配置文件. 以下是Peoplesoft体 ...
- python实现分页插件
class Pages: def __init__(self, current_page, data_count, per_page_count=10, pager_num=7):#pager_num ...
- Python 基于Python实现Ftp文件上传,下载
基于Python实现Ftp文件上传,下载 by:授客 QQ:1033553122 测试环境: Ftp客户端:Windows平台 Ftp服务器:Linux平台 Python版本:Python 2.7 ...
- input radio单选框样式优化
HTML代码: <form> <div> <input id="item1" type="radio" name="it ...
- go语言练习:条件语句和循环语句
1.for循环+if条件语句简单例子: package main import "fmt" func main() { var a int for a = 0; a <= 2 ...
- python基础一数据类型之字符串
摘要: python基础一中有字符串,所以这篇主要讲字符串. 一,字符串的注释 二,字符串的索引与切片 三,字符串的方法 一,字符串的注释 单引号 双引号 三引号都可以用户定义字符串.三引号不仅可以定 ...
- js拼接table查询信息部分
其一: $("#datagrid").empty(); var a=0; <order-rows> a++; $("#datagrid").appe ...
- NXlog配置
NXlog文档: http://nxlog.org/docs/nxlog-ce/nxlog-reference-manual.html https://nxlog.org/documentation/ ...
- Centos7 永久更改主机名
操作环境: [root@bogon ~]# uname -a Linux #localhost.localdomain 3.10.0-514.el7.centos.plus.i686 #1 SMP W ...
- extern “C”的作用详解
extern "C"的主要作用就是为了能够正确实现C++代码调用其他C语言代码.加上extern "C"后,会指示编译器这部分代码按C语言的进行编译,而不是C+ ...