我在 XBAP 应用程序中有一个 WebBrowser 控件(System.Windows.Forms.WebBrowser + WindowsFormsHost)。问题是当我“Navigate()”到需要 cookie 的网站并输入凭据时,WebBrowser 没有在 GET (HTTP) 方法的“Cookie”属性中设置“会话”编号参数,登录失败。我怀疑 WebBrowser 不接受 cookie(或会话 cookie?)。我尝试以这种方式使用 IInternetSecurityManager:
public static bool TryAllowCookies(Uri uri)
{
IInternetSecurityManager securityManager = (IInternetSecurityManager)new InternetSecurityManager();
int policy = 0;
Guid context = Guid.Empty;
int hr = securityManager.ProcessUrlAction(uri.ToString(),
URLACTION_COOKIES_SESSION | URLACTION_COOKIES_ENABLED | URLACTION_COOKIES |
URLACTION_COOKIES_SESSION_THIRD_PARTY | URLACTION_COOKIES_THIRD_PARTY,
ref policy,
Marshal.SizeOf(policy),
ref context,
Marshal.SizeOf(context),
PUAF_DEFAULT, 0);
return (hr == 0) && policy == URLPOLICY_ALLOW;
}
ProcessUrlAction 总是返回 True,但不起作用。
我没有找到如何将 IInternetSecurityManager 与 WebBrowser 控件一起使用的完整示例。而在 Windows 窗体应用程序中,相同的控件可以完美运行。
HTTP 嗅探:
来自 XBAP 应用程序的网络浏览器(不起作用):
GET /cgi-bin/xxx.xxx HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-xpsdocument, application/x-ms-application, application/x-ms-xbap, application/xaml+xml, application/msword, application/vnd.ms-powerpoint, application/vnd.ms-excel, */*
Referer: http://xxx.xxx.xxx.xxx/cgi-bin/yyy.yyy
Accept-Language: it
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET4.0E; .NET4.0C; InfoPath.2)
Host: xxx.xxx.xxx.xxx
Connection: Keep-Alive
Cookie: language=it; user=smith
来自 WINDOWS 表单应用程序 (WORKS) 的网络浏览器
GET /cgi-bin/xxx.xxx HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-xpsdocument, application/x-ms-application, application/x-ms-xbap, application/xaml+xml, application/msword, application/vnd.ms-powerpoint, application/vnd.ms-excel, */*
Referer: http://xxx.xxx.xxx.xxx/cgi-bin/yyy.yyy
Accept-Language: it
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET4.0E; .NET4.0C; InfoPath.2)
Host: xxx.xxx.xxx.xxx
Connection: Keep-Alive
Cookie: language=en; user=smith; session=2a9cffb0babea74e3432fc9805470faa; current_page=
谢谢你,里卡多