Beispiel #1
0
        private static void PerformLoginOnPocket(WebRequests client, string username, string password)
        {
            // Explanation of the requests below:
            // In order to perform the LOGIN on Pocket website, we have to issue a POST request with a few parameters in it's BODY.
            // Aside from the obvious "Login" and "Password", there's also a variable parameter called "Form_Check".
            // This "form_check" parameter can be found on the login page, hidden in it's HTML, so what we are going to do is "Extract" this information
            // from there, and use it on the login request to automate the whole login flow.

            // First thing we have to do is to define the actual "Target" for our demo, in this case, it will be "www.imdb.com"
            // To start Simple, let's try to get the HTML of the Home Page of the site. Since we are RETRIEVING a resource (the home page), the request method needs to be a GET
            // But first, let's setup the headers for this request
            client.ClearCookies();
            client.Host      = "getpocket.com";
            client.UserAgent = "Web Crawling 101 Book - Used for educational purposes only";
            client.Referer   = "https://getpocket.com/login?e=4";
            client.Origin    = "https://getpocket.com";
            client.Timeout   = 18000; // 18 Seconds

            // Reaching Home Page for the "Form Check" parameter
            Console.WriteLine(" => Executing GET Request for Home Page");
            string homePageHTML = client.Get("https://getpocket.com/login?e=4");

            // Parsing "Form Check" parameter
            string formCheck = ExtractFormCheckParameter(homePageHTML);

            Console.WriteLine(" => Extracted FormCheck parameter hidden within the HTML '{0}'", formCheck);

            // Formatting the HTTP POST BODY of the LOGIN Request
            string postData = String.Format("feed_id={0}&password={1}&route=&form_check={2}&src=&source=email&source_page=%2Flogin&is_ajax=1",
                                            username, password, formCheck);

            // HTTP Post URl for "Login" on Pocket
            Console.WriteLine(" => Performing Login");
            string pocketLoginUrl = "https://getpocket.com/login_process.php";
            string loginResponse  = client.Post(pocketLoginUrl, postData);

            Console.WriteLine(" => Login Status Code : {0}", client.StatusCode);
            Console.WriteLine(" => Login Response Text : {0}", loginResponse);
        }