最近做项目的时候需要模拟post请求取数据但用了普通的Cookies无法获取完整的Cookies信息 只是获取了一部分 ,导致取回来的是重新登陆的页面。后来经过不懈的精神,终于找到了方法实现获取HTTPOnly,下面直接贴代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
namespace HQTX_BatchAddData
{undefined
/// <summary>
/// WinInet.dll wrapper
/// </summary>
internal static class CookieReader
{undefined
private const int INTERNET_COOKIE_HTTPONLY = 0x00002000;
[DllImport("wininet.dll", SetLastError = true)]
private static extern bool InternetGetCookieEx(
string url,
string cookieName,
StringBuilder cookieData,
ref int size,
int flags,
IntPtr pReserved);
public static string GetCookie(string url)
{undefined
int size = 512;
StringBuilder sb = new StringBuilder(size);
if (!InternetGetCookieEx(url, null, sb, ref size, INTERNET_COOKIE_HTTPONLY, IntPtr.Zero))
{undefined
if (size < 0)
{undefined
return null;
}
sb = new StringBuilder(size);
if (!InternetGetCookieEx(url, null, sb, ref size, INTERNET_COOKIE_HTTPONLY, IntPtr.Zero))
{undefined
return null;
}
}
return sb.ToString();
}
}
}
————————————————
版权声明:本文为CSDN博主「_陈陆亮」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/chenluliang/article/details/53335084
评论区