java模拟登陆

java爬虫


模拟登陆CSDN

  • 抓包得到post请求的5个参数:

username:139******[email protected]
password:*******()
lt:LT-243423-zaxybu6QLbPZfJSXCBOfBkstaiCKml
execution:e3s1
_eventId:submit

Cookie:uuid_tt_dd=5955540397292079753_20170815; UM_distinctid=15e" +
"502c8a4d5b5-0ebe367e3c65d5-e313761-e1000-15e502c8a4eaa4; __utma=17226283.1282527090.15082" +
"48516.1508248516.1508248516.1; __utmz=17226283.1508248516.1.1.utmcsr=(direct)|utmccn=(direct)" +
"|utmcmd=(none); __message_sys_msg_id=0; __message_gu_msg_id=0; __message_cnel_msg_id=0; __message_district_code=420000; __message_in_school=0; UN=qq_37976565; UE="[email protected]"; BT=1508662667082; JSESSIONID=C2ED5A9B0A01ADD063B0219B5A4953F1.tomcat2; LSSC=LSSC-513836-NbJPY2BrWFZud9WPdsjOlzU2RyQrOe-passport.csdn.net; dc_tos=oy82pe; dc_session_id=1508499845140_0.037202" +
"744870221016; Hm_lvt_6bcd52f51e9b3dce32bec4a3997715ac=1508593384,1508659648,1508659652,1508671911" +
";"+
Hm_lpvt_6bcd52f51e9b3dce32bec4a3997715ac=1508671925"

步骤
  • 1.首先发送一个get请求得到登陆页面及后3个请求参数
  • 2.发送post请求登陆到请求地址
  • 3.分析post得到的结果判断是否登陆成功

用到的API

  • HttpClient
  • Jsoup
