Dwa wątki z C# (HttpWebRequest)

0

Pytanie #1:

Mam taki Kod:

       CookieContainer m_ccCiastkaPoZalogowaniu;
        string m_sTokenM;

private string pobierzToken(string sWtronaWWW)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(sWtronaWWW);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader reader = new StreamReader(response.GetResponseStream());
            string kodStrony = reader.ReadToEnd();
            response.Close();
            reader.Close();
            int pozycjaStart = kodStrony.IndexOf("_csrf_token");
            kodStrony = kodStrony.Substring(pozycjaStart, kodStrony.Length - pozycjaStart);
            kodStrony = kodStrony.Remove(0, 20);
            string token = kodStrony.Substring(0, kodStrony.IndexOf("\""));
            token = token.Replace("/", "%2F");
            token = token.Replace("+", "%2B");
            token = token.Replace("=", "%3D");
            //MessageBox.Show(token);
            return token;
        }

                try
                {
                    m_sTokenM = pobierzToken("http://fileshark.pl/zaloguj");
                    string username = "sileent";
                    string password = "dude901";

                    String postData = string.Format("_username={0}&_password={1}&_csrf_token={2}&submit=Zaloguj", username, password, m_sTokenM);
                    CookieContainer tymczasoweCiastka = new CookieContainer();
                    UTF8Encoding kodowanie = new UTF8Encoding();
                    Byte[] byteData = kodowanie.GetBytes(postData);
                    HttpWebRequest postRequest = (HttpWebRequest)WebRequest.Create("http://fileshark.pl/zaloguj");
                    postRequest.Method = "POST";
                    postRequest.KeepAlive = true;
                    postRequest.CookieContainer = tymczasoweCiastka;
                    postRequest.ContentType = "application/x-www-form-urlencoded";
                    postRequest.Referer = string.Format("http://fileshark.pl/");
                    postRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.2; rv:11.0) Gecko/20100101 Firefox/11.0";
                    Stream postRequestStream = postRequest.GetRequestStream();
                    postRequestStream.Write(byteData, 0, byteData.Length);
                    postRequestStream.Close();
                    HttpWebResponse postResponse;
                    postRequest.ServicePoint.Expect100Continue = false;
                    postResponse = (HttpWebResponse)postRequest.GetResponse();
                    tymczasoweCiastka.Add(postResponse.Cookies);
                    m_ccCiastkaPoZalogowaniu = tymczasoweCiastka;

                    StreamReader postRequestReader = new StreamReader(postResponse.GetResponseStream());
                    string resultSource = postRequestReader.ReadToEnd();
                    metroTextBox1.Text = resultSource;
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }

próbuje się zalogować na podaną stronę, lecz mimo pobierania tokenu cały czas wyświetla mi stronę startową. Wydaje mi się, że gdzieś może być błąd z ciastkami ale męczę się z tym już cały dzień i nic mi nie przychodzi do głowy.

Pytanie #2

Na stronie rapidu.net jest bardzo podobny formularz lecz nie posiada on "name" same id, jak poprzez HttpWebRequest mogę się do tego zabrać aby móc się zalogować?

0

// hasła to fake oczywiście ^^

0

A sprawdzałeś Charlesem albo Fiddlerem jakie są różnice pomiędzy zapytaniem z programu a zapytaniem z przeglądarki?
Czy próbujesz to rozwiązać w ciemno, nie wiedząc nawet gdzie jest problem?

0
  1. (dotyczy też 2) Bardzo często zdarza się tak że trzeba pobrać GET stronę logowania (wprawdzie Ty to robisz pobierając token ale ciastek nie obsługujesz) bo wtedy zapisywane są potrzebne do logowania ciacha. Sprawdź z tym GET jak nie pomoże to można dalej kombinować.

  2. Nie rozumiem co znaczy nie posiada name? Tam przeglądarka wysyła:

https://rapidu.net/ajax.php?a=getUserLogin

