0
我有以下代碼到我的網站,我想登錄到如下所示。我運行它時沒有錯誤消息,但它在最後返回的HTML是原始登錄頁面,這表明我沒有登錄。我嘗試過使用多個網站,同樣的事情一直在發生,所以我不知道是否在我想要的時候記錄下我的內容,或者當我轉到其他頁面時是否記得有關Cookie的內容。Java HTMLUnit登錄不起作用?
任何幫助將不勝感激。
import java.util.logging.Level;
import org.jsoup.*;
import org.jsoup.nodes.Document;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlOption;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSelect;
public class testing {
public static void main(String[] args) throws Exception {
java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF);
final WebClient webClient = new WebClient();
final HtmlPage page = webClient.getPage("www.websiteneededtologinto.com");
HtmlElement usrname = page.getElementByName("Email");
usrname.click();
usrname.type("MYUSERNAME");
HtmlElement psswrd = page.getElementByName("Passwd");
psswrd.click();
psswrd.type("MYPASSWORD");
HtmlElement signin = page.getElementByName("signIn");
signin.click();
String html = Jsoup.connect("pagerequiringlogintoaccess.com").get().html();
System.out.println(html);
}
}