Friday, 14 October 2016

three login | htmlunit login example | form submit html | login example in java | three way login

three login | htmlunit login example | form submit html | login example in java | three way login


here, two way to login on form using HtmlUnit.
using this method you can setup by your custom website login.
you have to just modify your attribute by custom parameter.

Requrie library :- HtmlUnit

 Html Code :-

<html>
<head>
<title>Login Demo</title>
</head>
<body>
<table width="25%">
<form id="contacts-form" method="post" action="/jsp/checkLogin.action;jsessionid=934179CEDE3B37016895C94ADF0D953D" onsubmit="return validateForm();" name="loginform">

<fieldset class="checkbox-set" width="25%" >
<legend>Login Demo</legend>
<fieldset>
<label for="au_Login">Email Id&nbsp&nbsp</label>
<input id="username" class="txtbox" type="text" onblur="if (this.value == '') this.value = 'email address'" onfocus="if (this.value == 'email address') this.value = ''" value="" name="username">
<br>
<label for="au_Login">Password</label>
<input id="password" class="txtbox" type="password" onblur="if (this.value == '') this.value = 'password'" onfocus="if (this.value == 'password') this.value = ''" value="" name="password">
</fieldset>
<fieldset>

<input id="login" class="loginbtn" type="submit" value="Login" name="login">
<input id="reset" class="cancelbtn" type="reset" value="Cancel" name="reset">
</fieldset>
</label>
<br>
</fieldset>
</form>
</table>
</body>
</html>


Example :-

import com.gargoylesoftware.htmlunit.HttpMethod;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.WebRequest;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlInput;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.util.NameValuePair;
import java.net.URL;
import java.util.ArrayList;

/**
 *
 * @author vishal.khokhar
 */
public class LoginDemo {

    public static void main(String[] args) {
        new LoginDemo().doLoginMethodOne();
        new LoginDemo().doLoginMethodSecond();
        new LoginDemo().doLoginMethodThird();
    }

    private void doLoginMethodOne() {
        try {
            String username = "your username";
            String password = "your password";

            WebClient webClient = new WebClient();
            webClient.getOptions().setThrowExceptionOnScriptError(false);
            webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
            webClient.getOptions().setJavaScriptEnabled(false);

            //Setup your site url
            HtmlPage page = (HtmlPage) webClient.getPage("http://scrapemania.blogspot.in/login");
            HtmlForm form = page.getFormByName("loginform");
            form.getInputByName("username").setValueAttribute(username);
            form.getInputByName("password").setValueAttribute(password);

            HtmlPage homepage = form.getInputByValue("login").click();
            String Stringpage = homepage.getWebResponse().getContentAsString();

            System.out.println(Stringpage);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private void doLoginMethodSecond() {
        try {
            String userName = "your username";
            String userPassword = "your password";

            WebClient webClient = new WebClient();
            webClient.getOptions().setThrowExceptionOnScriptError(false);
            webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
            webClient.getOptions().setJavaScriptEnabled(false);

            //Setup your site url
            WebRequest webRequest = new WebRequest(new URL("http://scrapemania.blogspot.in/login"), HttpMethod.POST);

            webRequest.setRequestParameters(new ArrayList<NameValuePair>());
            webRequest.getRequestParameters().add(new NameValuePair("username", userName));
            webRequest.getRequestParameters().add(new NameValuePair("password", userPassword));
            webRequest.getRequestParameters().add(new NameValuePair("jsessionid", "934179CEDE3B37016895C94ADF0D953D"));
            webRequest.getRequestParameters().add(new NameValuePair("submit", "loginform"));

            HtmlPage homepage = webClient.getPage(webRequest);
            String Stringpage = homepage.getWebResponse().getContentAsString();

            System.out.println(Stringpage);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private void doLoginMethodThird() {
        try {
            String userName = "your username";
            String userPassword = "your password";

            WebClient webClient = new WebClient();
            webClient.getOptions().setThrowExceptionOnScriptError(false);
            webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
            webClient.getOptions().setJavaScriptEnabled(false);

            //Setup your site url
            HtmlPage page = (HtmlPage) webClient.getPage("http://scrapemania.blogspot.in/login");
            HtmlForm form = page.getFormByName("loginform");
            form.getInputByName("username").setValueAttribute(userName);
            HtmlInput passWordInput = form.getInputByName("password");
            passWordInput.removeAttribute("disabled");
            passWordInput.setValueAttribute("userPassword");

            page = form.getInputByValue("login").click();
            String Stringpage = page.getWebResponse().getContentAsString();
            System.out.println(Stringpage);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

No comments:

Post a Comment