POST /ajax.php?a=getUserLogin HTTP/1.1
Host: rapidu.net
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0
Accept: application/json, text/javascript, */*; q=0.01
Accept-Language: pl,en-US;q=0.7,en;q=0.3
Accept-Encoding: gzip, deflate
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
X-Requested-With: XMLHttpRequest
Referer: https://rapidu.net/
Content-Length: 36
Cookie: rapidu_session=i1cm7ndhdk0qo35eunkbgjkhm5
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
login=aaaa&pass=bbbb&remember=0&_go=
0

@some_ONE tak sprawdzałem program tego typu ale może coś przeoczyłem

@kAzek o tym nie pomyślałem, z tym, że trochę nie wiem jak to zrobić z całym pobieraniem getu, przetrzymywaniem go w ciastku a potem dołączeniem do mojego logowania, żeby cały czas było to jedno zdarzenie. (wciąż raczkuje w c# z tego co widzę...)
A możesz mi jeszcze powiedzieć w jaki sposób wygenerowałeś taki kod który wystawiłeś mi odnośnie pytania nr. 2?

0

a tak w ogóle jak zarbać się do tego rapidu? Bo tutaj mamy doczynienia z ajaxowym logowaniem no i niestety gdy wywołuje logowanie przez adres http://rapidu.net domyślam się, że logowanie nawet nie jest rozpoczynane. Z drugiej strony jeśli zacznę tak: https://rapidu.net/ajax.php?a=getUserLogin to nie wywołuje danej strony.

0

jeśli by Ci to pomogło to mogę dać tutaj jakieś przykładowe dane. Jeśli miałbyś ochotę

0

proszę: fileshark.pl testk:testk
rapidu: testk:testk123

0

Pisany na szybko można powiedzieć "na kolanie" ale działający kod logowania na obydwa serwisy:

        private string pobierzToken(string kodStrony)
        {
            int pozycjaStart = kodStrony.IndexOf("_csrf_token");
            kodStrony = kodStrony.Substring(pozycjaStart, kodStrony.Length - pozycjaStart);
            kodStrony = kodStrony.Remove(0, 20);
            string token = kodStrony.Substring(0, kodStrony.IndexOf("\""));
            token = token.Replace("/", "%2F");
            token = token.Replace("+", "%2B");
            token = token.Replace("=", "%3D");
            //MessageBox.Show(token);
            return token;
        }

        private void btnFileSharkLogin_Click(object sender, EventArgs e)
        {
            const string ua = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0";
            const string username = "testk";
            const string password = "testk";
            const string postDataFrmt = "_username={0}&_password={1}&_csrf_token={2}&submit=Zaloguj";
            const string preLoginURL = "http://fileshark.pl/zaloguj";
            const string loginURL = "http://fileshark.pl/login_check";

            CookieContainer tymczasoweCiastka = new CookieContainer();
            HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(preLoginURL);
            HttpWebResponse httpResponse;

            httpRequest.UserAgent = ua;
            httpRequest.Method = "GET";
            httpRequest.KeepAlive = true;
            httpRequest.CookieContainer = tymczasoweCiastka;
            httpRequest.ContentLength = 0;

            httpResponse = (HttpWebResponse)httpRequest.GetResponse();

            StreamReader httpRequestReader = new StreamReader(httpResponse.GetResponseStream());
            string resultSource = httpRequestReader.ReadToEnd();

            string m_sTokenM = pobierzToken(resultSource);

            httpResponse.Close();

            string postData = string.Format(postDataFrmt, username, password, m_sTokenM);
            Byte[] byteData = Encoding.UTF8.GetBytes(postData);

            httpRequest = (HttpWebRequest)WebRequest.Create(loginURL);

            httpRequest.UserAgent = ua;
            httpRequest.KeepAlive = true;
            httpRequest.CookieContainer = tymczasoweCiastka;
            httpRequest.ContentLength = 0;
            httpRequest.Method = "POST";
            httpRequest.ContentType = "application/x-www-form-urlencoded";
            httpRequest.Referer = preLoginURL;
            httpRequest.ContentLength = byteData.Length;

            using (var stream = httpRequest.GetRequestStream())
            {
                stream.Write(byteData, 0, byteData.Length);
            }
            httpResponse = (HttpWebResponse)httpRequest.GetResponse();

            httpRequestReader = new StreamReader(httpResponse.GetResponseStream());
            resultSource = httpRequestReader.ReadToEnd();

            httpResponse.Close();

            richTextBox1.Text = resultSource;
        }

        private void btnRapiduLogin_Click(object sender, EventArgs e)
        {
            const string ua = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0";
            const string username = "testk";
            const string password = "testk123";
            const string postDataFrmt = "login={0}&pass={1}&remember=0&_go=j";
            const string preLoginURL = "https://rapidu.net/";
            const string loginURL = "https://rapidu.net/ajax.php?a=getUserLogin";

            CookieContainer tymczasoweCiastka = new CookieContainer();
            HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(preLoginURL);
            HttpWebResponse httpResponse;

            httpRequest.UserAgent = ua;
            httpRequest.Method = "GET";
            httpRequest.KeepAlive = true;
            httpRequest.CookieContainer = tymczasoweCiastka;
            httpRequest.ContentLength = 0;

            httpResponse = (HttpWebResponse)httpRequest.GetResponse();

            StreamReader httpRequestReader = new StreamReader(httpResponse.GetResponseStream());
            string resultSource = httpRequestReader.ReadToEnd();

            //string m_sTokenM = pobierzToken(resultSource);

            httpResponse.Close();

            string postData = string.Format(postDataFrmt, username, password);
            Byte[] byteData = Encoding.UTF8.GetBytes(postData);

            httpRequest = (HttpWebRequest)WebRequest.Create(loginURL);

            httpRequest.UserAgent = ua;
            httpRequest.KeepAlive = true;
            httpRequest.CookieContainer = tymczasoweCiastka;
            httpRequest.ContentLength = 0;
            httpRequest.Method = "POST";
            httpRequest.ContentType = "application/x-www-form-urlencoded";
            httpRequest.Referer = preLoginURL;
            httpRequest.ContentLength = byteData.Length;

            using (var stream = httpRequest.GetRequestStream())
            {
                stream.Write(byteData, 0, byteData.Length);
            }
            httpResponse = (HttpWebResponse)httpRequest.GetResponse();

            httpRequestReader = new StreamReader(httpResponse.GetResponseStream());
            resultSource = httpRequestReader.ReadToEnd(); //tu pasowalo by spr. {"message":"success","redirect":""}

            httpRequest = (HttpWebRequest)WebRequest.Create(preLoginURL);

            httpRequest.UserAgent = ua;
            httpRequest.Method = "GET";
            httpRequest.KeepAlive = true;
            httpRequest.CookieContainer = tymczasoweCiastka;
            httpRequest.ContentLength = 0;

            httpResponse = (HttpWebResponse)httpRequest.GetResponse();

            httpRequestReader = new StreamReader(httpResponse.GetResponseStream());
            resultSource = httpRequestReader.ReadToEnd(); 

            httpResponse.Close();

            richTextBox1.Text = resultSource;
        }
0

jestem Twoim dłużnikiem! Dziękuje bardzo :D

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