httpclient模拟登陆CSDN

来源:互联网 发布:欢乐逛软件下载 编辑:程序博客网 时间:2024/06/09 19:32

    目录(?)[+]

    1. 工具介绍
    2. 步骤分析
    3. 如何简单快速使用HttpClient
    4. 模拟登陆实战
    5. 题外话

    工具介绍


    本篇文章主要是讲解如何模拟登陆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中得到的请求参数和账号密码模拟发送post请求到登陆请求地址
    3、最后分析post返回的结果判断登陆是否成功

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

    如何简单快速使用HttpClient


    可能你对HttpClient的API不熟悉,那么如何在项目中快速使用HttpClient呢?

    这里已经封装了两个最常用的get和post请求方法,所以之前就让你别担心啦~^_^

    如果不想花时间看API的话直接拿去用就可以了

    [java] view plaincopy
    1. /** 
    2.  * Http工具类 
    3.  *  
    4.  * @author Zhu 
    5.  *  
    6.  */  
    7. public class HttpUtils {  
    8.   
    9.     private static CloseableHttpClient httpClient = HttpClients.createDefault();  
    10.     private static HttpClientContext context = new HttpClientContext();  
    11.   
    12.     private HttpUtils() {  
    13.   
    14.     }  
    15.   
    16.     public static String sendGet(String url) {  
    17.         CloseableHttpResponse response = null;  
    18.         String content = null;  
    19.         try {  
    20.             HttpGet get = new HttpGet(url);  
    21.             response = httpClient.execute(get, context);  
    22.             HttpEntity entity = response.getEntity();  
    23.             content = EntityUtils.toString(entity);  
    24.             EntityUtils.consume(entity);  
    25.             return content;  
    26.         } catch (Exception e) {  
    27.             e.printStackTrace();  
    28.             if (response != null) {  
    29.                 try {  
    30.                     response.close();  
    31.                 } catch (IOException e1) {  
    32.                     e1.printStackTrace();  
    33.                 }  
    34.             }  
    35.         }  
    36.         return content;  
    37.     }  
    38.   
    39.     public static String sendPost(String url, List<NameValuePair> nvps) {  
    40.         CloseableHttpResponse response = null;  
    41.         String content = null;  
    42.         try {  
    43.             // HttpClient中的post请求包装类  
    44.             HttpPost post = new HttpPost(url);  
    45.             // nvps是包装请求参数的list  
    46.             if (nvps != null) {  
    47.                 post.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));  
    48.             }  
    49.             // 执行请求用execute方法,content用来帮我们附带上额外信息  
    50.             response = httpClient.execute(post, context);  
    51.             // 得到相应实体、包括响应头以及相应内容  
    52.             HttpEntity entity = response.getEntity();  
    53.             // 得到response的内容  
    54.             content = EntityUtils.toString(entity);  
    55.             // 关闭输入流  
    56.             EntityUtils.consume(entity);  
    57.             return content;  
    58.         } catch (Exception e) {  
    59.             e.printStackTrace();  
    60.         } finally {  
    61.             if (response != null) {  
    62.                 try {  
    63.                     response.close();  
    64.                 } catch (IOException e) {  
    65.                     e.printStackTrace();  
    66.                 }  
    67.             }  
    68.         }  
    69.         return content;  
    70.     }  
    71. }  

    现在get和post对你来说都已经轻而易举了,那么开始模拟登陆吧~


    模拟登陆实战


    按照我们先前的思路来前进吧!

    1、首先我们需要发送一个get请求来得到登陆页面,并从登陆页面上得到三个请求参数
    [java] view plaincopy
    1. /** 
    2.  * 获取必要的登陆参数信息 
    3.  *  
    4.  * @throws IOException 
    5.  */  
    6. private void fetchNecessaryParam() throws IOException {  
    7.     // 查看CSDN登陆页面源码发现登陆时需要post5个参数  
    8.     // name、password,另外三个在页面的隐藏域中,a good start  
    9.     logger.info("获取必要的登陆信息。。。。。");  
    10.     // 这样登陆不行,因为第一次需要访问需要拿到上下文context  
    11.     // Document doc = Jsoup.connect(LOGIN_URL).get();  
    12.     String html = HttpUtils.sendGet(LOGIN_URL);  
    13.     Document doc = Jsoup.parse(html);  
    14.     Element form = doc.select(".user-pass").get(0);  
    15.     lt = form.select("input[name=lt]").get(0).val();  
    16.     execution = form.select("input[name=execution]").get(0).val();  
    17.     _eventId = form.select("input[name=_eventId]").get(0).val();  
    18.     logger.info("获取成功。。。。。");  
    19. }  

    2、用从1中得到的请求参数和账号密码模拟发送post请求到登陆请求地址
    3、最后分析post返回的结果判断登陆是否成功
    [java] view plaincopy
    1. private boolean mockLogin() {  
    2.     logger.info("开始登陆。。。。。");  
    3.     boolean result = false;  
    4.     List<NameValuePair> nvps = new ArrayList<NameValuePair>();  
    5.     nvps.add(new BasicNameValuePair("username", username));  
    6.     nvps.add(new BasicNameValuePair("password", password));  
    7.     nvps.add(new BasicNameValuePair("lt", lt));  
    8.     nvps.add(new BasicNameValuePair("execution", execution));  
    9.     nvps.add(new BasicNameValuePair("_eventId", _eventId));  
    10.     String ret = HttpUtils.sendPost(LOGIN_URL, nvps);  
    11.     if (ret.indexOf("redirect_back") > -1) {  
    12.         logger.info("登陆成功。。。。。");  
    13.         result = true;  
    14.     } else if (ret.indexOf("登录太频繁") > -1) {  
    15.         logger.info("登录太频繁,请稍后再试。。。。。");  
    16.     } else {  
    17.         logger.info("登陆失败。。。。。");  
    18.     }  
    19.     return result;  
    20. }  

    题外话:


    模拟登陆之后你就可以随心所欲的操作了~可以写个直接发blog的小程序或者是刷访问量之类的~
    不过访问的太频繁可能会被封IP之类的~~~~

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


    0 0
    原创粉丝点击