使用.net怎么获取浏览器的Cookie?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:国际域名空间、虚拟空间、营销软件、网站建设、四平网站维护、网站推广。一、接口文件
using System;
using System.ComponentModel;
using System.Net;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Permissions;
using System.Text;
namespace CookieHandler
{
internal sealed class INativeMethods
{
#region enums
public enum ErrorFlags
{
ERROR_INSUFFICIENT_BUFFER = 122,
ERROR_INVALID_PARAMETER = 87,
ERROR_NO_MORE_ITEMS = 259
}
public enum InternetFlags
{
INTERNET_COOKIE_HTTPONLY = 8192, //Requires IE 8 or higher
INTERNET_COOKIE_THIRD_PARTY = 131072,
INTERNET_FLAG_RESTRICTED_ZONE = 16
}
#endregion
#region DLL Imports
[SuppressUnmanagedCodeSecurity, SecurityCritical, DllImport("wininet.dll", EntryPoint = "InternetGetCookieExW", CharSet = CharSet.Unicode, SetLastError = true, ExactSpelling = true)]
internal static extern bool InternetGetCookieEx([In] string Url, [In] string cookieName, [Out] StringBuilder cookieData, [In, Out] ref uint pchCookieData, uint flags, IntPtr reserved);
#endregion
}
}
二、获取cookie类
复制代码 代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Net;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Permissions;
using System.Text;
namespace CookieHandler
{
/// <SUMMARY></SUMMARY>
/// 取得WebBrowser的完整Cookie。
/// 因为默认的webBrowser1.Document.Cookie取不到HttpOnly的Cookie
/// IE7不兼容,IE8可以,其它未知
///
public class FullWebBrowserCookie
{
public static Dictionary<string, string> GetCookieList(Uri uri, bool throwIfNoCookie)
{
Dictionary<string, string> dict = new Dictionary<string, string>();
string cookie = GetCookieInternal(uri, throwIfNoCookie);
Console.WriteLine("FullWebBrowserCookie - 所有cookie:" + cookie);
string[] arrCookie = cookie.Split(';');
foreach (var item in arrCookie)
{
string[] arr = item.Split('=');
string key = arr[0].Trim();
string val = "";
if (arr.Length >= 2)
{
val = arr[1].Trim();
}
if (!dict.ContainsKey(key))
{
dict.Add(key, val);
}
}
Console.WriteLine("FullWebBrowserCookie - cookie已载入dict,共" + dict.Count.ToString() + "项");
return dict;
}
public static string GetCookieValue(string key, Uri uri, bool throwIfNoCookie)
{
Console.WriteLine("GetCookieValue");
Dictionary<string, string> dict = GetCookieList(uri, throwIfNoCookie);
if (dict.ContainsKey(key))
{
return dict[key];
}
return "";
}
[SecurityCritical]
public static string GetCookieInternal(Uri uri, bool throwIfNoCookie)
{
Console.WriteLine("GetCookieInternal");
uint pchCookieData = 0;
string url = UriToString(uri);
uint flag = (uint)INativeMethods.InternetFlags.INTERNET_COOKIE_HTTPONLY;
//Gets the size of the string builder
if (INativeMethods.InternetGetCookieEx(url, null, null, ref pchCookieData, flag, IntPtr.Zero))
{
pchCookieData++;
StringBuilder cookieData = new StringBuilder((int)pchCookieData);
//Read the cookie
if (INativeMethods.InternetGetCookieEx(url, null, cookieData, ref pchCookieData, flag, IntPtr.Zero))
{
DemandWebPermission(uri);
return cookieData.ToString();
}
}
int lastErrorCode = Marshal.GetLastWin32Error();
if (throwIfNoCookie || (lastErrorCode != (int)INativeMethods.ErrorFlags.ERROR_NO_MORE_ITEMS))
{
throw new Win32Exception(lastErrorCode);
}
return null;
}
private static void DemandWebPermission(Uri uri)
{
string uriString = UriToString(uri);
if (uri.IsFile)
{
string localPath = uri.LocalPath;
new FileIOPermission(FileIOPermissionAccess.Read, localPath).Demand();
}
else
{
new WebPermission(NetworkAccess.Connect, uriString).Demand();
}
}
private static string UriToString(Uri uri)
{
if (uri == null)
{
throw new ArgumentNullException("uri");
}
UriComponents components = (uri.IsAbsoluteUri ? UriComponents.AbsoluteUri : UriComponents.SerializationInfoString);
return new StringBuilder(uri.GetComponents(components, UriFormat.SafeUnescaped), 2083).ToString();
}
}
}
关于使用.net怎么获取浏览器的Cookie问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。
分享名称:使用.net怎么获取浏览器的Cookie-创新互联
本文来源:https://www.cdcxhl.com/article24/ddggje.html
成都网站建设公司_创新互联,为您提供ChatGPT、搜索引擎优化、品牌网站制作、品牌网站建设、移动网站建设、建站公司
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联