N4pster
N4pster

Reputation: 185

Import cookies from text file in c# Window Forms

Hi guys i want to import cookies to use with the web browser in a Vb program. These cookies are in a text file exported from foirefox using an addon called advanced cookie manager. This is the full text file content for a youtube and google account cookies.

    accounts.google.com FALSE   /   TRUE    0   GALX    6eHg_ah2OcA
accounts.google.com FALSE   /   TRUE    0   GoogleAccountsLocale_session    en
accounts.google.com FALSE   /   TRUE    0   GAPS    1:_Nr1hdAKxmmjavGHMMvU0mmYvqJE8A:Nc1AzVmN-IKZb3oq
accounts.google.com FALSE   /   TRUE    0   LSID    s.DE|s.youtube|talk|youtube:DQAAALoAAADgCb0yghM0OyhoTEOWeKd__922mYyKa3H0yBIs5jGOfSbMfP6C76Ao7YBT3chA3BA4zmqB3w5so99qg0QzJgh4LmocSsTuOc7aAraSsUH0dxfz0lJpoWxYx7f3hXI1HB2XP6YVtlpHZi-VAjsaaRDLanfpMddm73RtUnlu4xSJoNAgF0xJNogs_Of4EPAssrchOowBKcdM1-P-aQMy7rgvo5V5RF1RxTVZ8dyWRdqk8ocqEk09M-dld3fLm7cZB7g
.youtube.com    TRUE    /   FALSE   0   VISITOR_INFO1_LIVE  2S8cjkP9HuI
.youtube.com    TRUE    /   FALSE   0   use_hitbox  d5c5516c3379125f43aa0d495d100d6ddAEAAAAw
.youtube.com    TRUE    /   TRUE    0   SSID    A6maF6dfv-b3voGtv
.youtube.com    TRUE    /   TRUE    0   SAPISID q2lzZ6EO9sD4zfaO/Ag61mffuXQhzbTylQ
.youtube.com    TRUE    /   TRUE    0   HSID    Al2Ze5_OdWHfE-lKA
.youtube.com    TRUE    /   TRUE    0   APISID  dCxiK0RIWXVF4ss9/ADUW4MrNC4wbMNIZo
.youtube.com    TRUE    /   FALSE   0   PREF    f1=50000000&fv=0.0.0
.youtube.com    TRUE    /   FALSE   0   ACTIVITY    1358853848282
.youtube.com    TRUE    /   TRUE    0   demographics    b5b9b36b16b837de11794f76ac5adc24e3QGAAAAZ2VuZGVydAEAAABtdAMAAABhZ2VpHgAAADA=
.youtube.com    TRUE    /   TRUE    0   SID DQAAALkAAABYU1N6s7Wc4ckiyqekON91HZLX2WNwPAtmyu82zizR8ahweELmw13pWScKM0DWZMn3DrGU4L-ycQQpQ2EirWKdJ0zfjdGZh3iZCfYhDZcCfDgAqSmWpbGS8UW0tbkapa7hTyoSlmu342A7xWtzujGSVW94RvmXmKc7f9U6jKQ55rTABiaoiZ3DEYqDjb2pSfGu9x1wRSTpDH09nGT_rvN-IPr6Exy9aLErbk0VHhjeQVD-a93jPbKs_71T9gGmr00
.youtube.com    TRUE    /   TRUE    0   LOGIN_INFO  ad4923957f199d9967473fbb9efc6facc3cAAAB7IjEiOiAxLCAiMiI6ICJSWVh2cXhhMHRMQmZaUHowTEpSdGFRPT0iLCAiMyI6IDEzNjEyNDgyMDYsICI0IjogIkdBSUEiLCAiNiI6IGZhbHNlLCAiNyI6IDEzNTg5NTY1ODAsICI4IjogNTMwMDM1NDIxMzY3fQ==`

All i want to know is :how can i use these text files or its content with the Vb web browser.

Upvotes: 1

Views: 2648

Answers (2)

Dai
Dai

Reputation: 155558

I assume you want to use the WinForms WebBrowser control.

According to MSDN, there is no Cookies property of the WebBrower control, this is because, I assume, the WebBrowser uses the same browser-state as desktop IE... not that this matters.

According to this thread on MSDN ( http://social.msdn.microsoft.com/Forums/windows/en-US/e43239f3-3914-43ec-ac50-223b022ec2ea/webbrowser-control-and-cookies?forum=winformsdesigner ) you can add cookies by importing the InternetSetCookie function from wininet.dll.

So I'd do something like this (in C#, sorry, but converting it to VB should be easy)

// Ideally I'd parse the text-file using a finite-state-machine parser modelled on a CSV parser, but for this exercise I'll resort to `String.Split`.

private static List<String[]> GetCookieLines(String fileName) {

    List<String[]> cookieLines = new List<String[]>();
    using(FileStream fs = new FileStream( pathToYourCookies.txt, FileMode.Open ))
    using(StreamReader rdr = new StreamReader( fs, Encoding.UTF8 )) { 

        String line;
        while( (line = rdr.ReadLine()) != null ) {

            String[] components = line.Split('\t'); // assuming tab-separated file
            cookieLines.Add( components )
        }
    }
}

[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern Boolean InternetSetCookie(string urlName, string cookieName, string cookieData);

public static void ImportCookies(String fileName) {

    List<String[]> cookies = GetCookieLines( fileName );
    foreach(String[] cookie in cookies) {

        String domainName = cookie[0];
        String name       = cookie[4];
        String value      = cookie[5];

        InternetSetCookie( domainName, name, value );
    }
}

Upvotes: 6

Wiktor Zychla
Wiktor Zychla

Reputation: 48279

The InternetSetCookie function should still work

http://netpl.blogspot.com/2008/09/property-of-mshtmlihtmldocument2-does.html

You will need to parse your file (easy) and then call the method for each single cookie. The WebBrowser should pick up cookies, we've used this technique in the past several times.

Upvotes: 6

Related Questions