阅读背景:

使用htmlunit尝试登录网站,面对此类错误。

来源:互联网 

Code

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package qzone;

import java.net.*;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.*;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author wangyi
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Main main = new Main();
        try {
            main.submittingForm();
        } catch (Exception ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void submittingForm() throws Exception {
        final WebClient webClient = new WebClient();
        //webClient.setJavaScriptEnabled(false);
        // Get the first page
        final HtmlPage page1 = webClient.getPage("https://qzone.qq.com/");

        // Get the form that we are dealing with and within that form,
        // find the submit button and the field that we want to change.
        final HtmlForm form = page1.getFormByName("loginform");

        //final HtmlSubmitInput button = form.getInputByName("subbtn");
        final HtmlSubmitInput button = page1.getHtmlElementById("subbtn");
        final HtmlTextInput loginName = form.getInputByName("u");
        final HtmlPasswordInput loginPs = form.getInputByName("p");

        // Change the value of the text field
        loginName.setValueAttribute("525356734");
        loginPs.setValueAttribute("5230911,..");


        // Now submit the form by clicking the button and get back the second page.
        final HtmlPage page2 = button.click();
        final String pageAsXml = page2.asText();
        System.out.println(pageAsXml);
        //webClient.closeAllWindows();
    }

    public void homePage() throws Exception {
    final WebClient webClient = new WebClient();
    final HtmlPage page = webClient.getPage("https://htmlunit.sourceforge.net");
    //massertEquals("HtmlUnit - Welcome to HtmlUnit", page.getTitleText());

    final String pageAsXml = page.asXml();
    //assertTrue(pageAsXml.contains("<body class=\"composite\">"));

    final String pageAsText = page.asText();
    //assertTrue(pageAsText.contains("Support for the HTTP and HTTPS protocols"));
    //System.out.println(pageAsText);
    webClient.closeAllWindows();
}
}
/*
 * To change this template, choose To



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: