Solved

Login with c# HttpWebRequest

Posted on 2014-02-17
1
604 Views
Last Modified: 2014-03-03
I would like to automatically log into a site 'petwholesaler.com' and remain in the site using the HttpWebRequest browser class. I tried the following:


  CookieContainer _siteContainer;
             string _login = "myUserName";
             string _password = "myPassword";
           

             string strPostData = String.Format("username={0}&password={1}", _login, _password);

             // Setup the http request.
             HttpWebRequest wrWebRequest = WebRequest.Create("http://www.petwholesaler.com/login.php?ref=express_order_form") as HttpWebRequest;
             wrWebRequest.Method = "POST";
             wrWebRequest.ContentLength = strPostData.Length;
             wrWebRequest.ContentType = "application/x-www-form-urlencoded";
             _siteContainer = new CookieContainer();
             wrWebRequest.CookieContainer = _yahooContainer;

             // Post to the login form.
             using (StreamWriter swRequestWriter = new StreamWriter(wrWebRequest.GetRequestStream()))
             {
                 swRequestWriter.Write(strPostData);
                 swRequestWriter.Close();
             }

             // Get the response.
             HttpWebResponse response = (HttpWebResponse)wrWebRequest.GetResponse();
              StreamReader myStreamReader = new StreamReader(response.GetResponseStream());
             
             string responseString = myStreamReader.ReadToEnd();
             StreamWriter swrite = new StreamWriter(@"PetWholesaler.html");
              swrite.Write(responseString);
              swrite.Close();

Open in new window


It doesn't work. Does anyone know what's the exact code I need to login?
0
Comment
Question by:esak2000
1 Comment
 
LVL 7

Accepted Solution

by:
Kishan Zunjare earned 500 total points
ID: 39866666
Have a look into bellow code;

HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("http://petwholesaler.com//"); 
            req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705;)"; 
            req.Method = "POST";
            req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
            req.Headers.Add("Accept-Language: en-us,en;q=0.5");
            req.Headers.Add("Accept-Encoding: gzip,deflate");
            req.Headers.Add("Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7");
            req.KeepAlive = true;
            req.Headers.Add("Keep-Alive: 300");
            req.Referer ="http://petwholesaler.com/login";

            req.ContentType = "application/x-www-form-urlencoded"; 

            String Username = "username";
            String PassWord = "Password";

            StreamWriter sw = new StreamWriter(req.GetRequestStream());
            sw.Write("application=portal&url=http%3A%2F%2Fwww.petwholesaler.com.%2Fportal%2Fredirect%3Bjsessionid%3D1C568AAA1FB8B5C757CF5F68BE6ECE65%3Ftype%3Dssologin%26url%3D%2Fportal%2Fshow%3Bjsessionid%3D1C568AAA1FB8B5C757CF5F68BE6ECE65%3Fidc%3D1023278&realm=sso&userid=" + Username + "&password=" + password + "&x=16&y=11");
            sw.Close();

            HttpWebResponse response = (HttpWebResponse)req.GetResponse();


            StreamReader reader = new StreamReader(response.GetResponseStream());
            string tmp = reader.ReadToEnd();

            foreach (Cookie cook in response.Cookies)
            {
                tmp += "\n" + cook.Name + ": " + cook.Value;
            }


            Response.Write(tmp);
            Response.End();

Open in new window



Hope this helps
- Kishan
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question