0

I have a little problem with cookie handling in C#

So on my web site, I have a login page, once logged in, I am redirected to the home page. I get with HttpWebRequest to connect and follow the redirection, I created a class, here it is :

class webReq
{
    private string urlConnection;
    private string login;
    private string password;
    private CookieCollection cookieContainer;
    private long executionTime = 0;



    public webReq(string urlCo, string login, string pass)
    {
        this.urlConnection = urlCo;
        this.login = login;
        this.password = pass;
        this.cookieContainer = null;
    }

    public void StartConnection()
    {
        string WriteHTML = "D:/REM/Connection.html";

        List<string> datas = new List<string>();
        datas.Add("Username=" + this.login);
        datas.Add("Password=" + this.password);
        datas.Add("func=ll.login");
        datas.Add("NextURL=/admin/livelink.exe");
        datas.Add("loginbutton=Sign in");
        string postData = "";
        postData = string.Join("&", datas);
        var buffer = Encoding.ASCII.GetBytes(postData);

        try
        {
            var watch = System.Diagnostics.Stopwatch.StartNew();
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(this.urlConnection);
            request.AllowAutoRedirect = true;
            request.Method = "POST";
            request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1003.1 Safari/535.19";
            request.Accept = "text/html, application/xhtml+xml, */*";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = buffer.Length;
            request.CookieContainer = new CookieContainer();

            Stream stream = request.GetRequestStream();
            stream.Write(buffer, 0, buffer.Length);
            stream.Close();
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            stream = response.GetResponseStream();
            watch.Stop();
            this.executionTime = watch.ElapsedMilliseconds;

            StreamReader reader = new StreamReader(stream);
            System.IO.File.WriteAllText(WriteHTML, reader.ReadToEnd());
            this.cookieContainer = new CookieCollection();
            foreach (Cookie cookie in response.Cookies)
            {
                this.cookieContainer.Add(cookie);
            }
        }
        catch (WebException ex)
        {
            Console.WriteLine(ex.GetBaseException().ToString());
        }
    }
}

I load the home page well, and I manage to get a cookie.

So I developed a function to use my cookie to browse the website :

    public void connectUrl(string url, int numeroTest)
    {
        string WriteHTML = "D:/REM/Page"+numeroTest+".html";

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

        //Add cookie to request.CookieContainer
        request.CookieContainer = new CookieContainer();
        request.CookieContainer.Add(this.cookieContainer);
        var watch = System.Diagnostics.Stopwatch.StartNew();
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        Stream stream = response.GetResponseStream();
        watch.Stop();
        this.executionTime = watch.ElapsedMilliseconds;
        StreamReader reader = new StreamReader(stream);
        System.IO.File.WriteAllText(WriteHTML, reader.ReadToEnd());

    }

Normally, I have to retrieve three cookies, like on the website : Cookie on webSite

Only, I can't navigate on the website, I end up on the login page, the cookies are not good, and that I'm in debug, I only loaded one cookie(BrowseSettings) out of the three(LLCookie & LLTZCookie) :

Debug cookie C#

I don't understand why I can't retrieve all the cookies on the website.... If anyone has a solution!

3
  • I would consider using Selenium for these kinds of requirements. Commented Sep 19, 2018 at 12:25
  • I can't, all this is part of a project for which it has already defined the technologies to be used (for integration etc) Commented Sep 19, 2018 at 12:36
  • thanks xD ! hoping that someone can explain where the problem is ^^ Commented Sep 19, 2018 at 12:39

1 Answer 1

1

I found the reason why I can't get all the cookies, even if I can't find exactly why it works by disabling redirection, in my StartConnection() method :

request.AllowAutoRedirect = true;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.