?
Solved

Login with c# HttpWebRequest

Posted on 2014-02-17
1
Medium Priority
?
727 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 2000 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

850 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