I'm trying to share a session cookie between my HttpWebRequest
and WebBrowser
control. It was working fine until I moved the WebBrowser
into a custom control.
When I try to add the cookie, it does not get set. I have confirmed the value of the cookie I'm sending, it just doesn't add to the browser's cookie string.
Cookie is generated:
// cookieJar used for the previous HttpWebRequest
CookieCollection cc = cookieJar.GetCookies(the_url);
StringBuilder sb = new StringBuilder();
foreach (Cookie cook in cc)
{
sb.Append(cook.ToString() + ";");
}
// Confirmed cookie value below is correct: "PHPSESSID=21....";
return sb.ToString();
Cookie is sent to custom control:
// bc is my custom BrowserControl()
bc.add_cookie(c); // Fails in here
Cookie is added to WebBrowser (not working):
// Custom control method
public void add_cookie(string c)
{
// I check both values here - all looks correct
browser.Document.Cookie = c;
// I check both values again here - nothing has changed
}
Browser's cookie value (before and after - no change occurs):
// google analytics cookie (I believe)
__utma=323...; __utmb=235235...; __utmz=25235.236523.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utmc=42346346
Keep in mind this code was working before I moved the WebBrowser to a custom control. If there's no solution I can revert the changes but I am trying to avoid that.
In short: Why isn't the cookie being added to the WebBrowser only when it's inside a custom control?
Cookies is not everything, there's also session authentication cache which you cannot copy. WebBrowser
uses UrlMon
library, so the right way of doing this would be to use other UrlMon
APIs like URLOpenStream
, URLDownloadToFile
or URLDownloadToCacheFile
.
This approach allows to download any resource on the same session with the WebBrowser
control. The UrlMon
APIs can be called from C# via P/invoke, here is related answer . It is also possible to upload data on the same session.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.