简体   繁体   中英

how to Login to website programatically using using C#

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}", store@admin.com", "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);
            }
        }

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) eg

    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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM