新浪微博模拟登录(Java)

简介:

     使用HttpClient模拟新浪微博登录,贴在这里做个备忘,也希望能帮助到有这方面需求的童鞋们,代码如下:
      

Java代码   收藏代码
  1. package com.yida.spider4j.crawler.test.sina;  
  2.   
  3. import java.io.UnsupportedEncodingException;  
  4. import java.math.BigInteger;  
  5. import java.net.URLEncoder;  
  6. import java.security.InvalidKeyException;  
  7. import java.security.KeyFactory;  
  8. import java.security.NoSuchAlgorithmException;  
  9. import java.security.interfaces.RSAPublicKey;  
  10. import java.security.spec.InvalidKeySpecException;  
  11. import java.security.spec.RSAPublicKeySpec;  
  12. import java.util.HashMap;  
  13. import java.util.Map;  
  14.   
  15. import javax.crypto.BadPaddingException;  
  16. import javax.crypto.Cipher;  
  17. import javax.crypto.IllegalBlockSizeException;  
  18. import javax.crypto.NoSuchPaddingException;  
  19.   
  20. import com.yida.spider4j.crawler.utils.common.FastJSonUtils;  
  21. import com.yida.spider4j.crawler.utils.common.StringUtils;  
  22. import com.yida.spider4j.crawler.utils.httpclient.HttpClientUtils;  
  23. import com.yida.spider4j.crawler.utils.httpclient.Result;  
  24. import com.yida.spider4j.crawler.utils.io.FileUtils;  
  25.   
  26. /** 
  27.  * @ClassName: LoginTest 
  28.  * @Description: 新浪微博登录测试 
  29.  * @author Lanxiaowei(736031305@qq.com) 
  30.  * @date 2015年10月29日 下午5:48:58 
  31.  * 
  32.  */  
  33. public class LoginTest {  
  34.     private static final char[] DIGITS_LOWER = { '0''1''2''3''4''5',  
  35.             '6''7''8''9''a''b''c''d''e''f' };  
  36.   
  37.     private static final char[] DIGITS_UPPER = { '0''1''2''3''4''5',  
  38.             '6''7''8''9''A''B''C''D''E''F' };  
  39.   
  40.     public static void main(String[] args) throws Exception {  
  41.         //登录账号和密码  
  42.         String account = "xxxxxxxxx";  
  43.         String pwd = "xxxxxxx";  
  44.           
  45.         //新浪微博登录  
  46.         Result result = login(account,pwd);  
  47.         String cookie = result.getCookie();  
  48.         cookie = getSUB(cookie);  
  49.         System.out.println("cookie:" + cookie);  
  50.           
  51.         //访问【文章同学】的新浪微博首页  
  52.         String html = visitWenZhang(cookie);  
  53.           
  54.         //把返回的html内容写入文件,方便打开进行验证是否正确返回  
  55.         FileUtils.writeFile(html, "C:/wenzhang.html""UTF-8"false);  
  56.           
  57.           
  58.     }  
  59.       
  60.     public static String getSUB(String cookie) {  
  61.         String sub = cookie.replaceAll(".*;SUB=(.*);SUBP=.*""$1");  
  62.         sub = "SUB=" + sub + ";";  
  63.         return sub;  
  64.     }  
  65.       
  66.     /** 
  67.      * @Author: Lanxiaowei(736031305@qq.com) 
  68.      * @Title: visitWenZhang 
  69.      * @Description: 访问【文章同学】的新浪微博 
  70.      * @param @return 
  71.      * @param @throws Exception 
  72.      * @return String 
  73.      * @throws 
  74.      */  
  75.     public static String visitWenZhang(String cookie) throws Exception {  
  76.         String wenzhang = "http://weibo.com/wenzhang626";  
  77.         Map<String,String> headers = new HashMap<String,String>();  
  78.         //SUB cookie项是关键  
  79.         //SUB=_2A257NaVGDeTxGedH7lsZ8yvPwziIHXVYQpGOrDV8PUNbuNAMLROnkW8p9rH2Bsuc2yUSKU1PzJykmlLc7Q..;  
  80.         headers.put("Cookie", cookie);  
  81.         String html = HttpClientUtils.getHTML(wenzhang,headers);  
  82.         //System.out.println(html);  
  83.         return html;  
  84.     }  
  85.       
  86.     /** 
  87.      * @Author: Lanxiaowei(736031305@qq.com) 
  88.      * @Title: login 
  89.      * @Description: 模拟新浪微博登录 
  90.      * @param @param account 
  91.      * @param @param pwd 
  92.      * @param @return 
  93.      * @param @throws Exception 
  94.      * @return String 
  95.      * @throws 
  96.      */  
  97.     public static Result login(String account,String pwd) throws Exception {  
  98.         String url = "http://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.4.18)&_=" +   
  99.                 System.currentTimeMillis();  
  100.         String content = prelogin();  
  101.         Map<String,Object> paramMap = FastJSonUtils.toMap(content);  
  102.         String pubkey = paramMap.get("pubkey").toString();  
  103.         String servertime = paramMap.get("servertime").toString();  
  104.         String nonce = paramMap.get("nonce").toString();  
  105.         String rsakv = paramMap.get("rsakv").toString();  
  106.           
  107.         Map<String,String> headers = new HashMap<String,String>();  
  108.         headers.put("Host""login.sina.com.cn");  
  109.         headers.put("Origin""http://weibo.com");  
  110.         headers.put("Content-Type""application/x-www-form-urlencoded");  
  111.           
  112.         Map<String,String> params = new HashMap<String,String>();  
  113.         params.put("entry""weibo");  
  114.         params.put("gateway""1");  
  115.         params.put("from""");  
  116.         params.put("savestate""7");  
  117.         params.put("useticket""1");  
  118.         params.put("pagerefer""http://s.weibo.com/weibo/%25E6%2596%2587%25E7%25AB%25A0%25E5%2590%258C%25E5%25AD%25A6?topnav=1&wvr=6&b=1");  
  119.         params.put("vsnf""1");  
  120.         params.put("su", encodeAccount(account));  
  121.         params.put("service""miniblog");  
  122.         params.put("servertime", servertime);  
  123.         params.put("nonce", nonce);  
  124.         params.put("pwencode""rsa2");  
  125.         params.put("rsakv", rsakv);  
  126.         params.put("sp", getSP(pwd, pubkey, servertime, nonce));  
  127.         params.put("encoding""UTF-8");  
  128.         params.put("cdult""2");  
  129.         params.put("domain""weibo.com");  
  130.         params.put("prelt""154");  
  131.         params.put("returntype""TEXT");  
  132.           
  133.         Result result = HttpClientUtils.post(url, headers, params);  
  134.         //System.out.println(json);  
  135.         return result;  
  136.     }  
  137.       
  138.     /** 
  139.      * @Author: Lanxiaowei(736031305@qq.com) 
  140.      * @Title: prelogin 
  141.      * @Description: 登录必需参数获取 
  142.      * @param @return 
  143.      * @param @throws Exception 
  144.      * @return String 
  145.      * @throws 
  146.      */  
  147.     public static String prelogin() throws Exception {  
  148.         String url = "http://login.sina.com.cn/sso/prelogin.php?entry=weibo&callback=sinaSSOController.preloginCallBack&su=&rsakt=mod&client=ssologin.js(v1.4.18)&_=1446099453139";  
  149.         String content = HttpClientUtils.getHTML(url);  
  150.           
  151.         if(null != content && !content.equals("")) {  
  152.             content = content.replaceAll("sinaSSOController.preloginCallBack\\((.*)\\)""$1");  
  153.         }  
  154.         //System.out.println(content);  
  155.         return content;  
  156.     }  
  157.       
  158.     /** 
  159.      * @Author: Lanxiaowei(736031305@qq.com) 
  160.      * @Title: getSP 
  161.      * @Description: 登录密码加密 
  162.      * @param @param pwd 
  163.      * @param @param pubkey 
  164.      * @param @param servertime 
  165.      * @param @param nonce 
  166.      * @param @return 
  167.      * @return String 
  168.      * @throws 
  169.      */  
  170.     public static String getSP(String pwd,String pubkey,String servertime,String nonce) {  
  171.         String t = "10001";  
  172.         String message = servertime + "\t" + nonce + "\n" + pwd;  
  173.         String result = null;  
  174.         try {  
  175.             result = rsa(pubkey, t , message);  
  176.         } catch (InvalidKeyException e) {  
  177.             e.printStackTrace();  
  178.         } catch (IllegalBlockSizeException e) {  
  179.             e.printStackTrace();  
  180.         } catch (BadPaddingException e) {  
  181.             e.printStackTrace();  
  182.         } catch (NoSuchAlgorithmException e) {  
  183.             e.printStackTrace();  
  184.         } catch (InvalidKeySpecException e) {  
  185.             e.printStackTrace();  
  186.         } catch (NoSuchPaddingException e) {  
  187.             e.printStackTrace();  
  188.         } catch (UnsupportedEncodingException e) {  
  189.             e.printStackTrace();  
  190.         }    
  191.         System.out.println("RSA加密后的密码:" + result);  
  192.         return result;  
  193.     }  
  194.   
  195.     /** 
  196.      * @Author: Lanxiaowei(736031305@qq.com) 
  197.      * @Title: encodeAccount 
  198.      * @Description: 登录账号编码 
  199.      * @param @param account 
  200.      * @param @return 
  201.      * @return String 
  202.      * @throws 
  203.      */  
  204.     private static String encodeAccount(String account) {  
  205.         String userName = "";  
  206.         try {  
  207.             userName = StringUtils.base64Encode(URLEncoder.encode(account,  
  208.                     "UTF-8"));  
  209.   
  210.         } catch (UnsupportedEncodingException e) {  
  211.             e.printStackTrace();  
  212.         }  
  213.         return userName;  
  214.     }  
  215.   
  216.     /** 
  217.      * @Author: Lanxiaowei(736031305@qq.com) 
  218.      * @Title: rsa 
  219.      * @Description: RSA加密 
  220.      * @param @param pubkey 
  221.      * @param @param exponentHex 
  222.      * @param @param pwd 
  223.      * @param @return 
  224.      * @param @throws IllegalBlockSizeException 
  225.      * @param @throws BadPaddingException 
  226.      * @param @throws NoSuchAlgorithmException 
  227.      * @param @throws InvalidKeySpecException 
  228.      * @param @throws NoSuchPaddingException 
  229.      * @param @throws InvalidKeyException 
  230.      * @param @throws UnsupportedEncodingException 
  231.      * @return String 
  232.      * @throws 
  233.      */  
  234.     public static String rsa(String pubkey, String exponentHex, String pwd)  
  235.             throws IllegalBlockSizeException, BadPaddingException,  
  236.             NoSuchAlgorithmException, InvalidKeySpecException,  
  237.             NoSuchPaddingException, InvalidKeyException,  
  238.             UnsupportedEncodingException {  
  239.         KeyFactory factory = KeyFactory.getInstance("RSA");  
  240.   
  241.         BigInteger m = new BigInteger(pubkey, 16);  
  242.         BigInteger e = new BigInteger(exponentHex, 16);  
  243.         RSAPublicKeySpec spec = new RSAPublicKeySpec(m, e);  
  244.   
  245.         //创建公钥  
  246.         RSAPublicKey pub = (RSAPublicKey) factory.generatePublic(spec);  
  247.         Cipher enc = Cipher.getInstance("RSA");  
  248.         enc.init(Cipher.ENCRYPT_MODE, pub);  
  249.   
  250.         byte[] encryptedContentKey = enc.doFinal(pwd.getBytes("UTF-8"));  
  251.   
  252.         return new String(encodeHex(encryptedContentKey));  
  253.     }  
  254.   
  255.     protected static char[] encodeHex(final byte[] data, final char[] toDigits) {  
  256.         final int l = data.length;  
  257.         final char[] out = new char[l << 1];  
  258.           
  259.         for (int i = 0, j = 0; i < l; i++) {  
  260.             out[j++] = toDigits[(0xF0 & data[i]) >>> 4];  
  261.             out[j++] = toDigits[0x0F & data[i]];  
  262.         }  
  263.         return out;  
  264.     }  
  265.   
  266.     public static char[] encodeHex(final byte[] data, final boolean toLowerCase) {  
  267.         return encodeHex(data, toLowerCase ? DIGITS_LOWER : DIGITS_UPPER);  
  268.     }  
  269.   
  270.     public static char[] encodeHex(final byte[] data) {  
  271.         return encodeHex(data, true);  
  272.     }  
  273. }  

   很不幸,代码才仅仅公布一天,新浪微博方面就已经做出相应防御措施:如果你访问过于频繁的话,登录接口即http://login.sina.com.cn/sso/prelogin.php?entry=weibo&callback=sinaSSOController.preloginCallBack&su=&rsakt=mod&client=ssologin.js(v1.4.18)&_=1446099453139会直接返回

