0

I want to login to this website using C#: Here is my attempt but its sending me to first page. Not returning me the next page, that should be visible after login, please help me to resolve this:

string formParams = 
string.Format("mail={0}&password={1}", [email protected]", "admin");
      string cookieHeader;
      WebRequest req = WebRequest.Create("http://muslimgowns.com/dashboard/login/public_login");
            req.ContentType = "application/x-www-form-urlencoded";
            req.Method = "POST";
            byte[] bytes = Encoding.ASCII.GetBytes(formParams);
            req.ContentLength = bytes.Length;
            using (Stream os = req.GetRequestStream())
            {
                os.Write(bytes, 0, bytes.Length);
            }
            WebResponse resp = req.GetResponse();
            cookieHeader = resp.Headers["Set-cookie"];
            using (StreamReader sr = new  StreamReader(resp.GetResponseStream()))
            {
                string pageSource = sr.ReadToEnd();
                File.AppendAllText("first.txt", pageSource);
            }

            string pageSource1;
            string getUrl = "http://muslimgowns.com/dashboard/home";
            WebRequest getRequest = WebRequest.Create(getUrl);
            getRequest.Headers.Add("Cookie", cookieHeader);
            WebResponse getResponse = getRequest.GetResponse();
            using (StreamReader sr = new StreamReader(getResponse.GetResponseStream()))
            {
                pageSource1 = sr.ReadToEnd();
                File.AppendAllText("second.txt", pageSource1);
            }
        }
3
  • Please be a bit more specific when asking question. What have you tried ? Take a look at stackoverflow.com/help/how-to-ask Commented Dec 28, 2015 at 12:19
  • the generic way to do that (the most general lets say) is to make a programmatic http request with the login data and keep state in your script (cookies etc) so session is maintained and script is like a logged-in user. however this depends on what you want to do and how the site is accessed Commented Dec 28, 2015 at 12:21
  • Okay, here is my code but its sending me to first page. Not returning me the next page, that should be visible after login: Commented Dec 28, 2015 at 15:56

1 Answer 1

4

You can use Selenium WebDriver to automate the login process or any other process for that matter. http://www.seleniumhq.org/

The basic idea is to: 1. Include Selenium Webdriver in your C# project 2. Goto to www.fastundercar.com

driver.Url = "http://www.fastundercar.com";
  1. Find the Username, Password fields and the submit button (by Id, name or class) e.g.

    IWebElement username = driver.FindElement(By.Name("ULogin$txtUserName"));

  2. Set values for the username and password fields

  3. Submit the button -

    driver.findElement(By.id("submit")).click();

Check out the below link for reference: http://www.seleniumhq.org/docs/03_webdriver.jsp

Sign up to request clarification or add additional context in comments.

1 Comment

Firefox has a nice Add-on FireBug along with an Add-on-add-on FirePath. This gives you an easy way to find ids/xpaths of page objects.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.