Solved

Login with c# HttpWebRequest

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Hide Tab Page 3 19
cs.Designer Issue 2 19
Setting runtime form location 4 19
Format column on datatable 7 17
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now