0

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?

noseratio
  • 59,932
  • 34
  • 208
  • 486
Mr Wednesday
  • 552
  • 4
  • 16

1 Answers1

0

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.

Community
  • 1
  • 1
noseratio
  • 59,932
  • 34
  • 208
  • 486
  • It was working when the `WebBrowser` was just a control on the form. It only stopped working when I made it into a custom control along with some other controls. – Mr Wednesday Mar 02 '14 at 00:00
  • @MrWednesday, is that a fact it continues working when you revert it back to not using a custom control, or are you just assuming this? – noseratio Mar 02 '14 at 00:05
  • No, I didn't say it will work if reverted. I said it worked before being converted to a custom control using the setup I described in my question. I have just reverted the code and indeed it is still not working. It appears that the issue is not related to being in a custom control. A (not so)happy coincidence. – Mr Wednesday Mar 02 '14 at 00:23
  • @MrWednesday, I don't think you can reliably share the session/cookies between `WebBrowser` and other HTTP client APIs, besides by using `UrlMon` as described in my answer. – noseratio Mar 02 '14 at 00:25
  • Can you advise why it was working previously however now is not? – Mr Wednesday Mar 02 '14 at 00:36
  • @MrWednesday, the same thing: coincidence. As already mentioned, there are other things in HTTP protocol which make a particular HTTP session *unique*, besides cookies. – noseratio Mar 02 '14 at 00:40
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/48790/discussion-between-mr-wednesday-and-noseratio) – Mr Wednesday Mar 02 '14 at 01:06
  • @MrWednesday, that's pretty much everything I could contribute to help with this question. If you need more details about how HTTP session works, refer to [HTTP RFC](http://www.w3.org/Protocols/rfc2616/rfc2616.html). – noseratio Mar 02 '14 at 01:09