C# Web Scraper mit login
-
Hi,
wie kann man den source Code einer Webseite (http://www.tutorials.de) für die man sich einloggen muss mit C# anzeigen lassen?
Mit meinem bisherigen Versuch klappt es leider noch nicht ganz sondern zeigt nur die Startseite an...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(); }
-
Vermutlich weil es ein CMS ist und Loginbox nur ein Modul.
Du hast immer die Startseite und das Loginmodul wird nur eingeblendet.
Was willst DU denn machen?
-
Hi,
also am Ende will ich mit dem Programm auf Themen antworten können und private Nachrichten/Posts anzeigen lassen.
-
Hi,
heute bin ich wieder dazugekommen etwas weiter zu entwickeln...
Einloggen und Seiten aufrufen klappt super blos will ich jetzt in einem Forum posten können und das tut irgendwie nicht.
Laut dem IE HTTP Analizer werden folgende Daten gesendet:title=Re%3A+%5B.NET%5D+Problem+mit+Pointer+oder+so+%C3%A4hnlich&message=qwertz&wysiwyg=0&iconid=0&s=&securitytoken=1299541972-2ad858e638eda8c0c6fa5119f0e39f08249dac76&do=postreply&t=1641679&p=13778829&specifiedpost=0&posthash=ba36d4b59e715facf9272795c6c95185&poststarttime=1299541972&loggedinuser=344783&multiquoteempty=&sbutton=Antworten&signature=1&parseurl=1&emailupdate=2
Währe super wenn jemand die Zeit hätte sich folgenden Code nochmal anzuschauen.
Code:private void button1_Click(object sender, EventArgs e) { string url = "http://board.gulli.com"; webBrowser1.DocumentText = sendRequest(url); } public string sendRequest(string url) { //LOGIN ASCIIEncoding encoding = new ASCIIEncoding(); byte[] loginData = encoding.GetBytes("do=login" + "&vb_login_username=" + id01.Text + "&vb_login_password=" + pw01.Text); CookieContainer cookieContainer = new CookieContainer(); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url + "/login.php?do=login"); request.CookieContainer = cookieContainer; request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; Stream stream = request.GetRequestStream(); stream.Write(loginData, 0, loginData.Length); stream.Close(); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); //GET PLAIN HTML request = (HttpWebRequest)WebRequest.Create(url + "/newreply.php?do=newreply&noquote=1&p=13778773"); request.CookieContainer = cookieContainer; request.Method = "GET"; response = (HttpWebResponse)request.GetResponse(); StreamReader sr = new StreamReader(response.GetResponseStream()); string sourceCode = sr.ReadToEnd(); response.Close(); // GET POST STRING byte[] postData = encoding.GetBytes(formPostMsg(sourceCode)); // SEND POST STRING request = (HttpWebRequest)WebRequest.Create(url + "/newreply.php?do=newreply&noquote=1&p=13778773"); request.CookieContainer = cookieContainer; request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; stream = request.GetRequestStream(); stream.Write(postData, 0, postData.Length); stream.Close(); response = (HttpWebResponse)request.GetResponse(); sr = new StreamReader(response.GetResponseStream()); sourceCode = sr.ReadToEnd(); response.Close(); //RETURN FINAL HTML TO WEBCONTROL return sourceCode; } public string formPostMsg(string sourceCode) { ArrayList postValue = new ArrayList(); postValue.Add("title="+"RERERERERERERE"); postValue.Add("message=" + "messagemessagemessagemessage"); postValue.Add("wysiwyg=" + "0"); postValue.Add("iconid=" + "0"); postValue.Add("s=" + ""); string startindex = "<input type=\"hidden\" name=\"securitytoken\" value=\""; int startIndex = sourceCode.IndexOf(startindex) + startindex.Length; int endIndex = sourceCode.IndexOf("\" />", startIndex); postValue.Add("securitytoken=" + sourceCode.Substring(startIndex, endIndex - startIndex)); startindex = "<input type=\"hidden\" name=\"t\" value=\""; startIndex = sourceCode.IndexOf(startindex) + startindex.Length; endIndex = sourceCode.IndexOf("\" />", startIndex); postValue.Add("t=" + sourceCode.Substring(startIndex, endIndex - startIndex)); startindex = "<input type=\"hidden\" name=\"p\" value=\""; startIndex = sourceCode.IndexOf(startindex) + startindex.Length; endIndex = sourceCode.IndexOf("\" />", startIndex); postValue.Add("p=" + sourceCode.Substring(startIndex, endIndex - startIndex)); startindex = "<input type=\"hidden\" name=\"posthash\" value=\""; startIndex = sourceCode.IndexOf(startindex) + startindex.Length; endIndex = sourceCode.IndexOf("\" />", startIndex); postValue.Add("specifiedpost=" + ""); postValue.Add("posthash=" + sourceCode.Substring(startIndex, endIndex - startIndex)); startindex = "<input type=\"hidden\" name=\"poststarttime\" value=\""; startIndex = sourceCode.IndexOf(startindex) + startindex.Length; endIndex = sourceCode.IndexOf("\" />", startIndex); postValue.Add("poststarttime=" + sourceCode.Substring(startIndex, endIndex - startIndex)); startindex = "<input type=\"hidden\" name=\"loggedinuser\" value=\""; startIndex = sourceCode.IndexOf(startindex) + startindex.Length; endIndex = sourceCode.IndexOf("\" />", startIndex); postValue.Add("loggedinuser=" + sourceCode.Substring(startIndex, endIndex - startIndex)); postValue.Add("multiquoteempty=" + ""); postValue.Add("sbutton=" + "Antworten"); postValue.Add("signature=" + "1"); postValue.Add("parseurl=" + "1"); postValue.Add("emailupdate=" + "2"); MessageBox.Show(string.Join("&", postValue.ToArray(typeof(string)) as string[])); return string.Join("&", postValue.ToArray(typeof(string)) as string[]); }