[C#] Bestaand IE cookie binden aan een WebRequest

Pagina: 1
Acties:

  • Av3ng3rtje
  • Registratie: December 2002
  • Laatst online: 05-01 22:08
Hallo mensen,

Ik wil met behulp van een WebRequest de html pagina download en regex'en , nu is de regex goed alleen ik kwam er achter dat je ingelogd moet zijn.. Als je echter een WebRequest doet gebruikt hij naar mijn weten niet de IE cookies.

Nu wil ik het zo hebben dat hij iig de cookies gebruikt van mijn IE , dus ik heb bestand ingelezen, en als value wegschreven in een Cookie object en deze megegeven aan de CookieContainer voor het WebRequest object.

Uit de HTML code blijkt dat hij hem niet pakt..

C#:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//profileurl = http://aaotracker.4players.de/usertracker.php?userid=xxxx

HttpWebRequest request = (HttpWebRequest) WebRequest.Create(profileurl);

//Cookie map achterhalen
RegistryKey key = Registry.CurrentUser.OpenSubKey("Volatile Environment");

string homedrive = (string)key.GetValue("HOMEDRIVE");
string homepath = (string)key.GetValue("HOMEPATH");

string[] splitHomepath = homepath.Split('\\');
string val = string.Empty;

string cookiePath = homedrive + homepath + "\\Cookies\\" + splitHomepath[2].ToLower() + "@aaotracker.4players[2].txt";

//Cookie value achterhalen
if (File.Exists(cookiePath)) {
    StreamReader r = new StreamReader(cookiePath);

    string l = string.Empty;

    while((l = r.ReadLine()) != null) {
    //klopt de newline van het cookie of met deze /n/r zijn ?
        val += l + "/n"; 
    }
}

//nieuwe cookiecontainer
CookieContainer cookies = new CookieContainer();

//cookie toevoegen, misschien andere naam ?, en hoe kan ik deze achterhalen?
cookies.Add(new Cookie(splitHomepath[2],val,"/","aaotracker.4players.de"));

request.Timeout = 30000;
request.CookieContainer = cookies;

Stream stream = request.GetResponse().GetResponseStream();

StreamReader reader = new StreamReader(stream);
Regex regex = new Regex("<td width=\"300\"><[a-z]?[^>]* href=\"\\.\\/webspec\\/index\\.php\\?addr\\=(?<link>.[^\"]+)\">(?<name>.+)</a></td>");

string line = string.Empty;
string serverName = string.Empty;
string serverAddress = string.Empty;  

while ((line = reader.ReadLine()) != null)
{
    Match m = regex.Match(line);

    if (m.Success)
    {
        serverName = m.Groups["name"].ToString();
        serverAddress = m.Groups["link"].ToString();

        break;
    }

}

reader.Close();


Bind ik de cookie verkeerd?

  • joopst
  • Registratie: Maart 2005
  • Laatst online: 01-10-2024
Ik heb ook zoiets gemaakt.
Mijn cookie is het cookie van de forms authentication.
code:
1
2
3
4
5
6
7
8
            myRequest.CookieContainer = New System.Net.CookieContainer

            'add the cookies we currently have :)
            currentCookie = context.Request.Cookies(System.Web.Security.FormsAuthentication.FormsCookieName)
            newCookie = New System.Net.Cookie(currentCookie.Name, currentCookie.Value, "/", context.Request.Url.Host)
            myRequest.CookieContainer.Add(newCookie)

            myResponse = CType(myRequest.GetResponse(), System.Net.HttpWebResponse)

zoals je ziet, ik haal ze gewoon uit het request ...

ik lees je verhaaltje nog een keer .. en zie dat wat ik zeg BigBullshit is :P

[ Voor 8% gewijzigd door joopst op 24-03-2006 16:40 ]


  • joopst
  • Registratie: Maart 2005
  • Laatst online: 01-10-2024
Volgens mij voeg je hem wel goed toe ..
als het niet werkt .. dan is je cookiename, value oid niet goed ...

  • Av3ng3rtje
  • Registratie: December 2002
  • Laatst online: 05-01 22:08
Ik heb eens voor de grap gekeken hoe firefox met de cookies omging en ze een andere naam gegeven en het werkte

C#:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
string cookieIEPath = homedrive + homepath + "\\Cookies\\" + splitHomepath[2].ToLower() + "@aaotracker.4players[2].txt";

if (File.Exists(cookieIEPath))
{
    StreamReader r = new StreamReader(cookieIEPath);

    string l = string.Empty;

    bool nextLineUserid = false;
    bool nextLinePassword = false;

    while ((l = r.ReadLine()) != null)
    {

        if (nextLineUserid)
        {
            userid = l;
            nextLineUserid = false;
        }
        else
        {
            if (l == "wbb_userid")
            {
                nextLineUserid = true;
            }
        }

        if (nextLinePassword)
        {
            userpassword = l;
            nextLinePassword = false;
        }
        else
        {
            if (l == "wbb_userpassword")
            {
                nextLinePassword = true;
            }
        }
    }
}


CookieContainer cookies = new CookieContainer();

cookies.Add(new Cookie("wbb_userid", userid, "/", "aaotracker.4players.de"));
cookies.Add(new Cookie("wbb_userpassword", userpassword, "/", "aaotracker.4players.de"));

request.Timeout = 30000;
request.CookieContainer = cookies;

Stream stream = request.GetResponse().GetResponseStream();

StreamReader reader = new StreamReader(stream);
Regex regex = new Regex("<td width=\"300\"><[a-z]?[^>]* href=\"\\.\\/webspec\\/index\\.php\\?addr\\=(?<link>.[^\"]+)\">(?<name>.+)</a></td>");

string line = string.Empty;
string serverName = string.Empty;
string serverAddress = string.Empty;  

while ((line = reader.ReadLine()) != null)
{
    Match m = regex.Match(line);

    if (m.Success)
    {
        serverName = m.Groups["name"].ToString();
        serverAddress = m.Groups["link"].ToString();

        break;
    }

}

reader.Close();