Web Scraper mit login

Lkjhgf Lkijhgfd

Grünschnabel
Hi,
wie kann man den source Code einer Seite für die man sich einloggen muss mit C# anzeigen lassen?
Ein Beispiel wäre super! Danke im Vorraus... :)
 
Zuletzt bearbeitet:
Hi,

mit "einer Seite" meinst du bestimmt eine Internetseite, oder?

Wenn ja dann guck dir mal das WebBrowser Control an.

Gruß
 
Hi,
danke für den Tipp.
habe jetzt folgenden Code der mich eigentlich auf tutorial.de einloggen soll. Leider zeigt er aber nur die Startseite an...

PHP:
private void button1_Click(object sender, EventArgs e)
{


// Cookie for our session
var cookieContainer = new CookieContainer();

// Encode post variables
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] loginDataBytes = encoding.GetBytes("vb_login_username=belaz&vb_login_password=123");

// Prepare our login HttpWebRequest
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.tutorials.de/login.php");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.CookieContainer = cookieContainer;
request.ContentLength = loginDataBytes.Length;
request.MaximumAutomaticRedirections = 10;
request.AllowAutoRedirect = true;


// Write encoded post variable to the stream
Stream newStream = request.GetRequestStream();
newStream.Write(loginDataBytes, 0, loginDataBytes.Length);
newStream.Close();

// Retrieve HttpWebResponse
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

// Link the response cookie to the domain
cookieContainer.Add(new Uri("http://www.tutorials.de/login.php"), response.Cookies);

// Prepare our navigate HttpWebRequest, and set his cookie.
HttpWebRequest requestProfile = (HttpWebRequest)WebRequest.Create("http://www.tutorials.de/login.php");
requestProfile.CookieContainer = cookieContainer;

// Retrieve HttpWebResponse
HttpWebResponse responseProfile = (HttpWebResponse)requestProfile.GetResponse();

// Retrieve stream response and read it to end
Stream st = responseProfile.GetResponseStream();

StreamReader sr = new StreamReader(st);
webBrowser1.DocumentText = sr.ReadToEnd();

}
 
Die AllowNavigation Property ist doch standartmäßig immer auf true.
Also am Ende will ich mit dem Programm auf Themen antworten können und private Nachrichten/Posts anzeigen lassen.
 
Hoi!

Entweder arbeitest du mit dem HttpWebRequest, und übernimmst die gesamte Kommunikation selbst, oder du arbeitest nur mit dem WebControl.

Geht es dir nur um den Quelltext der Seiten, ist der HttpWebRequest sinnvoller. Wenn du dann damit arbeitest, solltest dir aber auch genau ansehen welche Daten bei einem Login übertragen werden und an welche URL die Übertragung stattfindet.
Username und Passwort alleine reicht z.b. bei uns nicht aus.

Dein 2. WebRequest ist übrigens auch nur bedingt sinnvoll. Wenn du eingeloggt bist, und die login.php aufrufst, wirst auch wieder nur in den Forenindex umgeleitet. Ein anderes Ziel wäre da vielleicht besser? (Warum machst überhaupt 2? Um zu testen ob deine Cookies brav gesetzt sind?)

Hier ein Beispiel wie du dich hier im Forum einloggen kannst, und den Quelltext in eine TextBox ausgibst. (Basierend auf deinem Code.. ;))

Aber bedenke bitte, einen Missbrauch unseres Forums durch irgend ein Tool werden wir nicht dulden.

lg,..

C#:
// Cookie for our session 
var cookieContainer = new CookieContainer();

// Encode post variables 
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] loginDataBytes = encoding.GetBytes("do=login&securitytoken=guest&s=&vb_login_username=USER&vb_login_password=PWD");

// Prepare our login HttpWebRequest 
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.tutorials.de/login.php?do=login");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.CookieContainer = cookieContainer;
request.ContentLength = loginDataBytes.Length;
request.MaximumAutomaticRedirections = 10;
request.AllowAutoRedirect = true;


// Write encoded post variable to the stream 
Stream newStream = request.GetRequestStream();
newStream.Write(loginDataBytes, 0, loginDataBytes.Length);
newStream.Close();

// Retrieve HttpWebResponse 
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
	textBox1.Text = reader.ReadToEnd();
}

response.Close();
 
Hi,
danke funktioniert super!! !! :)
Wenn ich jetzt auf die selbe Weisse nen Post senden will wie finde ich raus welche Werte ich senden muss (so wie hier do=login&securitytoken=guest)? Muss ich einfach die values aller hidden input forms (securitytoken, loggedinuser, posthash, ...) mit IndexOf/ Substring herausfinden und dann senden?
 
