0

I found out that when I use this function to login and get information from it in coco variable I find a content of the same login page. It's very weird since when I get wrong credentials I go back to the login page with an error of wrong credentials. But when I use good credentials, I get to the login page with blank forms.

Why aren't I redirected to the page after the login?

private int[] LoginCheck(string TargetWebApp)
        {
            int[] result = new int[2];
            var watch = System.Diagnostics.Stopwatch.StartNew();
            try
            {


            string formUrl = "https://XXXX"; 
            string formParams = string.Format("email={0}&password={1}&submit=Login", "XXXXX", "XXXXXX");
            string cookieHeader;
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(formUrl);
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
            req.ContentType = "application/x-www-form-urlencoded";
            req.AllowAutoRedirect = true;
            req.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11";
            req.Method = "POST";
            byte[] bytes = Encoding.ASCII.GetBytes(formParams);
            req.ContentLength = bytes.Length;
            var newStream = req.GetRequestStream();
            newStream.Write(bytes, 0, bytes.Length);
            newStream.Close();


            var resp = req.GetResponse();
            var responseStream = resp.GetResponseStream();
            var responseReader = new StreamReader(responseStream);
            var coco = responseReader.ReadToEnd();

            var elapsedMs = watch.ElapsedMilliseconds;
            watch.Stop();
            Console.WriteLine("Login to XXXXX succeed in {0} Milliseconds", elapsedMs);
            cookieHeader = resp.Headers["Set-cookie"];
            result[0] = 1;
            result[1] = (int)elapsedMs;

        }
        catch (Exception e)
        {
            //Any exception will return false.
            Console.WriteLine(e.Message);
            result[0] = 0;
            result[1] = 0;
        }

        return result;
    }
EilonA
  • 361
  • 5
  • 17
  • Does https://www.codeproject.com/Messages/4823065/Simplest-way.aspx help? – mjwills May 16 '18 at 12:59
  • You asked [the same question](https://stackoverflow.com/questions/50365204/how-to-pull-data-from-web-after-login-using-httpwebrequest#comment87750306_50365204) 5 hours ago. Same considerations as before. – Jimi May 16 '18 at 12:59
  • You guys are right. I will try the guide mjwills wrote and see if it helps and if not I will comment here. – EilonA May 16 '18 at 13:24

0 Answers0