HttpClient实现CSDN自动登录
生活随笔
收集整理的這篇文章主要介紹了
HttpClient实现CSDN自动登录
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
參考網上代碼,通過httpclient實現CSDN自動登錄,問題是對于網頁表單JS動態加載的還沒找到辦法自動填寫和提交。
1、HttpUtils類封裝httpclient的get和post方法
package gddx;import java.io.IOException; import java.util.List;import org.apache.http.HttpEntity; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.protocol.HttpClientContext; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import org.apache.http.NameValuePair;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); //輸出htmlSystem.out.println(content);// 關閉輸入流 EntityUtils.consume(entity); return content; } catch (Exception e) { e.printStackTrace(); } finally { if (response != null) { try { response.close(); } catch (IOException e) { e.printStackTrace(); } } } return content; } }2、CSDNLogin實現自動登錄
package gddx;import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.apache.http.NameValuePair; import org.apache.http.message.BasicNameValuePair;/*** @author Jason.F* Date 2016年5月19日* **/ public class CSDNLogin {private String loginURL = "https://passport.csdn.net/account/login";private String lt=null;private String execution=null;private String _eventId=null;/** * 獲取必要的登陸參數信息 * * @throws IOException */ private void fetchNecessaryParam(){ // 查看CSDN登陸頁面源碼發現登陸時需要post5個參數 // name、password,另外三個在頁面的隱藏域中,a good start System.out.println("獲取登錄表單參數。。。。。"); // 這樣登陸不行,因為第一次需要訪問需要拿到上下文context // Document doc = Jsoup.connect(LOGIN_URL).get(); String html = HttpUtils.sendGet(loginURL); 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(); System.out.println("獲取成功。。。。。"); } private void login() { fetchNecessaryParam();System.out.println("開始登陸。。。。。"); List<NameValuePair> nvps = new ArrayList<NameValuePair>(); nvps.add(new BasicNameValuePair("username", "fjssharpsword")); nvps.add(new BasicNameValuePair("password", "xyz")); nvps.add(new BasicNameValuePair("lt", lt)); nvps.add(new BasicNameValuePair("execution", execution)); nvps.add(new BasicNameValuePair("_eventId", _eventId)); String ret = HttpUtils.sendPost(loginURL, nvps); if (ret.indexOf("redirect_back") > -1) { System.out.println("登陸成功。。。。。"); } else if (ret.indexOf("登錄太頻繁") > -1) { System.out.println("登錄太頻繁,請稍后再試。。。。。"); } else { System.out.println("登陸失敗。。。。。"); } } public static void main(String[] args) {CSDNLogin lession = new CSDNLogin();lession.login();} }《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀
總結
以上是生活随笔為你收集整理的HttpClient实现CSDN自动登录的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 算法导论之概率分析和随机算法
- 下一篇: Jsoup实现Iteye自动登录