0

I'm trying to login to www.autoscout24.de and retrieve adds and messages. Login form has a random generated hidden input/token. Being new to C#, I've read different tuts about using C# to login to websites and all I found was simple codes that work only in simple login forms (user:pass). I've imagined a 2-step approach: first make a GET request to retrieve needed data and a POST request with login credentials and other needed imputes. Using HtmlAgilityPack I'm passed first step but the second request just returns the login page again instead of "My account" page. My code:

using System;
using System.IO;
using System.Net;
using System.Text;

namespace WebRequest__custom
{
    class Program
    {
        static void Main(string[] args)
        {
            CookieContainer _cookies;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://angebot.autoscout24.de/login?fromNavi=myAS24");
            WebResponse _response = request.GetResponse();
            Stream stream = _response.GetResponseStream();
            StreamReader strReader = new StreamReader(stream);

            string _cookiesHeader = _response.Headers["Set-cookie"];
            _cookies = request.CookieContainer;
            string _content = strReader.ReadToEnd();
            //Console.WriteLine(_content.Substring(0,500));

            var _dom = new HtmlAgilityPack.HtmlDocument();
            _dom.LoadHtml(_content);

            // Get POST link
            var _postLinkNode = _dom.DocumentNode.SelectSingleNode("//*[@id='loginForm']/div[3]/form");
            var postLink = _postLinkNode.Attributes["action"].Value;
            //Console.WriteLine(postLink);

            //get Token
            var _tokenNode = _dom.DocumentNode.SelectSingleNode("//*[@id='loginForm']/div[3]/form/input");
            var token = _tokenNode.Attributes["value"].Value;
            //Console.WriteLine(token);

            // Start login request
            HttpWebRequest requestLogin = (HttpWebRequest)WebRequest.Create("https://accounts.autoscout24.com"+ postLink);

            requestLogin.ContentType = "application/x-www-form-urlencoded";
            requestLogin.Method = "POST";
            requestLogin.KeepAlive = true;
            requestLogin.AllowAutoRedirect = true;

            string postData = "&__RequestVerificationToken=" + token;
            postData += "&Username=web-cppxt@mail-tester.com";
            postData += "&Password=Qwert123!";
            postData += "&RememberMeCheckBox=on&RememberMe=true";

            byte[] _bytes = Encoding.UTF8.GetBytes(postData);
            requestLogin.ContentLength = _bytes.Length;
            requestLogin.CookieContainer = _cookies;
            using(Stream sr = requestLogin.GetRequestStream())
            {
                sr.Write(_bytes, 0, _bytes.Length);
            }

            WebResponse loginResponse = requestLogin.GetResponse();
            StreamReader loginStreamReader = new StreamReader(loginResponse.GetResponseStream());
            string secondPage = loginStreamReader.ReadToEnd();



            Console.WriteLine(secondPage.Substring(0,500));
            Console.ReadKey();

        }
    }
}
  • so what is the question, and what is the actual problem can you set breakpoints and debug your own code to identify what line in your code is causing the problem and or issue..? – MethodMan Dec 19 '17 at 01:16
  • "I''m passed first step but the second request just returns the login page again instead of "My account" page." - Instead of logging me in (redirect to "My account" page so I can scrap data) it returns the login page again. I suspect it's a matter of headers/cookies management, but due to my low experience with C#, I'm stuck here. – user3120048 Dec 19 '17 at 01:21
  • Well from the quick glance, it looks like these guys use oauth2 and they pass the state id in the redirect uri for their form, e.g. when you navigate to `https://angebot.autoscout24.de/login?fromNavi=myAS24` you get redirected to their endpoint with a bunch of stuff in the URI. You don't need to parse anything with HAP, all you need to do is take those parameters and submit in your request. – zaitsman Dec 19 '17 at 01:35
  • I'm sorry but I'm not sure I get it. You're saying that I should get all parameters from the first response uri and parse them in the new request along with email and pass? What about the other 3 form's inputs?... Doesn't that mean that the post request is not complete? – user3120048 Dec 19 '17 at 01:54

0 Answers0