日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C#用WebBrowser与WIN API辅助模拟获取网站完整Cookie

發布時間:2024/9/20 C# 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#用WebBrowser与WIN API辅助模拟获取网站完整Cookie 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

網上找到的可以完整獲取Cookie的方法,轉載一下希望能幫助更多人. 親測可用

在Winform中使用WebBrowser控件獲取網站的Cookie有時候是不完整的,默認調用Document.Cookie也取不到Cookie,其中就是因為有些網站對于關鍵Cookie做了保護,為Cookie加上了HttpOnly的屬性,HttpOnly可以防止cookie被“讀取”,這時我們就需要利用WIN API用來輔助獲取網站的完整Cookie了。

using System; using System.ComponentModel; using System.Net; using System.Runtime.InteropServices; using System.Security; using System.Security.Permissions; using System.Text; namespace Freecms {internal sealed class NativeMethods{#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}/// <SUMMARY></SUMMARY> /// 取得WebBrowser的完整Cookie。 /// 因為默認的webBrowser1.Document.Cookie取不到HttpOnly的Cookie /// public class FullWebBrowserCookie{[SecurityCritical]public static string GetCookieInternal(Uri uri, bool throwIfNoCookie){uint pchCookieData = 0;string url = UriToString(uri);uint flag = (uint)NativeMethods.InternetFlags.INTERNET_COOKIE_HTTPONLY;//Gets the size of the string builder if (NativeMethods.InternetGetCookieEx(url, null, null, ref pchCookieData, flag, IntPtr.Zero)){pchCookieData++;StringBuilder cookieData = new StringBuilder((int)pchCookieData);//Read the cookie if (NativeMethods.InternetGetCookieEx(url, null, cookieData, ref pchCookieData, flag, IntPtr.Zero)){DemandWebPermission(uri);return cookieData.ToString();}}int lastErrorCode = Marshal.GetLastWin32Error();if (throwIfNoCookie || (lastErrorCode != (int)NativeMethods.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();}} }

調用方法(webBrowser1.Url為webBrowser1控件登陸后包含cookie信息的Url):

string pCookie=Freecms.FullWebBrowserCookie.GetCookieInternal(webBrowser1.Url, false);

總結

以上是生活随笔為你收集整理的C#用WebBrowser与WIN API辅助模拟获取网站完整Cookie的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。