c#获取httponly cookie

如何在httpwebresponse中获取httponly cookie? 习惯上我使用CookieContainer来获取httpwebresponse中的cookie,但它不能与httponly cookie一起使用。

有没有其他方式来抓住他们?

是的, 可以使用“Wininet.dll”中的“InternetGetCookieEx”函数从客户端程序中检索HTTPOnly cookie 。 你必须使用像这样的PInvoke代码:

///  /// WinInet.dll wrapper ///  internal static class CookieReader { ///  /// Enables the retrieval of cookies that are marked as "HTTPOnly". /// Do not use this flag if you expose a scriptable interface, /// because this has security implications. It is imperative that /// you use this flag only if you can guarantee that you will never /// expose the cookie to third-party code by way of an /// extensibility mechanism you provide. /// Version: Requires Internet Explorer 8.0 or later. ///  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); ///  /// Returns cookie contents as a string ///  ///  ///  public static string GetCookie(string url) { int size = 512; StringBuilder sb = new StringBuilder(size); if (!InternetGetCookieEx(url, null, sb, ref size, INTERNET_COOKIE_HTTPONLY, IntPtr.Zero)) { if (size < 0) { return null; } sb = new StringBuilder(size); if (!InternetGetCookieEx(url, null, sb, ref size, INTERNET_COOKIE_HTTPONLY, IntPtr.Zero)) { return null; } } return sb.ToString(); } } 

代码来自MSDN 。

我希望有所帮助!

您无法从CookieContainer中检索HTTPOnly Cookie。

来自MSDN

…如果您希望在响应中返回cookie,则必须始终创建CookieContainer以通过请求发送。 HTTPOnly cookie也是如此,您无法检索。