Windows Phone的web浏览器设置cookies [英] Windows Phone WebBrowser set cookies

查看:310
本文介绍了Windows Phone的web浏览器设置cookies的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的HttpWebRequest的,它使用依赖于JSESSIONID cookie的一些基本的身份验证REST服务。我需要传递的cookie来WebBrowser控件重用会议,但没有发现任何可用的解决方案,让我通过在浏览器的cookie存储该Cookie。

I am using HttpWebRequest for REST services which uses some basic authentication that relies on JSESSIONID cookie. I need to pass that cookie to WebBrowser control to reuse that session, but didn't find any usable solution that would allow me to pass that cookie in the Browser's cookie store.

有什么办法?我现在能想到的唯一的办法就是使用Naviagate(URL,空,MANUALLY_CONSTRUCTED_HEADER),这是一种暴力。

Is there any way? The only way that I can think of now is to use Naviagate(url, null, MANUALLY_CONSTRUCTED_HEADER) which is kind of brute-force.

是的,真的要使用web浏览器进行这样的动作。

Yes, and really have to use WebBrowser for this kind of action.

有什么建议?

推荐答案

另一个想法是,你可能能够通过JavaScript添加的cookies。你应该能够做到这一点(提供IsScriptEnabled是真的在你的浏览器控件):

Another thought is that you might be able to add cookies via JavaScript. You should be able to do this (provided IsScriptEnabled is true on your browser control):

private void setCookie(string name, string value, string path = "", string domain = "", bool isSecure=false, string expires = "")
{
        var sb = new StringBuilder();
        sb.AppendFormat("document.cookie = '{0}=\" + escape(\"{1}\")", name, value);
        if (!String.IsNullOrEmpty(expires))
            sb.AppendFormat(";expires=\"{0}\"", expires); // should be a GMTString
        if (!String.IsNullOrEmpty(path))
            sb.AppendFormat(";path=\"{0}\"", path); 
        if (!String.IsNullOrEmpty(domain))
            sb.AppendFormat(";domain=\"{0}\"", domain);
        if (isSecure)
            sb.Append(";secure'");
        var cookieJs = sb.ToString();
        Debug.WriteLine(cookieJs);
        webBrowser.InvokeScript(cookieJs);
}

这篇关于Windows Phone的web浏览器设置cookies的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