Solved

Login with c# HttpWebRequest

Posted on 2014-02-17
1
622 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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.

680 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