zoukankan      html  css  js  c++  java
  • 使用win api 返回CookieContainer

       1: [DllImport("wininet.dll", SetLastError = true)]
       2: public static extern bool InternetGetCookieEx(
       3:   string url, 
       4:   string cookieName, 
       5:   StringBuilder cookieData, 
       6:   ref int size,
       7:   Int32  dwFlags,
       8:   IntPtr  lpReserved);
       9:  
      10:  private const Int32 InternetCookieHttponly = 0x2000;
       1: /// <summary>
       2: /// Gets the URI cookie container.
       3: /// </summary>
       4: /// <param name="uri">The URI.</param>
       5: /// <returns></returns>
       6: public static CookieContainer GetUriCookieContainer(Uri uri)
       7: {
       8:     CookieContainer cookies = null;
       9:     // Determine the size of the cookie
      10:     int datasize = 8192 * 16;
      11:     StringBuilder cookieData = new StringBuilder(datasize);
      12:     if (!InternetGetCookieEx(uri.ToString(), null, cookieData, ref datasize, InternetCookieHttponly, IntPtr.Zero))
      13:     {
      14:         if (datasize < 0)
      15:             return null;
      16:         // Allocate stringbuilder large enough to hold the cookie
      17:         cookieData = new StringBuilder(datasize);
      18:         if (!InternetGetCookieEx(
      19:             uri.ToString(),
      20:             null, cookieData, 
      21:             ref datasize, 
      22:             InternetCookieHttponly, 
      23:             IntPtr.Zero))
      24:             return null;
      25:     }
      26:     if (cookieData.Length > 0)
      27:     {
      28:         cookies = new CookieContainer();
      29:         cookies.SetCookies(uri, cookieData.ToString().Replace(';', ','));
      30:     }
      31:     return cookies;
      32: }

       1: private const int INTERNET_OPTION_END_BROWSER_SESSION = 42;
       2:  
       3: [DllImport("wininet.dll", SetLastError = true)]
       4: private static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int lpdwBufferLength);
       5:  
       6: public static void ClearCookie()
       7: {
       8:     InternetSetOption(IntPtr.Zero, INTERNET_OPTION_END_BROWSER_SESSION, IntPtr.Zero, 0); 
       9: }
  • 相关阅读:
    FMDB 使用方法
    Masonry
    iOS请求服务器数据去空NSNull
    NSProgress
    根据图标名称动态设置TreeList图标
    控制显示行头列(Indicator)
    窗体内元素遍历-通用方法(DevExpress 中BarManager的遍历)
    BarManager相关使用
    C# 枚举变量
    dev TreeList 获取可视区域节点方法
  • 原文地址:https://www.cnblogs.com/yeye518/p/2808935.html
Copyright © 2011-2022 走看看