Html代码   收藏代码
  1. <html><head><script language='javascript'>parent.sinaSSOController.feedBackUrlCallBack({"result":false,"errno":"4049","reason":"\u4e3a\u4e86\u60a8\u7684\u5e10\u53f7\u5b89\u5168\uff0c\u8bf7\u8f93\u5165\u9a8c\u8bc1\u7801"});</script></head><body></body></html>  

   经过Unicode解码后,就是

      这TM比较蛋疼,我暂时不知道解决!
     除此之外,提交参数新浪微博也做了小调整,如图:

      

Java代码   收藏代码
  1. Map<String,String> params = new HashMap<String,String>();  
  2. params.put("entry""weibo");  
  3. params.put("gateway""1");  
  4. params.put("from""");  
  5. params.put("savestate""7");  
  6. params.put("useticket""1");  
  7. params.put("pagerefer""http://s.weibo.com/weibo/%25E6%2596%2587%25E7%25AB%25A0%25E5%2590%258C%25E5%25AD%25A6?topnav=1&wvr=6&b=1");  
  8. params.put("vsnf""1");  
  9. params.put("su", encodeAccount(account));  
  10. params.put("service""miniblog");  
  11. params.put("servertime", servertime);  
  12. params.put("nonce", nonce);  
  13. params.put("pwencode""rsa2");  
  14. params.put("rsakv", rsakv);  
  15. params.put("sp", getSP(pwd, pubkey, servertime, nonce));  
  16. params.put("encoding""UTF-8");  
  17. params.put("sr""1366*768");  
  18. params.put("prelt""1011");  
  19. params.put("url""http://weibo.com/ajaxlogin.php?framelogin=1&callback=parent.sinaSSOController.feedBackUrlCallBack");  
  20. params.put("domain""weibo.com");  
  21. params.put("returntype""META");  

   起始我也就是在研究爬虫想集成爬虫自动登录验证功能,拿新浪微博来试试手测试一下罢了,新浪微博不用紧张。如果你第一次运行此代码可能会模拟登录成功,试了好多次之后,可能也会出现跟我一样的情况,要求输入验证码啦。或许当你看到此篇博客时,新浪微博的登录验证逻辑已经发现变化,上面的代码已经完全失效,但我想还是能够给你们些许提示。验证码破解这个比较有难度,我就不趟这浑水了,你们有兴趣就继续研究去吧.OK,That's all,Thanks your Attention!

