1.模拟登录csdn,最开始的时候使用的是httpclient,网上的所有模拟登录csdn的版本都是找到lt/execution/event_id.连同用户名及密码
一起发送即可,但是目前的csdn的登录参数可不止这三个,fkid怎么解决?我抓包时发现fkid是登录时其value才被赋值,奈何找了半天找不到调用的函数,猜测是由js动态生成的,于是转而使用
htmlunit
1 public static void main(String[] args) throws Exception { 2 ????????WebClient client = new WebClient(BrowserVersion.CHROME); 3 ?????????4 ????????//允许js 5 ????????client.getOptions().setJavaScriptEnabled(true); 6 ?????????7 ????????//禁用css 8 ????????client.getOptions().setCssEnabled(false); 9 ????????10 ????????//超时11 ????????client.getOptions().setTimeout(5000);12 ????????13 ????????//js执行超时14 ????????client.setJavaScriptTimeout(10000*3);15 ????????16 ????????//允许重定向17 ????????client.getOptions().setRedirectEnabled(true);18 ????????19 ????????20 ????????//必须设置js异常抛出禁止21 ????????client.getOptions().setThrowExceptionOnScriptError(false);22 ????????23 ????????//设置忽略证书24 ????????client.getOptions().setUseInsecureSSL(false);25 ????????26 ????????//设置ajax27 ????????client.setAjaxController(new NicelyResynchronizingAjaxController());28 ????????29 ????????//设置cookie30 ????????client.getCookieManager().setCookiesEnabled(true);31 ????????32 ????????//打开网址33 ????????HtmlPage page = client.getPage("https://passport.csdn.net/account/login");34 ????????35 ????????//等待js加载完全36 ????????client.waitForBackgroundJavaScriptStartingBefore(20000);37 ????????38 ????????HtmlForm form = (HtmlForm) page.getElementById("fm1");39 ????????HtmlTextInput username = ?form.getInputByName("username");40 ????????HtmlPasswordInput password = ?form.getInputByName("password");41 ????????username.setValueAttribute("18361078119");42 ????????password.setValueAttribute("01046818wyc");43 ????????44 ????????//定位登录按钮(xpath)45 ????????HtmlButtonInput button ?= (HtmlButtonInput) page.getByXPath("//input[(@class=‘logging‘)]").get(0);46 ?????????47 ????????HtmlPage retPage = button.click();48 ???????// 等待JS驱动dom完成获得还原后的网页 ?49 ???????client.waitForBackgroundJavaScript(1000); ?50 ???????//输出url51 ???????System.out.println(retPage.getUrl().toString()); ??52 ???????//输出网页的内容53 ???????System.out.println(retPage.asXml());54 55 ????????client.close();56 ????????57 ????}
htmlunit ?模拟登录 无验证码
原文地址:https://www.cnblogs.com/tele-share/p/9445419.html