Hallo liebes Forum,
ich bin gerade dabei, Seitenaufrufe zu simulieren. Einloggen, Informationen auslesen usw. funktioniert einwandfrei. Nur sitze ich bei einem Request auf dem Trockenen.
Ich bekomme einen anderen Response zurück, als im Browser: Ich muss mich einloggen, wird angezeigt.
Zur Information, ich reiche die Cookies mit einem CookieContainer immer weiter.
Den **Request **habe ich mit dem **Entwicklertool **in **Chrome **rausgefunden:
Request URL: xxx
Request Method:POST
Status Code:200 OK
Request Headersview source
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4
Cache-Control:max-age=0
Connection:keep-alive
Content-Length:110
Content-Type:application/x-www-form-urlencoded
Cookie:lastNotification=2; shoutboxtab=0; sessionid_hr_DE=97e92d9ec9ae2807f28fbd8aa9831b6d; __utma=147192244.1771292268.1338049739.1338224802.1338234173.9; __utmb=147192244.7.10.1338234173; __utmc=147192244; __utmz=147192244.1338049739.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)
Host:xxx
Origin:xxx
Referer:xxx
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.52 Safari/536.5Form Dataview URL encoded
menge:1
id:7
cat:1
preis:10.00
preis_cent:1000
inventar_name:wodka
submitForm:Für €10,00 kaufenResponse Headersview source
Connection:close
Content-Encoding:gzip
Content-Length:8069
Content-Type:text/html
Date:Mon, 28 May 2012 19:43:15 GMT
Server:Apache/2.2.20 (Ubuntu) mod_wsgi/3.3 Python/2.6.7
Set-Cookie:sessionid_hr_DE=97e92d9ec9ae2807f28fbd8aa9831b6d; Domain=reloaded.pennergame.de; expires=Tue, 29-May-2012 01:43:15 GMT; Max-Age=21600; Path=/
Vary:Cookie,Accept-Encoding
X-SRC:R2-H36-22
Meine Funktion:
private string GetHtmlText(string url, string post, bool useCookieContainer)
{
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
Stream dataStream;
HttpWebResponse httpWebResponse;
StreamReader streamreader;
string response;
//httpWebRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
//httpWebRequest.ContentType = "application/x-www-form-urlencoded";
httpWebRequest.UserAgent = "Mozilla/5.0 Windows NT 6.1 WOW64 AppleWebKit/536.5 KHTML, like Gecko Chrome/19.0.1084.52 Safari/536.5";
if (post != null)
{
httpWebRequest.Method = "POST";
byte[] byteArrayData = Encoding.ASCII.GetBytes(post);
httpWebRequest.ContentLength = byteArrayData.Length;
dataStream = httpWebRequest.GetRequestStream();
dataStream.Write(byteArrayData, 0, byteArrayData.Length);
dataStream.Close();
}
else
{
httpWebRequest.Method = "GET";
}
if (useCookieContainer)
{
httpWebRequest.CookieContainer = this.cookies;
}
httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
if (httpWebResponse.StatusCode != HttpStatusCode.OK && httpWebResponse.StatusCode != HttpStatusCode.Found)
throw new Exception("Seite wurde nicht gefunden.");
dataStream = httpWebResponse.GetResponseStream();
streamreader = new StreamReader(dataStream, Encoding.UTF8);
response = streamreader.ReadToEnd();
streamreader.Close();
return response;
}
Mein Aufruf:
string response = GetHtmlText("xxx", "menge=1&id=7&cat=1&preis=10.00&preis_cent=1000&inventar_name=wodka", true);
Gibt es eine Sache, die ich ändern muss, damit mein Request 1zu1 mit dem vom Browser übereinstimmt?
Vielen Dank an Euch,
worlds sourcecode
Ja, du wirst einen Zugriff auf eine API gebrauchen müssen, die dir nach dem Anmelden eine Sitzungs-ID übermittelt.
Mit dieser kannst du dann dort arbeiten.
In diesem Fall sessionid_hr_DE=97e92d9ec9ae2807f28fbd8aa9831b6d ?
Api ist Neuland für mich. inwiefern funktioniert das? Link würde mir vorab reichen.
good n8!
Habs jetzt hinbekommen.
Einfach die Cookies und Header jedes Mal speichern und wiederverwenden.
Meine überarbeitete Funktion:
private string GetHtmlText(string url, string post, bool useAuthentification)
{
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse httpWebResponse;
Stream dataStream;
StreamReader streamreader;
string response;
httpWebRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/;q=0.8";
httpWebRequest.ContentType = "application/x-www-form-urlencoded";
httpWebRequest.UserAgent = "Mozilla/5.0 Windows NT 6.1 WOW64 AppleWebKit/536.5 KHTML, like Gecko Chrome/19.0.1084.52 Safari/536.5";
/*********************************** NEU ANFANG ********************************** --> */
if (useAuthentification)
{
/* restore PHPSESSID */
if (this.headers == null)
this.headers = new WebHeaderCollection();
for (int i = 0; i < this.headers.Count; i++)
{
string key = this.headers.GetKey(i);
if (key == "Set-Cookie")
{
key = "Cookie";
}
else
{
continue;
}
string value = this.headers.Get(i);
httpWebRequest.Headers.Add(key, value);
}
/* restore cookies */
if (this.cookies == null)
this.cookies = new CookieContainer();
httpWebRequest.CookieContainer = this.cookies;
}
/* <-- ********************************** NEU ENDE ***********************************/
if (post != null)
{
httpWebRequest.Method = "POST";
byte[] byteArrayData = Encoding.ASCII.GetBytes(post);
httpWebRequest.ContentLength = byteArrayData.Length;
dataStream = httpWebRequest.GetRequestStream();
dataStream.Write(byteArrayData, 0, byteArrayData.Length);
dataStream.Close();
}
else
{
httpWebRequest.Method = "GET";
}
httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
if (httpWebResponse.StatusCode != HttpStatusCode.OK && httpWebResponse.StatusCode != HttpStatusCode.Found)
throw new Exception("Seite wurde nicht gefunden.");
/*********************************** NEU ANFANG ********************************** --> */
if (useAuthentification)
{
/* save headers */
this.headers = new WebHeaderCollection();
for (int i = 0; i < httpWebResponse.Headers.Count; i++)
{
this.headers.Add(httpWebResponse.Headers.AllKeys[i], httpWebResponse.Headers.Get(i));
}
/* save cookies */
this.cookies = new CookieContainer();
foreach (Cookie cookie in httpWebResponse.Cookies)
{
this.cookies.Add(cookie);
}
}
/* <--********************************** NEU ENDE ***********************************/
dataStream = httpWebResponse.GetResponseStream();
streamreader = new StreamReader(dataStream, Encoding.UTF8);
response = streamreader.ReadToEnd();
streamreader.Close();
return response;
}
Bis dann,
worlds sourcecode