• 新浪微博数据抓取(java实现)


      1 多了不说,直接贴出相关部分的实现代码
      2 
      3  
      4 
      5 加密部分实现:
      6 
      7  
      8 
      9 package token.exe;
     10 
     11 import java.math.BigInteger;
     12 import java.util.Random;
     13 
     14 import org.apache.commons.codec.binary.Base64;
     15 
     16 public class WeiboEncoder {
     17 
     18     private static BigInteger n = null;
     19     private static BigInteger e = null;
     20     
     21     /**
     22      * 使用Base64加密用户名(su的获取)
     23      * @param account
     24      * @return
     25      */
     26     @SuppressWarnings("deprecation")
     27     public static String encodeAccount(String account){        
     28         return new String(Base64.encodeBase64(account.getBytes()));
     29     }
     30     
     31     /**
     32      * 使用RSAEncrypt对用户密码进行加密(sp的获取)
     33      * @param pwd
     34      * @param nStr
     35      * @param eStr
     36      * @return
     37      */
     38     public static String RSAEncrypt(String pwd, String nStr, String eStr){
     39         n = new BigInteger(nStr,16);
     40         e = new BigInteger(eStr,16);
     41         
     42         BigInteger r = RSADoPublic(pkcs1pad2(pwd,(n.bitLength()+7)>>3));
     43         String sp = r.toString(16);
     44         if((sp.length()&1) != 0 ) 
     45             sp = "0" + sp;
     46         return sp;
     47     }
     48     
     49     private static BigInteger RSADoPublic(BigInteger x){
     50          return x.modPow(e, n);
     51     }
     52     
     53     private static BigInteger pkcs1pad2(String s, int n){
     54         if(n < s.length() + 11) { // TODO: fix for utf-8
     55             System.err.println("Message too long for RSA");
     56             return null;
     57           }
     58         byte[] ba = new byte[n];
     59         int i = s.length()-1;
     60         while(i >= 0 && n > 0) {
     61             int c = s.codePointAt(i--);
     62             if(c < 128) { // encode using utf-8
     63               ba[--n] = new Byte(String.valueOf(c));
     64             }
     65             else if((c > 127) && (c < 2048)) {
     66               ba[--n] = new Byte(String.valueOf((c & 63) | 128));
     67               ba[--n] = new Byte(String.valueOf((c >> 6) | 192));
     68             }
     69             else {
     70               ba[--n] = new Byte(String.valueOf((c & 63) | 128));
     71               ba[--n] = new Byte(String.valueOf(((c >> 6) & 63) | 128));
     72               ba[--n] = new Byte(String.valueOf((c >> 12) | 224));
     73             }
     74           }
     75         ba[--n] = new Byte("0");
     76         
     77         byte[] temp = new byte[1];
     78         Random rdm = new Random(47L);
     79         
     80         while(n > 2) { // random non-zero pad
     81             temp[0] = new Byte("0");
     82             while(temp[0] == 0) 
     83                 rdm.nextBytes(temp);
     84             ba[--n] = temp[0];
     85         }
     86         ba[--n] = 2;
     87         ba[--n] = 0;
     88         
     89         return new BigInteger(ba);
     90     }
     91     
     92     
     93     
     94 }
     95 
     96  参数实体:
     97 
     98  
     99 
    100 package token.def;
    101 
    102 import java.io.Serializable;
    103 
    104 public class LoginParams implements Serializable {
    105     
    106     private static final long serialVersionUID = -5775728968372860382L;
    107     private String pcid;
    108     private String servertime;
    109     private String nonce;
    110     private String rsakv;
    111     private String imgUrl;
    112     private String sp;
    113     private String code;
    114     private boolean isLogin = true;
    115     
    116     public String getPcid() {
    117         return pcid;
    118     }
    119     
    120     public void setPcid(String pcid) {
    121         this.pcid = pcid;
    122     }
    123     
    124     public String getServertime() {
    125         return servertime;
    126     }
    127     
    128     public void setServertime(String servertime) {
    129         this.servertime = servertime;
    130     }
    131     
    132     public String getNonce() {
    133         return nonce;
    134     }
    135     public void setNonce(String nonce) {
    136         this.nonce = nonce;
    137     }
    138     
    139     public String getRsakv() {
    140         return rsakv;
    141     }
    142     
    143     public void setRsakv(String rsakv) {
    144         this.rsakv = rsakv;
    145     }
    146     
    147     public String getImgUrl() {
    148         return imgUrl;
    149     }
    150 
    151     public void setImgUrl(String imgUrl) {
    152         this.imgUrl = imgUrl;
    153     }
    154     
    155     public String getSp() {
    156         return sp;
    157     }
    158 
    159     public void setSp(String sp) {
    160         this.sp = sp;
    161     }
    162 
    163     public String getCode() {
    164         return code;
    165     }
    166 
    167     public void setCode(String code) {
    168         this.code = code;
    169     }
    170 
    171     public boolean isLogin() {
    172         return isLogin;
    173     }
    174 
    175     public void setLogin(boolean isLogin) {
    176         this.isLogin = isLogin;
    177     }
    178 
    179     @Override
    180     public String toString() {
    181         return "LoginParams [pcid=" + pcid + ", servertime=" + servertime
    182                 + ", nonce=" + nonce + ", rsakv=" + rsakv + ", imgUrl="
    183                 + imgUrl + ", sp=" + sp + ", code=" + code + ", isLogin="
    184                 + isLogin + "]";
    185     }
    186     
    187 }
    188 
    189  
    190 
    191  
    192 
    193  
    194 
    195  登陆部分实现:
    196 
    197  
    198 
    199 package token.exe;
    200 
    201 import java.io.FileOutputStream;
    202 import java.io.IOException;
    203 import java.net.URLEncoder;
    204 import java.security.KeyManagementException;
    205 import java.security.NoSuchAlgorithmException;
    206 import java.util.ArrayList;
    207 import java.util.Date;
    208 import java.util.HashMap;
    209 import java.util.List;
    210 import java.util.Properties;
    211 import java.util.Scanner;
    212 
    213 import org.apache.commons.httpclient.Header;
    214 import org.apache.commons.httpclient.HttpClient;
    215 import org.apache.commons.httpclient.HttpException;
    216 import org.apache.commons.httpclient.HttpStatus;
    217 import org.apache.commons.httpclient.HttpVersion;
    218 import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
    219 import org.apache.commons.httpclient.NameValuePair;
    220 import org.apache.commons.httpclient.cookie.CookiePolicy;
    221 import org.apache.commons.httpclient.methods.GetMethod;
    222 import org.apache.commons.httpclient.methods.PostMethod;
    223 import org.apache.commons.httpclient.params.HttpClientParams;
    224 import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
    225 import org.apache.commons.httpclient.protocol.Protocol;
    226 import org.jsoup.Jsoup;
    227 import org.jsoup.nodes.Document;
    228 import org.jsoup.nodes.Element;
    229 
    230 import token.SinaWeiboOAuth;
    231 import token.def.LoginParams;
    232 import weibo4j.model.MySSLSocketFactory;
    233 
    234 
    235 public class WeiboLoginer {
    236     
    237     private HttpClient httpClient; //httpClient实例初始化
    238     
    239     public  WeiboLoginer() {
    240         
    241         //httpclient连接配置
    242         MultiThreadedHttpConnectionManager httpManager = new MultiThreadedHttpConnectionManager();
    243         HttpConnectionManagerParams connectParams = httpManager.getParams();
    244         connectParams.setConnectionTimeout(3000);
    245         connectParams.setDefaultMaxConnectionsPerHost(100);
    246         connectParams.setSoTimeout(3000);        
    247         //httpclient参数配置
    248         HttpClientParams httpParams = new HttpClientParams();    
    249         httpParams.setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
    250         httpParams.setVersion(HttpVersion.HTTP_1_1);
    251         //设置默认Header
    252         List<Header> headers = new ArrayList<Header>();
    253         headers.add(new Header("Content-Type", "application/x-www-form-urlencoded"));
    254         headers.add(new Header("Host", "login.sina.com.cn"));
    255         headers.add(new Header("User-Agent","Mozilla/5.0 (Windows NT 6.1; rv:25.0) Gecko/20100101 Firefox/25.0"));
    256         headers.add(new Header("API-RemoteIP", "192.168.0.1"));//伪造新浪验证IP
    257         headers.add(new Header("X-Forwarded-For","192.168.0.1"));//伪造真实IP
    258         headers.add(new Header("CLIENT-IP", "192.168.0.1"));//伪造客户端IP
    259         //初始化httpclient
    260         httpClient = new HttpClient(httpParams, httpManager);
    261         httpClient.getHostConfiguration().getParams().setParameter("http.default-headers", headers);
    262         //设置ssl协议
    263         Protocol protocol = new Protocol("https",new MySSLSocketFactory(), 443);
    264         Protocol.registerProtocol("https", protocol);        
    265         //设置代理
    266 //        httpClient.getHostConfiguration().setProxy("", 0);
    267 //        httpClient.getParams().setAuthenticationPreemptive(false);
    268     }
    269     
    270     /**
    271      * 登陆并获取code值,如果出现验证码则返回还有验证码的参数信息
    272      * @return 
    273      */
    274     public LoginParams doLogin(String username, String password) {
    275         
    276         Properties properties = initProperties();
    277         String base64UserCount = WeiboEncoder.encodeAccount(username);
    278         HashMap<String, String> pubkeyMap = null;
    279         String sp = null;
    280         String imgUrl = null;
    281         LoginParams loginParams = new LoginParams();
    282         try {
    283             pubkeyMap = pubKeyMap(base64UserCount);
    284             sp = WeiboEncoder.RSAEncrypt(password, pubkeyMap.get("pubkey"),"10001");
    285             imgUrl = getPin(pubkeyMap);
    286             if (imgUrl != null) {
    287                 loginParams.setPcid(pubkeyMap.get("pcid"));
    288                 loginParams.setNonce(pubkeyMap.get("nonce"));
    289                 loginParams.setServertime(pubkeyMap.get("servertime"));
    290                 loginParams.setRsakv(pubkeyMap.get("rsakv"));
    291                 loginParams.setImgUrl(imgUrl);
    292                 loginParams.setSp(sp);
    293                 return loginParams;
    294             }
    295         } catch (IOException e) {
    296             // TODO Auto-generated catch block
    297             e.printStackTrace();
    298         }
    299         
    300         HashMap<String, String> ticketMap = null;
    301         try {
    302             ticketMap = getTicket(base64UserCount, sp, pubkeyMap);
    303         } catch (Exception e1) {
    304             // TODO Auto-generated catch block
    305             e1.printStackTrace();
    306         }
    307         
    308         //确认在最终登陆后是否再需要验证码(账号为新浪的注册邮箱)
    309         String vcUrl = isHasPinAgain(pubkeyMap, ticketMap);
    310         if (vcUrl != null) {
    311             loginParams.setPcid(pubkeyMap.get("pcid"));
    312             loginParams.setNonce(pubkeyMap.get("nonce"));
    313             loginParams.setServertime(pubkeyMap.get("servertime"));
    314             loginParams.setRsakv(pubkeyMap.get("rsakv"));
    315             loginParams.setImgUrl(imgUrl);
    316             loginParams.setSp(sp);
    317             return loginParams;
    318         }
    319         
    320         try {
    321             String code = authorize(ticketMap.get("ticket"), properties.getProperty("authorizeURL"), 
    322                     properties.getProperty("redirect_URI"), properties.getProperty("client_ID"), 
    323                     username, ticketMap.get("uid"));
    324             
    325             loginParams.setCode(code);
    326         } catch (KeyManagementException e) {
    327             // TODO Auto-generated catch block
    328             e.printStackTrace();
    329         } catch (NoSuchAlgorithmException e) {
    330             // TODO Auto-generated catch block
    331             e.printStackTrace();
    332         } catch (IOException e) {
    333             // TODO Auto-generated catch block
    334             e.printStackTrace();
    335         }
    336         return loginParams;
    337     
    338     }
    339     
    340     /**
    341      * 有验证码时登陆
    342      * @param sp
    343      * @param pin
    344      * @param pcid
    345      * @param servertime
    346      * @param nonce
    347      * @param rsakv
    348      * @return
    349      */
    350     public LoginParams doLoginByPin(String username, String sp, String pin, String pcid, 
    351             String servertime,String nonce,String rsakv ) {
    352         
    353         Properties properties = initProperties();
    354         String base64UserCount = WeiboEncoder.encodeAccount(username);
    355         HashMap<String, String> ticketMap = null;
    356         LoginParams params = new LoginParams();
    357         try {
    358             ticketMap = getTicket(base64UserCount, sp, pin, pcid, 
    359                     servertime, nonce, rsakv);
    360             if (ticketMap.containsKey("reason")) {
    361                 //意为"输入的验证码不正确"
    362                 String reply = "\u8f93\u5165\u7684\u9a8c\u8bc1\u7801\u4e0d\u6b63\u786e";
    363                 String reasonStr = ticketMap.get("reason");
    364                 if (reasonStr.equals(reply)) {
    365                     params.setLogin(false);
    366                     return params;
    367                 }
    368             }
    369             String code = authorize(ticketMap.get("ticket"), properties.getProperty("authorizeURL"), 
    370                     properties.getProperty("redirect_URI"), properties.getProperty("client_ID"), 
    371                     username, ticketMap.get("uid"));
    372             params.setCode(code);
    373         } catch (Exception e) {
    374             // TODO Auto-generated catch block
    375             e.printStackTrace();
    376         }
    377         
    378         return params;
    379     }
    380     
    381     /**
    382      * 模拟新浪授权
    383      * @param ticket ticket参数
    384      * @param redirectURI 回调地址
    385      * @param clientId appKey
    386      * @param username 用户名
    387      * @return token
    388      * @throws IOException
    389      * @throws KeyManagementException
    390      * @throws NoSuchAlgorithmException
    391      */
    392     private String authorize(String ticket, String authorizeURL, String redirectURI,
    393             String clientId, String username, String uid) throws IOException,
    394             KeyManagementException, NoSuchAlgorithmException {
    395         
    396         String code = null;
    397         String url = authorizeURL + "?client_id=" + clientId + "&redirect_uri=" 
    398                 + redirectURI + "&response_type=code&forcelogin=true";
    399         String regCallback = authorizeURL + "?client_id=" + clientId + "&redirect_uri=" 
    400                 + redirectURI + "&response_type=code&display=default&from=&with_cookie=";
    401         PostMethod post = new PostMethod(authorizeURL);    
    402         //模拟申请token的链接,如果不添加,那么回调地址返回则为空        
    403         post.setRequestHeader("Referer",url);
    404         // 模拟登录时所要提交的参数信息        
    405         NameValuePair[] formpPairs=new NameValuePair[]{
    406                 new NameValuePair("action", "login"),
    407                 new NameValuePair("userId",username),
    408                 new NameValuePair("ticket", ticket),
    409                 new NameValuePair("response_type", "code"),
    410                 new NameValuePair("redirect_uri", redirectURI),
    411                 new NameValuePair("client_id", clientId),
    412                 new NameValuePair("regCallback", URLEncoder.encode(regCallback, "UTF-8"))
    413                 };
    414         post.setRequestBody(formpPairs);
    415         int status = httpClient.executeMethod(post);
    416         if (status == HttpStatus.SC_OK) {
    417             byte[] htmlDatas = post.getResponseBody();
    418             code = authorizeAgain(htmlDatas, ticket, authorizeURL, 
    419                     redirectURI, clientId, username, uid);
    420         }else if (status == 302) {
    421             Header locationHeader = post.getResponseHeader("location");
    422             String location = locationHeader.getValue();
    423             code = location.substring(location.indexOf("=")+1);
    424         }        
    425                 
    426         return code;
    427     }
    428     
    429     /**
    430      * 二次提交授权申请
    431      * @param htmlDatas 第一次授权申请返回的页面数据
    432      * @return
    433      * @throws IOException 
    434      * @throws HttpException 
    435      */
    436     private String authorizeAgain(byte[] htmlDatas, String ticket, String authorizeURL, 
    437             String redirectURI,String clientId, String username,
    438             String uid) throws HttpException, IOException {
    439         
    440         String verifyToken = null;
    441         String html = new String(htmlDatas, "utf-8");
    442         Document doc = Jsoup.parse(html);
    443         Element verifyTokeneElement = doc.select("input[name=verifyToken]").first();
    444         verifyToken = verifyTokeneElement.attr("value");
    445         
    446         
    447         String code = null;
    448         String url = authorizeURL + "?client_id=" + clientId + "&redirect_uri=" 
    449                 + redirectURI + "&response_type=code&forcelogin=true";
    450         String regCallback = authorizeURL + "?client_id=" + clientId + "&redirect_uri=" 
    451                 + redirectURI + "&response_type=code&display=default&from=&with_cookie=";
    452         PostMethod post = new PostMethod(authorizeURL);    
    453         //模拟申请token的链接,如果不添加,那么回调地址返回则为空        
    454         post.setRequestHeader("Referer",authorizeURL);
    455         // 模拟登录时所要提交的参数信息        
    456         NameValuePair[] formpPairs=new NameValuePair[]{
    457                 new NameValuePair("action", "authorize"),
    458                 new NameValuePair("uid",uid),
    459                 new NameValuePair("url", url),
    460                 new NameValuePair("response_type", "code"),
    461                 new NameValuePair("redirect_uri", redirectURI),
    462                 new NameValuePair("client_id", clientId),
    463                 new NameValuePair("verifyToken", verifyToken),
    464                 new NameValuePair("regCallback", URLEncoder.encode(regCallback, "UTF-8"))
    465                 };
    466         post.setRequestBody(formpPairs);
    467         int status = httpClient.executeMethod(post);
    468         if (status == 302) {
    469             Header locationHeader = post.getResponseHeader("location");
    470             String location = locationHeader.getValue();
    471             if (location == null) {
    472                 throw new NullPointerException("redirect_uri is null");
    473             }
    474             code = location.substring(location.indexOf("=")+1);
    475         }    
    476         return code;
    477     }
    478     
    479     /**
    480      * 模拟用户预登录
    481      * @param unameBase64
    482      * @return
    483      * @throws IOException
    484      */
    485     private HashMap<String, String> pubKeyMap(String unameBase64)
    486             throws IOException {
    487         
    488         String url = "https://login.sina.com.cn/sso/prelogin.php?"
    489                 + "entry=openapi&"
    490                 + "callback=sinaSSOController.preloginCallBack&" + "su="
    491                 + unameBase64 + "&" + "rsakt=mod&" + "checkpin=1&"
    492                 + "client=ssologin.js(v1.4.5)" + "&_=" + new Date().getTime();
    493         return getParaFromResult(get(url));
    494     }
    495     
    496     /** 
    497      * 预登陆是否需要验证码
    498      * @param pubkeyMap 
    499      * @return 
    500      */
    501     private String getPin(HashMap<String, String> pubkeyMap) {
    502         
    503         String imgUrl = null;
    504         int isShowpin = 0;
    505         if (pubkeyMap != null) {
    506             String showpin = pubkeyMap.get("showpin");
    507             if (showpin != null) {
    508                 isShowpin = Integer.parseInt(showpin);
    509                 if (isShowpin == 1) {
    510                     String url = "https://login.sina.com.cn/cgi/pin.php?"
    511                             + "r=" + Math.floor(Math.random() * 100000000)
    512                             + "&s=0"
    513                             + "&p=" + pubkeyMap.get("pcid"); 
    514                     
    515                     imgUrl = url;
    516                 }
    517             }
    518         }
    519         return imgUrl;
    520     }
    521     
    522     
    523     /**
    524      * 确认登陆后是否需要再验证
    525      * @return 
    526      */
    527     private String isHasPinAgain(HashMap<String, String> pubkeyMap, 
    528             HashMap<String, String> ticketMap) {
    529         
    530         String imgUrl = null;
    531         int isHasPin = 0;
    532         if ((pubkeyMap != null) && (ticketMap != null)) {
    533             //意为"为了您的帐号安全,请输入验证码"
    534             String str = "\u4e3a\u4e86\u60a8\u7684\u5e10\u53f7\u5b89" +
    535                     "\u5168\uff0c\u8bf7\u8f93\u5165\u9a8c\u8bc1\u7801";
    536             
    537             if (ticketMap.containsKey("reason")) {
    538                 String reasonStr = ticketMap.get("reason");
    539                 if (reasonStr.equals(str)) {
    540                     isHasPin = 1;
    541                     String url = "https://login.sina.com.cn/cgi/pin.php?"
    542                             + "r=" + Math.floor(Math.random() * 100000000)
    543                             + "&s=0"
    544                             + "&p=" + pubkeyMap.get("pcid");
    545                     
    546                     imgUrl = url;
    547                 }
    548             }
    549         }
    550         return imgUrl;
    551     }
    552     
    553     /**
    554      * 获取验证码
    555      */
    556     public String getVCode(String pcid) {
    557         
    558         String imgUrl = null;
    559         if (pcid != null) {
    560             String url = "https://login.sina.com.cn/cgi/pin.php?"
    561                     + "r=" + Math.floor(Math.random() * 100000000)
    562                     + "&s=0"
    563                     + "&p=" + pcid;
    564             
    565             imgUrl = url;
    566         }
    567         return imgUrl;
    568     }
    569     
    570     /**
    571      * 保存验证码
    572      * @param url 验证码链接
    573      */
    574     public void saveVCodeImg(String url) {
    575         
    576         GetMethod getImages = new GetMethod(url);
    577         try {
    578             int status = httpClient.executeMethod(getImages);
    579             if (status == HttpStatus.SC_OK) {
    580                 FileOutputStream outputStream = new FileOutputStream("vc.jpg");
    581                 outputStream.write(getImages.getResponseBody());
    582                 outputStream.close();
    583             }
    584         } catch (HttpException e) {
    585             // TODO Auto-generated catch block
    586             e.printStackTrace();
    587         } catch (IOException e) {
    588             // TODO Auto-generated catch block
    589             e.printStackTrace();
    590         }
    591         
    592     }
    593     
    594     /**
    595      * 无验证码时模拟用户登录,并获取ticket
    596      * @param usernameBase64 使用Base64加密的用户名
    597      * @param sp 使用SHA1加密后的用户密码
    598      * @return
    599      * @throws Exception
    600      */
    601     private HashMap<String, String> getTicket(String usernameBase64, 
    602             String sp, HashMap<String, String> pubkeyMap) throws Exception {
    603         String url = null;
    604         if (pubkeyMap != null) {
    605             url = "https://login.sina.com.cn/sso/login.php?"
    606                     + "entry=openapi&" 
    607                     + "gateway=1&" 
    608                     + "from=&"
    609                     + "savestate=0&"   
    610                     + "useticket=1&"
    611                     + "pagerefer=&"
    612                     + "ct=1800&" 
    613                     + "s=1&" 
    614                     + "vsnf=1&" 
    615                     + "vsnval=&"
    616                     + "door=&"
    617                     + "su="+ usernameBase64
    618                     + "&"
    619                     + "service=miniblog&"
    620                     + "servertime="+ pubkeyMap.get("servertime")
    621                     + "&"
    622                     + "nonce="+ pubkeyMap.get("nonce")
    623                     + "&"
    624                     + "pwencode=rsa&"
    625                     + "rsakv="+ pubkeyMap.get("rsakv")
    626                     + "&"
    627                     + "sp="+ sp
    628                     + "&"
    629                     + "encoding=UTF-8&"
    630                     + "callback=sinaSSOController.loginCallBack&"
    631                     + "cdult=2&"
    632                     + "domain=weibo.com&"
    633                     + "prelt=37&"
    634                     + "returntype=TEXT&"
    635                     + "client=ssologin.js(v1.4.5)&" + "_=" + new Date().getTime();
    636                     
    637         }
    638         return getParaFromResult(get(url));
    639     }
    640     
    641     
    642     /**
    643      * 有验证码时模拟用户登录,并获取ticket
    644      * @param usernameBase64
    645      * @param sp
    646      * @param pin
    647      * @param pcid
    648      * @param servertime
    649      * @param nonce
    650      * @param rsakv
    651      * @return
    652      * @throws Exception
    653      */
    654     public HashMap<String, String> getTicket(String usernameBase64, String sp, String pin, 
    655             String pcid, String servertime,String nonce,String rsakv) throws Exception {
    656         
    657         String url = "https://login.sina.com.cn/sso/login.php?"
    658                         + "entry=openapi&" 
    659                         + "gateway=1&" 
    660                         + "from=&"
    661                         + "savestate=0&"   
    662                         + "useticket=1&"
    663                         + "pagerefer=&"
    664                         + "pcid=" + pcid + "&"
    665                         + "ct=1800&" 
    666                         + "s=1&" 
    667                         + "vsnf=1&" 
    668                         + "vsnval=&"
    669                         + "door=" + pin + "&"
    670                         + "su="+ usernameBase64
    671                         + "&"
    672                         + "service=miniblog&"
    673                         + "servertime="+ servertime
    674                         + "&"
    675                         + "nonce="+ nonce
    676                         + "&"
    677                         + "pwencode=rsa&"
    678                         + "rsakv="+ rsakv
    679                         + "&"
    680                         + "sp="+ sp
    681                         + "&"
    682                         + "encoding=UTF-8&"
    683                         + "callback=sinaSSOController.loginCallBack&"
    684                         + "cdult=2&"
    685                         + "domain=weibo.com&"
    686                         + "prelt=37&"
    687                         + "returntype=TEXT&"
    688                         + "client=ssologin.js(v1.4.5)&" + "_=" + new Date().getTime();
    689                         
    690         return getParaFromResult(get(url));
    691     }
    692     
    693     /**
    694      * 分析结果,取出所需参数
    695      * @param result 页面内容
    696      * @return
    697      */
    698     private HashMap<String, String> getParaFromResult(String result) {
    699         
    700         HashMap<String, String> hm = new HashMap<String, String>();
    701         result = result.substring(result.indexOf("{") + 1, result.indexOf("}"));
    702         String[] r = result.split(",");
    703         String[] temp;
    704         for (int i = 0; i < r.length; i++) {
    705             temp = r[i].split(":");
    706             for (int j = 0; j < 2; j++) {
    707                 if (temp[j].contains("""))
    708                     temp[j] = temp[j].substring(1, temp[j].length() - 1);
    709             }
    710             hm.put(temp[0], temp[1]);
    711         }
    712         return hm;
    713     }
    714     
    715     /**
    716      * 执行给定的URL,并输出目标URL返回的页面结果
    717      * @param url
    718      * @return
    719      * @throws IOException
    720      */
    721     private String get(String url) throws IOException {
    722         
    723         String surl = null;
    724         GetMethod getMethod = new GetMethod(url);
    725         int status = httpClient.executeMethod(getMethod);
    726         if (status == HttpStatus.SC_OK) {
    727             surl = new String(getMethod.getResponseBody(), "UTF-8");
    728         }
    729         getMethod.releaseConnection();
    730         return surl;
    731     }
    732     
    733     /**
    734      * 配置信息初始化
    735      * @return
    736      */
    737     private Properties initProperties() {
    738         
    739         Properties prop = new Properties();
    740         try {
    741             prop.load(Thread.currentThread().getContextClassLoader().
    742                     getResourceAsStream("config.properties"));
    743             
    744         } catch (IOException e) {
    745             // TODO Auto-generated catch block
    746             e.printStackTrace();
    747         }
    748         return prop;
    749     }
    750     
    751     /**
    752      * @param args
    753      */
    754     public static void main(String[] args) {
    755                 
    756         WeiboLoginer loginer = new WeiboLoginer();
    757         LoginParams loginParams = loginer.doLogin("","");
    758             //有验证码时
    759         if (loginParams.getCode() == null) {
    760             String pcid = loginParams.getPcid();
    761             String nonce = loginParams.getNonce();
    762             String rsakv = loginParams.getRsakv();
    763             String servertime = loginParams.getServertime();
    764             String sp = loginParams.getSp();
    765             
    766             System.err.println(loginParams.getImgUrl());
    767             //再次获取验证码
    768             System.err.println(loginer.getVCode(pcid));
    769             
    770             Scanner input = new Scanner(System.in);
    771             String pin = input.nextLine();
    772             
    773             LoginParams loginResult = loginer.doLoginByPin("",sp, pin, pcid, servertime, nonce, rsakv);
    774             if (!loginResult.isLogin()) {
    775                 System.err.println("验证码错误!重新录入");
    776                 
    777                 //获取验证码并保存(测试)
    778                 String imgUrl = loginer.getVCode(pcid);
    779                 loginer.saveVCodeImg(imgUrl);
    780                 
    781                 Scanner input1= new Scanner(System.in);
    782                 String pin1 = input1.nextLine();
    783                 
    784                 String code = loginer.doLoginByPin("",sp, pin1, pcid, servertime, nonce, rsakv).getCode();
    785                 System.out.println(SinaWeiboOAuth.getToken(code));
    786             }
    787 
    788         }else {
    789             //无验证码时
    790             String code = loginParams.getCode();
    791             System.out.println(SinaWeiboOAuth.getToken(code));
    792         }
    793     }
    794 
    795 }
    796 
    797  
    798 
    799  上述代码完整模拟了微博登陆的全过程,并获得最终授权
    800 
    801  
    802 
    803  
  • 相关阅读:
    防止死锁的加锁机制
    python线程threading.Timer源码解读
    python语言线程标准库threading.local源码解读
    栈和队列的总结
    如何根据入栈序列判断可能的出栈序列
    使用 Air 热编译 Gin 项目
    【Golang设计模式】7.外观模式
    Go中的数据类型、指针、new和make
    【Golang设计模式】6.模板方法模式
    【Golang设计模式】5.原型模式
  • 原文地址:https://www.cnblogs.com/zhengbing/p/3459170.html
Copyright © 2020-2023  润新知