Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

.net - Windows Phone WebBrowser set cookies

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.

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.

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

Any suggestions?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...