Zuletzt bearbeitet:
Hi irgendwie funktionierts noch nicht bekomme immer den Error "Bytes to be written to the stream exceed the Content-Length bytes size specified." bei "StreamLogIn2.Write"...
Hier nochmal der Code, sry wenn ich mich so ungeschickt anstelle. :(


[...]
PHP:
int startIndexsecuritytoken = sourceCode.IndexOf("<input type=\"hidden\" name=\"securitytoken\" value=\"") + 49;
int endIndexsecuritytoken = sourceCode.IndexOf("\" />", startIndexsecuritytoken);
string toDisplaysecuritytoken = sourceCode.Substring(startIndexsecuritytoken, endIndexsecuritytoken - startIndexsecuritytoken);

richTextBox1.Text = toDisplaysecuritytoken;

int startIndext = sourceCode.IndexOf("<input type=\"hidden\" name=\"t\" value=\"") + 37;
int endIndext = sourceCode.IndexOf("\" />", startIndext);
string toDisplayt = sourceCode.Substring(startIndext, endIndext - startIndext);

richTextBox2.Text = toDisplayt;

int startIndexp = sourceCode.IndexOf("<input type=\"hidden\" name=\"p\" value=\"") + 37;
int endIndexp = sourceCode.IndexOf("\" />", startIndexp);
string toDisplayp = sourceCode.Substring(startIndexp, endIndexp - startIndexp);

richTextBox3.Text = toDisplayp;

int startIndexposthash = sourceCode.IndexOf("<input type=\"hidden\" name=\"posthash\" value=\"") + 44;
int endIndexposthash = sourceCode.IndexOf("\" />", startIndexposthash);
string toDisplayposthash = sourceCode.Substring(startIndexposthash, endIndexposthash - startIndexposthash);

richTextBox4.Text = toDisplayposthash;

int startIndexpoststarttime = sourceCode.IndexOf("<input type=\"hidden\" name=\"poststarttime\" value=\"") + 50;
int endIndexpoststarttime = sourceCode.IndexOf("\" />", startIndexpoststarttime);
string toDisplaypoststarttime = sourceCode.Substring(startIndexpoststarttime, endIndexpoststarttime - startIndexpoststarttime);

richTextBox5.Text = toDisplaypoststarttime;

int startIndexloggedinuser = sourceCode.IndexOf("<input type=\"hidden\" name=\"loggedinuser\" value=\"") + 48;
int endIndexloggedinuser = sourceCode.IndexOf("\" />", startIndexloggedinuser);
string toDisplayloggedinuser = sourceCode.Substring(startIndexloggedinuser, endIndexloggedinuser - startIndexloggedinuser);

[...]

PHP:
string newPost =
     "&securitytoken=" + toDisplaysecuritytoken +
     "&do=" + "newreply" +
     "&t=" + toDisplayt +
     "&p=" + toDisplayp +
     "&specifiedpost=" + "0" +
     "&posthash=" + toDisplayposthash +
     "&poststarttime=" + toDisplaypoststarttime +
     "&loggedinuser=" + toDisplayloggedinuser +
     "&noquote=" + "1" +
     "&vB_Editor=" + "tztz";

[...]

PHP:
// Encode post variables 
ASCIIEncoding encoding2 = new ASCIIEncoding();
byte[] loginDataBytes2 = encoding2.GetBytes(newPost);

// Link the response cookie to the domain 
//cookieContainer.Add(new Uri("http://www.tutorials.de/newreply.php?p=1929515&noquote=1"), responseLogIn.Cookies); 

// Prepare our login HttpWebRequest 
HttpWebRequest requestLogIn2 = (HttpWebRequest)WebRequest.Create("http://www.tutorials.de/newreply.php?p=1929515&noquote=1");
requestLogIn2.Method = "POST";
requestLogIn2.ContentType = "application/x-www-form-urlencoded";
requestLogIn2.CookieContainer = cookieContainer;
requestLogIn2.ContentLength = loginDataBytes.Length;
//requestLogIn.MaximumAutomaticRedirections = 10;
//requestLogIn.AllowAutoRedirect = true;

// Write encoded post variable to the stream 
Stream StreamLogIn2 = requestLogIn2.GetRequestStream();
StreamLogIn2.Write(loginDataBytes2, 0, loginDataBytes2.Length);
StreamLogIn2.Close();

// Retrieve HttpWebResponse 
HttpWebResponse responseLogIn2 = (HttpWebResponse)requestLogIn2.GetResponse();
 
In deinem letzten Codesnippet solltest du wohl
C#:
requestLogIn2.ContentLength = loginDataBytes2.Length;
verwenden.. (du setzt den ContentLength Wert auf den falschen Wert.. ;))

Aber mal eine Frage: willst du wirklich mit deinem Tool in unserem Forum posten, oder was wird das..?
 
Zurück