本篇文章主要是解说怎样模拟登录CSDN。使用的工具是HttpClient+Jsoupjavascript
当中HttpClient主要是负责发送请求,而Jsoup主要是解析HTMLhtml
你可能对HttpClient的API不太了解,只是不要紧。往下看就行了~java
Jsoup的语法相似jQuery的选择器。相信有必定web基础的人都可以很是快的掌握web
当中select(String selector)就是最强大的选择器。另外还提供一系列的细化的方法,比方:chrome
getElementById(String id), getElementsByClass(String class), getElementsByTag(String tagName)编程
是否是很是亲切?对~这个就跟javascript的方法相似了~小程序
因此Jsoup对于开发WEB的朋友的学习成本是至关的低的!那么,继续吧骚年!工具
可能你对HttpClient的API不熟悉。那么怎样在项目中高速使用HttpClient呢?post
这里已经封装了两个最常常使用的get和post请求方法,因此以前就让你别操心啦~^_^学习
假设不想花时间看API的话直接拿去用就可以了
/** * Http工具类 * * @author Zhu * */ public class HttpUtils { private static CloseableHttpClient httpClient = HttpClients.createDefault(); private static HttpClientContext context = new HttpClientContext(); private HttpUtils() { } public static String sendGet(String url) { CloseableHttpResponse response = null; String content = null; try { HttpGet get = new HttpGet(url); response = httpClient.execute(get, context); HttpEntity entity = response.getEntity(); content = EntityUtils.toString(entity); EntityUtils.consume(entity); return content; } catch (Exception e) { e.printStackTrace(); if (response != null) { try { response.close(); } catch (IOException e1) { e1.printStackTrace(); } } } return content; } public static String sendPost(String url, List<NameValuePair> nvps) { CloseableHttpResponse response = null; String content = null; try { // HttpClient中的post请求包装类 HttpPost post = new HttpPost(url); // nvps是包装请求參数的list if (nvps != null) { post.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8")); } // 运行请求用execute方法,content用来帮咱们附带上额外信息 response = httpClient.execute(post, context); // 获得对应实体、包含响应头以及对应内容 HttpEntity entity = response.getEntity(); // 获得response的内容 content = EntityUtils.toString(entity); // 关闭输入流 EntityUtils.consume(entity); return content; } catch (Exception e) { e.printStackTrace(); } finally { if (response != null) { try { response.close(); } catch (IOException e) { e.printStackTrace(); } } } return content; } }
现在get和post对你来讲都已经垂手可得了。那么開始模拟登录吧~
/** * 获取必要的登录參数信息 * * @throws IOException */ private void fetchNecessaryParam() throws IOException { // 查看CSDN登录页面源代码发现登录时需要post5个參数 // name、password,另外三个在页面的隐藏域中,a good start logger.info("获取必要的登录信息。。。。。"); // 这样登录不行,因为第一次需要訪问需要拿到上下文context // Document doc = Jsoup.connect(LOGIN_URL).get(); String html = HttpUtils.sendGet(LOGIN_URL); Document doc = Jsoup.parse(html); Element form = doc.select(".user-pass").get(0); lt = form.select("input[name=lt]").get(0).val(); execution = form.select("input[name=execution]").get(0).val(); _eventId = form.select("input[name=_eventId]").get(0).val(); logger.info("获取成功。。。。
。"); }
private boolean mockLogin() { logger.info("開始登录。。。
。。"); boolean result = false; List<NameValuePair> nvps = new ArrayList<NameValuePair>(); nvps.add(new BasicNameValuePair("username", username)); nvps.add(new BasicNameValuePair("password", password)); nvps.add(new BasicNameValuePair("lt", lt)); nvps.add(new BasicNameValuePair("execution", execution)); nvps.add(new BasicNameValuePair("_eventId", _eventId)); String ret = HttpUtils.sendPost(LOGIN_URL, nvps); if (ret.indexOf("redirect_back") > -1) { logger.info("登录成功。。。。
。"); result = true; } else if (ret.indexOf("登陆太频繁") > -1) { logger.info("登陆太频繁。请稍后再试。
。
。
。
。
"); } else { logger.info("登录失败。。。。。"); } return result; }