Laden...

HttpWebRequest POST - Login Problem

Erstellt von blutiger_anfänger vor 15 Jahren Letzter Beitrag vor 14 Jahren 6.589 Views
B
blutiger_anfänger Themenstarter:in
293 Beiträge seit 2008
vor 15 Jahren
HttpWebRequest POST - Login Problem

Einen wunderschönen Sonntag,
ich zerbrech mir hier gerade den Kopf. Ich möchte per HttpWebRequest Daten in einem geschützten Bereich anfordern, doch ich komme nicht an dem Login vorbei.

Ich bekomme immer folgende Meldung, wenn ich die POST-Variablen versende:

Der Remoteserver hat einen Fehler zurückgegeben: (417) Expectation Failed.

Vielleicht findet ja jemand von euch den Fehler...

Meine Code bis jetzt:


HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("http://www.jappy.de");
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            StreamReader sr = new StreamReader(res.GetResponseStream());

            string html = string.Empty;
            string ts = string.Empty;
            string key = string.Empty;

            //Hole Quelltext der Seite im die Parameter der HiddenInputs zu holen für die 
            //Post-Variable
            req = (HttpWebRequest)HttpWebRequest.Create("https://www.jappy.de/password");
            req.CookieContainer = cookieContainer;
            req.Method = "GET";
            req.UserAgent = "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3 (.NET CLR 3.5.30729)";
            res = (HttpWebResponse)req.GetResponse();
            sr = new StreamReader(res.GetResponseStream());
            html = sr.ReadToEnd();

            ts = html.Substring(html.IndexOf("login[ts]\" value=\"") + 18, 10);
            key = html.Substring(html.IndexOf("login[key]\" value=\"") + 19, 32);
            
            //Setze die Post-Variable zusammen
            string loginData = "login%5Bts%5D=" + ts + "&login%5Bkey%5D=" + key + "&login%5Bu%5D=" + user + "&login%5Bp%5D=" + pass + "&Submit=login";
            

            // Logge ein
            req = (HttpWebRequest)HttpWebRequest.Create("https://www.jappy.de/password");
            req.CookieContainer = cookieContainer;
            req.Method = "POST";
            req.KeepAlive = true;
            req.Referer = "https://www.jappy.de/password";
            req.UserAgent = "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3 (.NET CLR 3.5.30729)";
            req.ContentType = "application/x-www-form-urlencoded";
            ASCIIEncoding encoding = new ASCIIEncoding();
            byte[] loginDataBytes = encoding.GetBytes(loginData);
            req.ContentLength = loginDataBytes.Length;
            MessageBox.Show(req.ContentLength.ToString());
            Stream stream = req.GetRequestStream();
            stream.Write(loginDataBytes, 0, loginDataBytes.Length);
            stream.Close();
            res = (HttpWebResponse)req.GetResponse();
            
            
            

            // Überprüfe ob eingeloggt
            req = (HttpWebRequest)HttpWebRequest.Create("http://www.jappy.de");
            req.CookieContainer = cookieContainer;
            req.Method = "GET";
            req.UserAgent = "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3 (.NET CLR 3.5.30729)";
            res = (HttpWebResponse)req.GetResponse();
            sr = new StreamReader(res.GetResponseStream());
            html = sr.ReadToEnd();
            sr.Close();
            if (html.Contains("Mein Profil"))
            {
                MessageBox.Show("login ok");
            }
            else
            {
                MessageBox.Show("login error");
            }

