时间:2021-05-28
一、接口文件
复制代码 代码如下:
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();
}
}
}
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文介绍了asp.net获取客户端浏览器访问的IP地址的实例代码,分享给大家,自己也留个笔记1、js方法Documentdocument.write('IP地址
1、COOKIE信息保存支持设置HttpOnly属性,后台直接设置。开启后JS无法获取COOKIE信息,更安全。2、COOKIE信息保存支持设置secure属性
有httponly的cookie,在httpwebreqeust请求时,会获取不到,可以采用直接获取head中的set-cookie,再转换成Cookie添加到
找Cookie就等于具有了session火狐浏览器的Cookie谷歌浏览器的CookieNetwork点击URL再点Headers不同链接产生的Cookie不同
使用已有cookie登陆使用浏览器登陆,获取浏览器中的cookie信息,来进行登陆。我们以博客园为例,先登录博客园账号。我们访问随笔列表,在控制台我们可以看到我