Ali Imran

Ali Imran

  • 1.4k
  • 383
  • 415.7k

Logging in website using c#

Dec 3 2016 2:38 AM

I have been trying to scrap the confidential data from http://shop2.gzanders.com/ . I have tried my best and consulted many similar questions and solutions but wasn't able to achieve the required goal. I used Fiddler to investigate the headers that should be sent with the HttpWebRequest Class and also worked with saving the cookies but wasn't able to achieve that. Attached is the code below. Also, here is the snippet of fiddler I am using to investigate the Login attempt into this website.Can anybody help me what I am doing wrong ? Also there is a form key associated with this form. How can i use that in my Request by code ? . 

 
  1. CookieCollection cookies = new CookieCollection();  
  2.            HttpWebRequest getRequest = (HttpWebRequest)WebRequest.Create("https://shop2.gzanders.com");  
  3.            getRequest.CookieContainer = new CookieContainer();  
  4.            getRequest.CookieContainer.Add(cookies);  
  5.            HttpWebResponse response = (HttpWebResponse)getRequest.GetResponse();  
  6.            cookies = response.Cookies;  
  7.            string getUrl = "https://shop2.gzanders.com/customer/account/login/referer/aHR0cDovL3Nob3AyLmd6YW5kZXJzLmNvbS8,/";  
  8.            string postData = String.Format("email={0}&pass={1}""myUsername""myPassword");  
  9.             getRequest = (HttpWebRequest)WebRequest.Create(getUrl);  
  10.            getRequest.CookieContainer = new CookieContainer();  
  11.            getRequest.CookieContainer.Add(cookies); //recover cookies First request  
  12.            getRequest.Method = WebRequestMethods.Http.Post;  
  13.            getRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko";  
  14.            getRequest.ProtocolVersion = HttpVersion.Version11;  
  15.            getRequest.AllowAutoRedirect = true;  
  16.            getRequest.ContentType = "application/x-www-form-urlencoded";  
  17.            getRequest.Accept = "text/html, application/xhtml+xml, image/jxr, */*";  
  18.            getRequest.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate");  
  19.            getRequest.Headers.Add(HttpRequestHeader.AcceptLanguage, "en-GB,en-US;q=0.7,en;q=0.3");  
  20.            getRequest.KeepAlive = true;  
  21.            getRequest.Host = "shop2.gzanders.com";  
  22.            byte[] byteArray = Encoding.ASCII.GetBytes(postData);  
  23.            getRequest.ContentLength = byteArray.Length;  
  24.            Stream newStream = getRequest.GetRequestStream();  
  25.            newStream.Write(byteArray, 0, byteArray.Length);  
  26.            HttpWebResponse getResponse = (HttpWebResponse)getRequest.GetResponse();  
  27.            if (response.StatusCode != HttpStatusCode.OK)  
  28.            {  
  29.                Console.WriteLine("Something Wrong");  
  30.            }  
  31.            Console.WriteLine(cookies);  
  32.            getResponse.Close();  
  33.            getRequest =   
  34.   
  35.    (HttpWebRequest)WebRequest.Create("http://shop2.gzanders.com/cleaning-kits.html");  
  36.                getRequest.CookieContainer = new CookieContainer();  
  37.                getRequest.CookieContainer.Add(cookies);  
  38.                getRequest.AllowAutoRedirect = false;  
  39.                getResponse = (HttpWebResponse)getRequest.GetResponse();  
  40.                string line = "";  
  41.                using (Stream s = response.GetResponseStream())  
  42.                {  
  43.                    StreamReader sr = new StreamReader(s);  
  44.   
  45.                    line = sr.ReadToEnd();  
  46.                }  
  47.                HtmlDocument doc = new HtmlDocument();  
  48.                doc.LoadHtml(line);  
  49.                //HtmlDocument doc = new HtmlWeb().Load(line);  
  50.                var quantity = doc.DocumentNode.SelectNodes("//div[@class='product-itemnumber']").FirstOrDefault();  
  51.        Console.Writeline(quantity.InnerText);  

Answers (1)