模拟登录CSDN——就是这么简单

工具介绍


本篇文章主要是解说怎样模拟登录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的朋友的学习成本是至关的低的!那么,继续吧骚年!工具


步骤分析


第一步、首先需要拿到模拟登录的请求地址,在CSDN登录页就可以找到:https://passport.csdn.net/account/login,不错,第一步已经成功

第二步、抓包获得post请求需要发送的參数,可以用FF或chrome来抓。例如如下图所看到的:


第三步、当中username和password是由咱们填的,那么后面三个參数呢?不急,看看登录页面的源码

原来在这儿呢!到这里一切都异常的顺利~

整理一下思路,不要被顺利冲昏了头脑~
一、首先咱们需要发送一个get请求来获得登录页面。并从登录页面上获得三个请求參数
二、用从1中获得的请求參数和帐号password模拟发送post请求到登录请求地址
三、最后分析post返回的结果推断登录是否成功

有了思路以后,咱们还需要借助编程来实现它,这里需要一个工具——HttpClient

怎样简单高速使用HttpClient


可能你对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对你来讲都已经垂手可得了。那么開始模拟登录吧~


模拟登录实战


依照咱们先前的思路来前进吧!

一、首先咱们需要发送一个get请求来获得登录页面,并从登录页面上获得三个请求參数
	/**
	 * 获取必要的登录參数信息
	 * 
	 * @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("获取成功。。。

。"); }


二、用从1中获得的请求參数和帐号password模拟发送post请求到登录请求地址
三、最后分析post返回的结果推断登录是否成功
	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; }


题外话:


模拟登录以后你就可以为所欲为的操做了~可以写个直接发blog的小程序或者是刷訪问量之类的~
只是訪问的太频繁可能会被封IP之类的~~~~

模拟登录CSDN仅仅是抛砖引玉。你也可以用此法模拟登录各类平台,百度啊、新浪微博啊等等
CSDN这里仅仅是一个基础的模拟的登录,别的可能还会涉及到SSL等各类技术、有兴趣的朋友可以试试

如有问题,欢迎你们指正~
相关文章
相关标签/搜索