Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i try login to website using HttpWepRequste and i sucuss login
the problem when i try go on other url then ask me to log in again
and thats my code where is the problem ???
and thats my code

What I have tried:

C#
CookieContainer cook = new CookieContainer();
            string data = "act=login&accesskey=&si_user=a7mad-bas%40hotmail.com&si_pass=1236987412&si_stay=1&x=14&y=32";
            Encoding enc = new UTF8Encoding();
            byte[] postdata = enc.GetBytes(data);

            HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://[site].com/en/account/");
            
            req.Method = "GET";
            req.KeepAlive = true;
            req.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:43.0) Gecko/20100101 Firefox/43.0";
            req.ContentType = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
             req.CookieContainer=cook ;
            logincook = cook;

            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            foreach (Cookie c in res.Cookies)
            {
                cook.Add(c);
            }
            logincook = cook;
            

            StreamReader sr = new StreamReader(res.GetResponseStream());
            string x = sr.ReadToEnd();
            richTextBox1.Text = x;
            //res.Close();

            //post Method 

            req = WebRequest.Create("http://[site].com/en/account/") as HttpWebRequest;
            
            req.Method = "POST";
            req.KeepAlive = true;
            req.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:43.0) Gecko/20100101 Firefox/43.0";
            req.ContentType = "application/x-www-form-urlencoded";
            req.ContentLength = postdata.Length;
            cook= req.CookieContainer  ;
           
            logincook = cook;
            Stream wr = req.GetRequestStream();
            wr.Write(postdata, 0, postdata.Length);

            res = req.GetResponse() as HttpWebResponse;
            foreach (Cookie c in res.Cookies)
            {
                cook.Add(res.Cookies);
            }
            logincook = cook;

            sr = new StreamReader( res.GetResponseStream());
            x = sr.ReadToEnd();
            richTextBox2.Text = x;

            //MessageBox.Show(logincook.Count.ToString());
            req = WebRequest.Create("http://[site].com/en/account/") as HttpWebRequest;
            
            req.Method = "GET";
            req.KeepAlive = true;
            req.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:43.0) Gecko/20100101 Firefox/43.0";
            req.ContentType = "application/x-www-form-urlencoded";
           // req.ContentLength = postdata.Length;
            req.CookieContainer = cook;
            logincook = cook;
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900