模擬登陸CSDN——就是這么簡單


工具介紹


本篇文章主要是解說怎樣模擬登陸CSDN。使用的工具是HttpClient+Jsoup

當中HttpClient主要是負責發送請求,而Jsoup主要是解析HTML

你可能對HttpClient的API不太了解,只是沒關系。往下看就好了~

Jsoup的語法類似jQuery的選擇器。相信有一定web基礎的人都能夠非常快的掌握

當中select(String selector)就是最強大的選擇器。另外還提供一系列的細化的方法,比方:

getElementById(String id), getElementsByClass(String class), getElementsByTag(String tagName)

是不是非常親切?對~這個就跟javascript的方法類似了~

所以Jsoup對於開發WEB的朋友的學習成本是相當的低的!那么,繼續吧騷年!


步驟分析


第一步、首先須要拿到模擬登陸的請求地址,在CSDN登陸頁就能夠找到:https://passport.csdn.net/account/login,不錯,第一步已經成功

第二步、抓包得到post請求須要發送的參數,能夠用FF或chrome來抓。例如以下圖所看到的:


第三步、當中username和password是由我們填的,那么后面三個參數呢?不急,看看登陸頁面的源碼

原來在這兒呢!到這里一切都異常的順利~

整理一下思路,不要被順利沖昏了頭腦~
1、首先我們須要發送一個get請求來得到登陸頁面。並從登陸頁面上得到三個請求參數
2、用從1中得到的請求參數和賬號password模擬發送post請求到登陸請求地址
3、最后分析post返回的結果推斷登陸是否成功

有了思路之后,我們還須要借助編程來實現它,這里須要一個工具——HttpClient

怎樣簡單高速使用HttpClient


可能你對HttpClient的API不熟悉。那么怎樣在項目中高速使用HttpClient呢?

這里已經封裝了兩個最經常使用的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對你來說都已經輕而易舉了。那么開始模擬登陸吧~


模擬登陸實戰


依照我們先前的思路來前進吧!

1、首先我們須要發送一個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("獲取成功。。。

。"); }


2、用從1中得到的請求參數和賬號password模擬發送post請求到登陸請求地址
3、最后分析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等各種技術、有興趣的朋友能夠試試

若有問題,歡迎大家指正~


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM