InternetSetCookie返回false后,GetLastError返回成功

时间:2018-11-16 15:49:18

标签: c# .net wininet

使用.NET 4.6.2,用C#编码

仅少数客户在他们的机器上遇到问题。当用户位于“本地管理员”组中时,一切正常,但是当他们从“本地管理员”中删除它们时,将显示InternetSetCookie返回false,但是GetLastWin32Error返回成功。

我得出的结论是InternetSetCookie返回的是false,因为我看不到它上面的任何一行都可能引发这种异常。但是我想一切皆有可能。

[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern bool InternetSetCookie(string lpszUrl, string lpszCookieName, string lpszCookieData);

private bool SetUriCookie(CookieContainer container, Uri uri, string cookieName){
    bool bRetVal = false;
    try{
        Cookie cookie = container.GetCookies(uri)[cookieName];
        string cookieValue = cookie == null ? "" : cookie.Value;
        DateTime cookieExpire = cookie == null ? DateTime.Now.AddMinutes(2) : cookie.Expires;
        string cookieData = string.Format("{0};expires={1}", cookieValue, cookieExpire.ToString("R"));
        bRetVal = InternetSetCookie(uri.ToString(), cookieName, cookieData);
        if (!bRetVal) throw new Win32Exception(Marshal.GetLastWin32Error());
    } catch (Exception e) {
        Logger.LogError(e, "Exception thrown attempting to store '{0}' cookie", cookieName);
    }
    return bRetVal;
}

这是记录的内容:

  

尝试存储“ xxxxx” cookie时引发异常错误:操作成功完成

即使API返回false,我也有理由相信正在存储cookie。我尚未确认。关于我应该如何进行的任何建议?

0 个答案:

没有答案
相关问题