Das originale Form sieht so aus:
[php]
<form method="post" action="/password" id="login" onsubmit="javascript:ssl();" accept-charset="UTF-8">
<input name="login[ts]" value="1234700129" type="hidden">
<input name="login[key]" value="a2cbb8a9d8c0a9dab89ec8e24014e8a5" type="hidden">
<div class="libo">
<div style="height: 10px;"></div>
<div style="text-indent: 8pt;">Login:</div>
</div>
<div class="lib">
Mitgliedsname:<br><input name="login++" class="libf" type="text"><br>

						Passwort:&lt;br&gt;&lt;input name=&quot;login[p]&quot; class=&quot;libf&quot; type=&quot;password&quot;&gt;&lt;input name=&quot;Submit&quot; value=&quot;login&quot; type=&quot;submit&quot;&gt;&lt;br&gt;  
						&lt;a href=&quot;/password&quot; class=&quot;ql&quot; style=&quot;font-size: 9px;&quot;&gt;Passwort vergessen?&lt;/a&gt;&lt;br&gt;  
																						&lt;input id=&quot;ssl_login&quot; checked=&quot;checked&quot; type=&quot;checkbox&quot;&gt;&lt;a href=&quot;/infos/ssl/&quot; class=&quot;ql&quot; style=&quot;font-size: 10px; font-family: arial;&quot;&gt;mit SSL-Verschlüsselung&lt;/a&gt;&lt;br&gt;  
											    &lt;/div&gt;  
			    &lt;/form&gt;  

[/php]

und der httprequest laut livehttpheaders so:


https://www.jappy.de/password

POST /password HTTP/1.1
Host: www.jappy.de
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3 (.NET CLR 3.5.30729)
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: de-de,de;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Referer: https://www.jappy.de/password
Cookie: __gads=ID=dd83110ec66f23f3:T=1234630405:S=ALNI_MZAJXQTJ0smQXM4Aaycw_jUzC4aGg; __utma=70291851.1543608365.1234630404.1234630404.1234697006.2; __utmz=70291851.1234630404.1.1.utmccn=(direct)|utmcsr=(direct)|utmcmd=(none); gd=62549424; __utmb=70291851; __utmc=70291851; ad=a%253A1%253A%257Bs%253A9%253A%2522adtigerfs%2522%253Ba%253A2%253A%257Bs%253A1%253A%2522a%2522%253Bi%253A5%253Bs%253A1%253A%2522z%2522%253Bi%253A1234699269%253B%257D%257D
Content-Type: application/x-www-form-urlencoded
Content-Length: 145
login%5Bts%5D=1234699268&login%5Bkey%5D=0f5b56424c57a3c88d7d177d1749901d&login%5Bu%5D=raffiausgoslar&login%5Bp%5D=Porsche01603543657&Submit=login
HTTP/1.x 200 OK
Transfer-Encoding: chunked
Content-Type: text/html; charset=utf-8
Set-Cookie: SID=4a236761177c26a19e9c9bf567975885; path=/; HttpOnly
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Content-Encoding: gzip
Vary: Accept-Encoding
Date: Sun, 15 Feb 2009 12:01:34 GMT
Server: lighttpd

ich hoffe ihr könnt mir helfen!

liebe Grüße,
Raffi

Wenn ich nicht hier bin, findest du mich auf code-bude.net.

B
blutiger_anfänger Themenstarter:in
293 Beiträge seit 2008
vor 15 Jahren

So nun klappts endlich! An dieser Stelle vielen Dank an Gordon Breuer der mir mit folgendem Artikel (http://gordon-breuer.de/post/2009/01/05/Twitter-Fehler-417-(Expectation-Failed).aspx) sehr weitergeholfen hat!

Es fehlte nur ein "simples" (darauf muss man erstmal kommen!)

System.Net.ServicePointManager.Expect100Continue = false;  

vor den HTTPrequest...

Liebe Grüße,
ein blutiger Anfänger

Wenn ich nicht hier bin, findest du mich auf code-bude.net.

K
1 Beiträge seit 2008
vor 14 Jahren

ich hab noch einen fehler entdeck.

string loginData = "login%5Bts%5D=" + ts + "&login%5Bkey%5D=" + key + "&login%5Bu%5D=" + user + "&login%5Bp%5D=" + pass + "&Submit=login";

tauschen mit

string loginData = "login%5Bts%5D=" + ts + "&login%5Bkey%5D=" + key + "&login%5Bu%5D=" + user.Text + "&login%5Bp%5D=" + pass.Text + "&Submit=login";
B
blutiger_anfänger Themenstarter:in
293 Beiträge seit 2008
vor 14 Jahren

Wieso ist das ein Fehler? user und pass sind Variablen und keine Textboxen, etc. Von daher ist doch alles ok?!

Wenn ich nicht hier bin, findest du mich auf code-bude.net.