HttpClient 4.5.3 模拟登陆CSDN

本文使用httpcomponents-client-4.5.3和httpcomponents-core-4.4.6进行CSDN模拟登陆。

//step 1:get some necessary information about login
        HttpClientBuilder httpClientBuilder2=HttpClients.custom();
        httpClientBuilder2.setUserAgent("");//重要,否者CSDN会拒绝访问
        CloseableHttpClient httpClient2=httpClientBuilder2.build();
        HttpGet httpGet=new HttpGet("https://passport.csdn.net/account/login");
        CloseableHttpResponse response=httpClient2.execute(httpGet);
        //这里使用Jsoup第三方包来解析网页
        Document document=Jsoup.parse(response.getEntity().getContent(),"utf-8","http://www.csdn.net");
        String lt = document.select("#fm1 > input[type='hidden']:nth-child(5)").get(0).val();
        String execution=document.select("#fm1 > input[type='hidden']:nth-child(6)").get(0).val();
        String eventId=document.select("#fm1 > input[type='hidden']:nth-child(7)").get(0).val();

        //step 2:post the login information
        HttpPost httpPost=new HttpPost("https://passport.csdn.net/account/login");
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();   
        nvps.add(new BasicNameValuePair("username", "****"));  
        nvps.add(new BasicNameValuePair("password", "****")); 
        nvps.add(new BasicNameValuePair("lt", lt));  
        nvps.add(new BasicNameValuePair("execution",execution));  
        nvps.add(new BasicNameValuePair("_eventId", eventId));  
        httpPost.setEntity(new UrlEncodedFormEntity(nvps,HTTP.UTF_8));
        CloseableHttpResponse response2=httpClient2.execute(httpPost);  
        for (Header header : response2.getAllHeaders()){
            System.out.println(header.getName()+" "+header.getValue());
        }
        System.out.println(response2.getStatusLine().getStatusCode());
        System.out.println(IOUtils.toString(response2.getEntity().getContent(),"utf-8"));


        //step 3 : visit VIP page
        HttpGet httpGetCsdn=new HttpGet("https://my.csdn.net/my/score");
        CloseableHttpResponse response3=httpClient2.execute(httpGetCsdn);
        System.out.println(response3.getStatusLine().getStatusCode());
        System.out.println(IOUtils.toString(response3.getEntity().getContent(),"utf-8"));
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值