Announcement

Collapse
No announcement yet.

Web Scraper mit login

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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...

    Code:
    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(); 
    
    }

  • #2
    Nimm doch das WebBrowser/IE-Control des .NET-Framworks. Dann bist du schon mal das Session-Handling los. Und über das Document-Property bekommst du zugriff die geladene HTML-Seite.

    Comment


    • #3
      Danke für die Antwort aber wie kann ich mich damit dann mit meinen im Programm vordefinierten Logindaten einloggen? Müsste man das dann nicht jedes mal manuell machen?

      Comment


      • #4
        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:

        Code:
        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:
        PHP Code:
               private void button1_Click(object senderEventArgs 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(loginData0loginData.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(postData0postData.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(startIndexendIndex startIndex));
                        
        startindex "<input type=\"hidden\" name=\"t\" value=\"";
                        
        startIndex sourceCode.IndexOf(startindex) + startindex.Length;
                        
        endIndex sourceCode.IndexOf("\" />"startIndex);
                    
        postValue.Add("t=" sourceCode.Substring(startIndexendIndex startIndex));
                        
        startindex "<input type=\"hidden\" name=\"p\" value=\"";
                        
        startIndex sourceCode.IndexOf(startindex) + startindex.Length;
                        
        endIndex sourceCode.IndexOf("\" />"startIndex);
                    
        postValue.Add("p=" sourceCode.Substring(startIndexendIndex 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(startIndexendIndex startIndex)); 
                        
        startindex "<input type=\"hidden\" name=\"poststarttime\" value=\"";
                        
        startIndex sourceCode.IndexOf(startindex) + startindex.Length;
                        
        endIndex sourceCode.IndexOf("\" />"startIndex);
                    
        postValue.Add("poststarttime=" sourceCode.Substring(startIndexendIndex startIndex)); 
                        
        startindex "<input type=\"hidden\" name=\"loggedinuser\" value=\"";
                        
        startIndex sourceCode.IndexOf(startindex) + startindex.Length;
                        
        endIndex sourceCode.IndexOf("\" />"startIndex);
                    
        postValue.Add("loggedinuser=" sourceCode.Substring(startIndexendIndex 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[]);
                } 

        Comment


        • #5
          Ich habe so das Gefühl dass ich gar nicht wirklich helfen möchte... Per Code irgendwelche Posts in einem Forum erstellen hört sich für mich jetzt nicht wirklich wünschenswert an -.-

          Comment


          • #6
            Natürlich lässt sich jeder httprequest missbrauchen aber deshalb kann man doch nicht das ganze Kapitel bannen! In diesem speziellen Fall will ich eigentlich nur eine Chrome extension machen um so eine Art UserCp für diverse Foren auf einmal zu erhalten.

            Comment


            • #7
              http://www.tutorials.de/net-web-und-...mit-login.html

              http://www.c-plusplus.de/forum/282313
              Christian

              Comment


              • #8
                Ja, aber da Antwortet niemand deshalb hab ich mir halt n neues Forum gesucht.

                Comment

                Working...
                X