转载:http://iamyida.iteye.com/blog/2253117

目录
相关文章
使用Java获得某个新浪微博用户发过的所有微博
使用Java获得某个新浪微博用户发过的所有微博
115 0
使用java代码获取新浪微博应用的access token
使用java代码获取新浪微博应用的access token
142 0
使用java搜索包含了指定关键字的新浪微博
使用java搜索包含了指定关键字的新浪微博
148 0
新浪微博WeiboManageAction的java实现
新浪微博WeiboManageAction的java实现
120 0
使用java程序对新浪微博的内容进行语义分析
使用java程序对新浪微博的内容进行语义分析
115 0
使用java搜索包含了指定关键字的新浪微博
使用java搜索包含了指定关键字的新浪微博
使用java搜索包含了指定关键字的新浪微博
使用java代码获取所有发送给我的新浪微博评论
使用java代码获取所有发送给我的新浪微博评论
使用java代码获取所有发送给我的新浪微博评论
使用Java获得某个新浪微博用户发过的所有微博
使用Java获得某个新浪微博用户发过的所有微博
使用Java获得某个新浪微博用户发过的所有微博
使用java代码获取新浪微博应用的access token
使用java代码获取新浪微博应用的access token
使用java代码获取新浪微博应用的access token

热门文章

最新文章