Logowanie się do panelu użytkownika w Wordpress.

0

EDIT: Udało mi się ;).

Cześć.
Nie mogę się zalogować do panelu użytkownika w Wordpress. Używając prostego HttpWebRequest lub przedefiniowanego WebClient nie da się zalogować na Wordpress. Przekierowuje do strony logowania. Wg mnie po prostu nie zapisuje wszystkich ciastek.
Napisałem kod, w którym używam TcpClient. Nie wiem jak wyłuskać odpowiednio ciastka i je później wykorzystać... Oto kod:

Tu był kiedyś kod!

Odpowiedź, którą dostałem od przykładowego bloga:

HTTP/1.1 302 Moved Temporarily
Date: Wed, 12 Sep 2012 20:59:04 GMT
Server: Apache
Expires: Wed, 11 Jan 1984 05:00:00 GMT
Cache-Control: no-cache, must-revalidate, max-age=0
Pragma: no-cache
X-Frame-Options: SAMEORIGIN
Set-Cookie: wordpress_test_cookie=WP+Cookie+check; path=/
Set-Cookie: wordpress_82b4f82eecbf2dcef94b8254bf517f02=LoginName%7C1348693146%7C09090648b21c25e3c1e15c3ec959830a; expires=Wed, 26-Sep-2012 20:59:06 GMT; path=/wp-content/plugins; httponly
Set-Cookie: wordpress_82b4f82eecbf2dcef94b8254bf517f02=LoginName%7C1348693146%7C09090648b21c25e3c1e15c3ec959830a; expires=Wed, 26-Sep-2012 20:59:06 GMT; path=/wp-admin; httponly
Set-Cookie: wordpress_logged_in_82b4f82eecbf2dcef94b8254bf517f02=LoginName%7C1348693146%7Ccea96d72d3703e50e1331db455180391; expires=Wed, 26-Sep-2012 20:59:06 GMT; path=/; httponly
Last-Modified: Wed, 12 Sep 2012 20:59:06 GMT
Location: http://costam.com/wp-admin/
Content-Length: 0
Keep-Alive: timeout=5, max=75
Connection: Keep-Alive
Content-Type: text/html; charset=UTF-8

Jak to teraz "zamienić" w CookieContainer? Nie mam pojęcia. Zgłupiałem.

Proszę o pomoc.

EDIT: Udało mi się ;). Emacsem przez sendmail()

0

Newbie to to nie jest...
Postanowiłem podać kod innym użytkownikom:

private bool LogIn()
        {
            string lgnSrc = webHelper.DownloadStuff(MainUrl + "wp-login.php");
            int crackedNumber = this.CrackCaptcha(lgnSrc);
            if (crackedNumber == -1) return false;
            string cptchResult = Regex.Match(lgnSrc, "name=(?:\"|')cptch_result(?:\"|') value=(?:\"|')(?<result>[^\"']+)(?:\"|')").Groups["result"].Value;

            string fulladdress = MainUrl.Replace("http://", "").Replace("/", "");
            string username = HttpUtility.UrlEncode(UserName);
            string password = HttpUtility.UrlEncode(PassWord);

            string formdata = "log=" + HttpUtility.UrlEncode(UserName) + "&pwd=" + HttpUtility.UrlEncode(PassWord) + "&cptch_result=" + HttpUtility.UrlEncode(cptchResult) + "&cptch_number=" + crackedNumber + "&rememberme=forever&wp-submit=Log+In&redirect_to=http%3A%2F%2F300mblinks.com%2Fwp-admin%2F&testcookie=1";
            formdata = string.Format(formdata, username, password, fulladdress);
            IPHostEntry entry = Dns.GetHostEntry(fulladdress);

            NetworkStream networkStream;
            string responseStr = string.Empty;
            using (TcpClient client = new TcpClient(fulladdress, 80))
            {
                client.SendTimeout = 30000;
                client.ReceiveTimeout = 30000;

                client.Client.Send(new ASCIIEncoding().GetBytes("POST " + MainUrl + "wp-login.php HTTP/1.1\nHost: 300mblinks.com\nConnection: close\nContent-Type: application/x-www-form-urlencoded\nContent-Length: " + formdata.Length + "\n\n" + formdata));

                networkStream = client.GetStream();

                byte[] buffer = new byte[6666];

                System.Threading.Thread.Sleep(1000);

                int bytesRead = networkStream.Read(buffer, 0, buffer.Length);
                responseStr = Encoding.ASCII.GetString(buffer, 0, bytesRead);
            }

            List<String> cookieHeaders = new List<string>();
            foreach (string header in responseStr.Split("\n\r".ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
            {
                if (header.StartsWith("Set-Cookie"))
                {
                    cookieHeaders.Add(header.Replace("Set-Cookie: ", ""));
                }
            }

            CookieContainer jar = new CookieContainer();
            foreach (string cook in cookieHeaders)
            {
                string name, value, path, domain;
                name = value = path = domain = "";

                string[] split = cook.Split(';');
                foreach (string part in split)
                {
                    if (part.StartsWith(" path="))
                    {
                        path = part.Replace(" path=", "").Trim();
                    }
                    if (part.StartsWith(" domain="))
                    {
                        domain = part.Replace(" domain=", "").Trim();
                    }
                    if (!part.StartsWith(" path=") && !part.StartsWith(" domain=") && part.Contains("=") && !part.Contains("expires"))
                    {
                        name = part.Split('=')[0].Trim();
                        value = part.Split('=')[1].Trim();
                    }
                }

                Cookie cookie = new Cookie(name, value, path, fulladdress);
                jar.Add(cookie);
            }

            webHelper.SetCookieContainer(jar);
            string src = webHelper.DownloadStuff(MainUrl);
            if (!src.Contains("Howdy, " + UserName)) return false;

            return true;
        }

1 użytkowników online, w tym zalogowanych: 0, gości: 1