Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Login with c# HttpWebRequest

Posted on 2014-02-17
1
Medium Priority
?
711 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

618 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