Simulate CSDN login-this is so simple as simulating csdn
Tool Introduction
This article mainly explains how to simulate CSDN login, using HttpClient + Jsoup
HttpClient is mainly responsible for sending requests, while Jsoup is mainly used to parse HTML
You may not know much about HttpClient APIs, but it doesn't matter. Just look down ~
The Jsoup syntax is similar to the jQuery selector. I believe that people with certain web basics can quickly master it.
Select (String selector) is the most powerful selector. It also provides a series of refined methods, such:
GetElementById (String id), getElementsByClass (String class), getElementsByTag (String tagName)
Is it very kind? Yes ~ This is similar to the javascript method ~
Therefore, Jsoup has a very low learning cost for WEB developers! So, continue to cool!
Step Analysis
Step 1,First, you need to obtain the request address for simulated login. On the CSDN login page, you can find: https://passport.csdn.net/account/login. the first step is successful.
Step 2,Capture packets to get the parameters to be sent for the post request. You can use FF or chrome to capture the parameters, as shown in:
Step 3,Here, username and password are filled by us. What about the next three parameters? Don't worry. Check the source code on the login page.
It was here! Everything went exceptionally well here ~
Sort out your ideas and don't be overwhelmed ~ 1. First, we need to send a get request to get the login page, three request parameters are obtained from the login page. 2. Request Parameters and account passwords obtained from 1 are used to simulate the sending of post requests to the login request address. 3. analyze the results returned by post to determine the login successful or not
After thinking about it, we also need to implement it through programming. Here we need a tool -- HttpClient
How to Use HttpClient easily and quickly
Maybe you are not familiar with HttpClient APIs. How can you quickly use HttpClient in projects?
The two most common get and post request methods have been encapsulated here, so don't worry about it before ~ Pai_^
If you don't want to spend time watching the API, you can simply use it.
/*** Http tool class ** @ 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.exe cute (get, conte Xt); 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 {// the post request packaging class in HttpClient HttpPost = new HttpPost (url ); // nvps is the listif (nvps! = Null) {post. setEntity (new UrlEncodedFormEntity (nvps, "UTF-8");} // execute the request using the execute method, content comes to help us with additional information response = httpClient.exe cute (post, context ); // obtain the corresponding entity, including the response header and corresponding content. HttpEntity entity = response. getEntity (); // get the content of response content = EntityUtils. toString (entity); // close the input stream EntityUtils. consume (entity); return content;} catch (Exception e) {e. printStackTrace ();} finally {if (response! = Null) {try {response. close () ;}catch (IOException e) {e. printStackTrace () ;}} return content ;}}
Now get and post are easy for you, so start simulating login ~
Simulated login practice
Follow our previous ideas!
1. First, we need to send a get request to get the login page and get three request parameters from the login page.
/*** Get necessary login parameter information ** @ throws IOException */private void fetchNecessaryParam () throws IOException {// view the source code of the CSDN login page. Five postfive parameters // name and password are required for logon, and the other three are in the hidden domain of the page, a good startlogger.info ("Get necessary login information ..... "); // This login will not work, because the first access needs to get the 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 ("obtained successfully ..... ");}
2. Use the Request Parameters and account passwords obtained from 1 to send a post request to the login request address. 3. analyze the results returned by post to determine whether the login is successful.
Private boolean mockLogin () {logger.info ("start logging on ..... "); 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 ("Login successful ..... "); Result = true;} else if (ret. indexOf ("too frequent Logon")>-1) {logger.info ("too frequent logon. Please try again later ..... ");} Else {logger.info (" Login Failed ..... ");} Return result ;}
Digress:
After simulated login, you can operate as you like ~ You can write a small program that sends a blog directly, or click farming traffic ~ However, if the access is too frequent, it may be blocked by IP addresses or the like ~~~~
Simulated login to CSDN is just a reference. You can also use this method to simulate login to various platforms. Baidu, Sina Weibo, and so on. CSDN is just a basic simulated login, other technologies such as SSL may be involved. If you are interested, try it.
If you have any questions, please let us know ~
How to log on to CSDN
You need to register an account first, and then log on to that account, and you will have points when you go in ,...
How to download csdn? You have logged on
After n times of speaking, view Chinese characters on the page to go To the download page.