sendGet函数
    public static String sendGet(String url){
        HttpResponse response;
        String content=null;
        try {
            HttpGet get=new HttpGet(url);
            response=httpClient.execute(get);
            HttpEntity entity=response.getEntity();
            content= EntityUtils.toString(entity);//用content辅助分析
            EntityUtils.consume(entity);
            return content;
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return content;
    }
sendPost函数
    public static String sendPost(String url, List nvps){
        HttpResponse response;
        String content=null;      //为了结果分析
        try {
//HttpClient中的Post请求包装类
            HttpPost post=new HttpPost(url);

//nvps是包装请求参数的list
            if (nvps!=null){
                post.setEntity(new UrlEncodedFormEntity(nvps,"UTF-8"));

            }
//执行请求用execution方法
            response=httpClient.execute(post);
            HttpEntity entity=response.getEntity();
            content=EntityUtils.toString(entity);
            EntityUtils.consume(entity);
            return  content;
        }catch (Exception e){
            e.printStackTrace();
        }
        return content;
    }

setPost()函数

 HttpResponse response;
        String content=null;
        try {
            HttpPost post=new HttpPost(url);
            if (nvps!=null){
                post.setEntity(new UrlEncodedFormEntity(nvps,"UTF-8"));
                post.setHeader("Cookie","uuid_tt_dd=5955540397292079753_20170815; UM_distinctid=15e" +
                        "502c8a4d5b5-0ebe367e3c65d5-e313761-e1000-15e502c8a4eaa4; __utma=17226283.1282527090.15082" +
                        "48516.1508248516.1508248516.1; __utmz=17226283.1508248516.1.1.utmcsr=(direct)|utmccn=(direct)" +
                        "|utmcmd=(none); __message_sys_msg_id=0; __message_gu_msg_id=0; __message_cnel_msg_id=0; __message_district_code=420000; __message_in_school=0; UN=qq_37976565; UE=\"[email protected]\"; BT=1508662667082; JSESSIONID=C2ED5A9B0A01ADD063B0219B5A4953F1.tomcat2; LSSC=LSSC-513836-NbJPY2BrWFZud9WPdsjOlzU2RyQrOe-passport.csdn.net; dc_tos=oy82pe; dc_session_id=1508499845140_0.037202" +
                        "744870221016; Hm_lvt_6bcd52f51e9b3dce32bec4a3997715ac=1508593384,1508659648,1508659652,1508671911" +
                        "; Hm_lpvt_6bcd52f51e9b3dce32bec4a3997715ac=1508671925");
            }
            response=httpClient.execute(post);
     

            HttpEntity entity=response.getEntity();
            content=EntityUtils.toString(entity);
            EntityUtils.consume(entity);
            if (content.indexOf("redirect_back")>-1){
                System.out.println("登陆成功....");
                //   result=true;
            }
            else if(content.indexOf("登陆太频繁")>-1){
                System.out.println("登陆太频繁");
            }else {
                System.out.println("登陆失败");
            }

            HttpGet get=new HttpGet("http://ask.csdn.net/");//验证Cookie是否设置成功
            String c = setCookie(response);
            //将cookie注入到get请求头当中
            get.setHeader("Cookie",c);
            HttpResponse r = httpClient.execute(get);
            String content1 = EntityUtils.toString(r.getEntity());
            return  content1;
        }catch (Exception e){
            e.printStackTrace();
        }
        return content;
}
主函数Main()
public class Main {
    private static String Login_url="HTTPS://passport.csdn.net/account/login";
   // private static String Login_url="://www.zhihu.com/login/phone_num";
    public static void main(String[] args)throws Exception {
        // write your code here
        Logger logger=Logger.getLogger("cyc");
        String username=" ";//账号
        String password="      ";//密码
       // String captcha_type;
        //String _xsrf;
        String it=null;
        String execution=null;
        String _eventId=null;
        logger.info("获取必要的登陆信息.....");
        String html= Zhihu.sendGet(Login_url);
        Document doc= Jsoup.parse(html);
        Element from=doc.select(".user-pass").get(0);
        System.out.println(html);

        it=from.select("input[name=lt]").get(0).val();

        execution=from.select("input[name=execution]").get(0).val();
        _eventId=from.select("input[name=_eventId]").get(0).val();
        //_xsrf=from.select("input[name=_xsrf]").get(0).val();
        //captcha_type=from.select("input[name=captcha_type]").get(0).val();

        System.out.println(it);
        System.out.println(execution);
        System.out.println(_eventId);
   //     System.out.println(captcha_type);
   //     System.out.println(_xsrf);
        System.out.println("");
        logger.info("获取成功.....");
        logger.info("开始登录.....");
       // boolean result=false;
        List nvps=new ArrayList();
        nvps.add(new BasicNameValuePair("username",username));
        nvps.add(new BasicNameValuePair("password",password));
        nvps.add(new BasicNameValuePair("lt",it));
        nvps.add(new BasicNameValuePair("execution",execution));
        nvps.add(new BasicNameValuePair("_eventId",_eventId));
        String ret=Zhihu.sendPost(Login_url,nvps);
        System.out.println(ret);
    }
}
登陆成功的结果打印

    
        
        
        
        
    
    
        
        
            
        
    

登陆



  
    
    
    编程技术问答-CSDN问答频道
    
    
    
    
    
    
    

    
    
    
    
    

       

    
    
    
    
    
    

    
    
    

········
    
        /现在要去取消关注
                       do_unfollow_url = 'http://my.csdn.net/index.php/follow/do_unfollow?username='+now_item_name+'&jsonpcallback=?';
                       $.ajax({
                               type: "get",
                               url: do_unfollow_url,
                               dataType:"jsonp",
                               success: function(data){
                                if(parseInt(data.succ)==1)
                                {
                                  $(item).removeClass('focus_cancel');
                                  //$(item).html(''+''+' 关注');
                                  //$(item).removeAttr("style");
                                  //$(item)[0].onmouseover = null;
                                  //$(item)[0].onmouseout = null;
                                }
                                 btn_state=1;
                               }

                          });
                    }
                }
             }
         });
        }
        else
        {
            $(item)[0].onclick = function(){
                window.location.href="https://passport.csdn.net/";
            }
        }
    });
}
showConcern();


你可能感兴趣的:(java模拟登陆)