• 腾讯微博数据抓取(java实现)


      1 不多说,同样贴出相关代码
      2 
      3  
      4 
      5 参数实体:
      6 
      7 package token.def;
      8 
      9 import java.io.Serializable;
     10 import java.util.Properties;
     11 
     12 public class TLoginParams implements Serializable {
     13     
     14     private static final long serialVersionUID = 6120319409538285515L;
     15     private String saltUin;
     16     private String dataRedirect;
     17     private String loginSig;
     18     private String loginUrl;
     19     private String imgURl;
     20     private String imgCookie;
     21     private boolean isLogin = true;
     22     private Properties prop;
     23     
     24     public String getSaltUin() {
     25         return saltUin;
     26     }
     27     public void setSaltUin(String saltUin) {
     28         this.saltUin = saltUin;
     29     }
     30     public String getDataRedirect() {
     31         return dataRedirect;
     32     }
     33     public void setDataRedirect(String dataRedirect) {
     34         this.dataRedirect = dataRedirect;
     35     }
     36     public String getLoginSig() {
     37         return loginSig;
     38     }
     39     public void setLoginSig(String loginSig) {
     40         this.loginSig = loginSig;
     41     }
     42     public String getLoginUrl() {
     43         return loginUrl;
     44     }
     45     public void setLoginUrl(String loginUrl) {
     46         this.loginUrl = loginUrl;
     47     }
     48     public String getImgURl() {
     49         return imgURl;
     50     }
     51     public void setImgURl(String imgURl) {
     52         this.imgURl = imgURl;
     53     }
     54     public String getImgCookie() {
     55         return imgCookie;
     56     }
     57     public void setImgCookie(String imgCookie) {
     58         this.imgCookie = imgCookie;
     59     }
     60     public boolean isLogin() {
     61         return isLogin;
     62     }
     63     public void setLogin(boolean isLogin) {
     64         this.isLogin = isLogin;
     65     }
     66     public Properties getProp() {
     67         return prop;
     68     }
     69     public void setProp(Properties prop) {
     70         this.prop = prop;
     71     }
     72     
     73     @Override
     74     public String toString() {
     75         return "TLoginParams [saltUin=" + saltUin + ", dataRedirect="
     76                 + dataRedirect + ", loginSig=" + loginSig + ", loginUrl="
     77                 + loginUrl + ", imgURl=" + imgURl + ", imgCookie=" + imgCookie
     78                 + ", isLogin=" + isLogin + ", prop=" + prop + "]";
     79     }
     80     
     81 }
     82 
     83  加密实现:
     84 
     85 package token.exe;
     86 
     87 import java.io.ByteArrayOutputStream;
     88 import java.io.UnsupportedEncodingException;
     89 import java.security.MessageDigest;
     90 
     91 public class TencentWeiboEncryption {
     92 
     93     private static final String HEXSTRING = "0123456789ABCDEF";
     94 
     95 
     96     /**
     97      * 获取指定字符串的md5值
     98      * @param originalText
     99      * @return
    100      * @throws Exception
    101      */
    102     private static String md5(String originalText) throws Exception {
    103         
    104         byte buf[] = originalText.getBytes("ISO-8859-1");
    105         StringBuffer hexString = new StringBuffer();
    106         String result = "";
    107         String digit = "";
    108         try {
    109             MessageDigest algorithm = MessageDigest.getInstance("MD5");
    110             algorithm.reset();
    111             algorithm.update(buf);
    112             byte[] digest = algorithm.digest();
    113             for (int i = 0; i < digest.length; i++) {
    114                 digit = Integer.toHexString(0xFF & digest[i]);
    115                 if (digit.length() == 1) {
    116                     digit = "0" + digit;
    117                 }
    118                 hexString.append(digit);
    119             }
    120             result = hexString.toString();
    121         } catch (Exception ex) {
    122             result = "";
    123         }
    124         return result.toUpperCase();
    125     }
    126 
    127 
    128     /**
    129      * 将16进制编码转换为相应的ASCII字符串
    130      * @param md5str
    131      * @return
    132      * @throws UnsupportedEncodingException
    133      */
    134     private static String hexchar2bin(String md5str) throws UnsupportedEncodingException {
    135         
    136         ByteArrayOutputStream baos = new ByteArrayOutputStream(md5str.length() / 2);
    137         for (int i = 0; i < md5str.length(); i = i + 2) {
    138             baos.write((HEXSTRING.indexOf(md5str.charAt(i)) << 4 | HEXSTRING.indexOf(md5str.charAt(i + 1))));
    139         }
    140         return new String(baos.toByteArray(), "ISO-8859-1");
    141     }
    142 
    143    
    144     /**
    145      * 获取加密后的密码
    146      * @param qq
    147      * @param password
    148      * @param verifycode
    149      * @return
    150      * @throws Exception
    151      */
    152     public static String getPassword(String qq, String password, String verifycode) throws Exception {
    153         String P = hexchar2bin(md5(password));
    154         String U = md5(P + hexchar2bin(qq.replace("\x", "").toUpperCase()));
    155         String V = md5(U + verifycode.toUpperCase());
    156         return V;
    157     }
    158 
    159 }
    160 
    161  微博登陆实现:
    162 
    163 package token.exe;
    164 
    165 import java.io.ByteArrayInputStream;
    166 import java.io.FileOutputStream;
    167 import java.io.IOException;
    168 import java.io.InputStream;
    169 import java.io.UnsupportedEncodingException;
    170 import java.net.URLEncoder;
    171 import java.security.KeyManagementException;
    172 import java.security.NoSuchAlgorithmException;
    173 import java.security.cert.CertificateException;
    174 import java.security.cert.X509Certificate;
    175 import java.util.ArrayList;
    176 import java.util.Date;
    177 import java.util.HashMap;
    178 import java.util.List;
    179 import java.util.Properties;
    180 import java.util.Scanner;
    181 
    182 import javax.net.ssl.SSLContext;
    183 import javax.net.ssl.TrustManager;
    184 import javax.net.ssl.X509TrustManager;
    185 
    186 import org.apache.http.Header;
    187 import org.apache.http.HttpHeaders;
    188 import org.apache.http.HttpHost;
    189 import org.apache.http.HttpResponse;
    190 import org.apache.http.HttpVersion;
    191 import org.apache.http.client.ClientProtocolException;
    192 import org.apache.http.client.methods.HttpGet;
    193 import org.apache.http.client.params.CookiePolicy;
    194 import org.apache.http.client.params.HttpClientParams;
    195 import org.apache.http.conn.params.ConnRoutePNames;
    196 import org.apache.http.conn.routing.HttpRoute;
    197 import org.apache.http.conn.scheme.PlainSocketFactory;
    198 import org.apache.http.conn.scheme.Scheme;
    199 import org.apache.http.conn.scheme.SchemeRegistry;
    200 import org.apache.http.conn.ssl.SSLSocketFactory;
    201 import org.apache.http.impl.client.DefaultHttpClient;
    202 import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
    203 import org.apache.http.message.BasicHeader;
    204 import org.apache.http.params.CoreConnectionPNames;
    205 import org.apache.http.params.HttpParams;
    206 import org.apache.http.params.HttpProtocolParams;
    207 import org.apache.http.params.SyncBasicHttpParams;
    208 import org.apache.http.util.EntityUtils;
    209 import org.jsoup.Jsoup;
    210 import org.jsoup.nodes.Document;
    211 import org.jsoup.nodes.Element;
    212 
    213 import token.TencentWeiboOAuth;
    214 import token.def.TLoginParams;
    215 
    216 import com.tencent.weibo.beans.RouteCfg;
    217 
    218 public class TencentWeiboLoginer {
    219     
    220     private DefaultHttpClient httpClient;
    221     
    222     //默认连接配置参数
    223     private static final int CONNECT_TIME_OUT = 5000;
    224     private static final int SOCKET_TIME_OUT = 5000;
    225     private static final int MAX_CONNECTIONS_PRE_HOST = 20;
    226     private static final int MAX_TOTAL_CONNECTIONS = 200;
    227     
    228     public TencentWeiboLoginer() {
    229         this(CONNECT_TIME_OUT, SOCKET_TIME_OUT, MAX_CONNECTIONS_PRE_HOST, MAX_TOTAL_CONNECTIONS, null, null);
    230     }
    231     
    232     public TencentWeiboLoginer(int connectTimeOut, int socketTimeOut, int maxConnectionsPreHost,
    233             int maxTotalConnections, List<RouteCfg> routeCfgs, HttpHost proxy) {
    234                 
    235         //注册ssl协议
    236         SSLContext ssl = null;
    237         SchemeRegistry schemeRegistry = null;
    238         X509TrustManager x509TrustManager = null;
    239         SSLSocketFactory sslSocketFactory = null;
    240         try {
    241             ssl = SSLContext.getInstance("TLS");
    242             x509TrustManager = new X509TrustManager() {
    243                 
    244                 @Override
    245                 public X509Certificate[] getAcceptedIssuers() {
    246                     // TODO Auto-generated method stub
    247                     return null;
    248                 }
    249                 
    250                 @Override
    251                 public void checkServerTrusted(X509Certificate[] chain, String authType)
    252                         throws CertificateException {
    253                     // TODO Auto-generated method stub
    254                     
    255                 }
    256                 
    257                 @Override
    258                 public void checkClientTrusted(X509Certificate[] chain, String authType)
    259                         throws CertificateException {
    260                     // TODO Auto-generated method stub
    261                     
    262                 }
    263             };
    264             ssl.init(null, new TrustManager[]{x509TrustManager}, null);
    265             sslSocketFactory = new SSLSocketFactory(ssl);            
    266             sslSocketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    267             
    268             //注册http和https协议
    269             schemeRegistry = new SchemeRegistry();
    270             schemeRegistry.register(new Scheme("https", 443, sslSocketFactory));
    271 //            schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
    272         } catch (NoSuchAlgorithmException e) {
    273             // TODO Auto-generated catch block
    274             e.printStackTrace();
    275         } catch (KeyManagementException e) {
    276             // TODO Auto-generated catch block
    277             e.printStackTrace();
    278         }
    279         
    280         //配置客户端链接管理类
    281         ThreadSafeClientConnManager connManager = new ThreadSafeClientConnManager(schemeRegistry);
    282         connManager.setDefaultMaxPerRoute(maxConnectionsPreHost);
    283         connManager.setMaxTotal(maxTotalConnections);
    284         
    285         //配置http请求连接参数
    286         HttpParams httpParams = new SyncBasicHttpParams();
    287         httpParams.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, connectTimeOut);
    288         httpParams.setParameter(CoreConnectionPNames.SO_TIMEOUT, socketTimeOut);
    289         
    290         //http协议参数配置
    291         HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);
    292         HttpProtocolParams.setUseExpectContinue(httpParams, false);
    293         
    294         //启用cookie
    295         HttpClientParams.setCookiePolicy(httpParams, CookiePolicy.BROWSER_COMPATIBILITY);
    296         
    297         //对特定ip端口修改最大连接数
    298         if (routeCfgs != null) {
    299             for (RouteCfg routeCfg : routeCfgs) {
    300                 HttpHost host = new HttpHost(routeCfg.getHost(), routeCfg.getPort());
    301                 connManager.setMaxForRoute(new HttpRoute(host), routeCfg.getMaxConnetions());
    302             }
    303         }
    304         
    305         //初始化httpClient
    306         httpClient = new DefaultHttpClient(connManager,httpParams);
    307         
    308         //添加headers
    309         List<Header> headers = new ArrayList<Header>();
    310         headers.add(new BasicHeader(HttpHeaders.ACCEPT, "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"));
    311         headers.add(new BasicHeader(HttpHeaders.ACCEPT_LANGUAGE, "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3"));
    312         headers.add(new BasicHeader(HttpHeaders.ACCEPT_CHARSET, "UTF-8"));
    313         headers.add(new BasicHeader(HttpHeaders.USER_AGENT, "Mozilla/5.0 (Windows NT 5.1; rv:25.0) Gecko/20100101 Firefox/25.0"));
    314         headers.add(new BasicHeader(HttpHeaders.CONNECTION, "keep-alive"));
    315         headers.add(new BasicHeader("X-Forwarded-For", "192.168.0.1"));
    316         headers.add(new BasicHeader("Client-IP", "192.168.0.1"));
    317         headers.add(new BasicHeader("API-RemoteIP", "192.168.0.1"));
    318         httpClient.getParams().setParameter("http.default-headers", headers);
    319         
    320         //设置代理
    321         if (proxy != null) {
    322             httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
    323         }
    324                 
    325     }
    326     
    327     /**
    328      * 模拟腾讯微博登陆
    329      * @return code值
    330      */
    331     public TLoginParams doLogin(String username, String password) {
    332         
    333         Properties properties = initProperties();
    334         String clientID = properties.getProperty("client_id");
    335         String redirectURI = properties.getProperty("redirect_uri");
    336         
    337         HashMap<String, String> urlMap = getUrlMap(clientID, redirectURI);
    338         String dataRedirect = urlMap.get("data-redirect");
    339         
    340         HashMap<String, String> loginInfoMap = preLogin(urlMap);
    341         String loginSig = loginInfoMap.get("login_sig");
    342         String loginUrl = loginInfoMap.get("login_url");
    343         
    344         HashMap<String, String> checkMap = isHasVC(dataRedirect, username, loginSig, loginUrl);
    345         String isHasVC = checkMap.get("isHasVC");
    346         String vc = checkMap.get("vc");
    347         String saltUin = checkMap.get("saltUin");
    348         
    349         TLoginParams tLoginParams = new TLoginParams();
    350         if (Integer.parseInt(isHasVC) != 0) {
    351             tLoginParams.setDataRedirect(dataRedirect);
    352             tLoginParams.setLoginSig(loginSig);
    353             tLoginParams.setLoginUrl(loginUrl);
    354             tLoginParams.setSaltUin(saltUin);
    355             tLoginParams.setImgURl(getVCode(username));
    356             return tLoginParams;
    357         }
    358         
    359         String checkSigUrl = finalLogin(vc, saltUin, dataRedirect, username, 
    360                 password, loginSig, loginUrl);
    361         Properties result = authorize(loginUrl, checkSigUrl);
    362         tLoginParams.setProp(result);
    363         return tLoginParams;
    364     }
    365     
    366     /**
    367      * 有验证码时验证登陆
    368      * @param vc
    369      * @param saltUin
    370      * @param dataRedirect
    371      * @param username
    372      * @param password
    373      * @param loginSig
    374      * @param loginUrl
    375      * @return
    376      */
    377     public TLoginParams doLoginByVC(String vc, String saltUin, String dataRedirect, String username, 
    378             String password, String loginSig, String loginUrl) {
    379         
    380         TLoginParams tLoginParams = new TLoginParams();
    381         
    382         String checkSigUrl = finalLogin(vc, saltUin, dataRedirect, username, password, loginSig, loginUrl);
    383         if (checkSigUrl.equals("您输入的验证码不正确,请重新输入。")) {
    384             tLoginParams.setLogin(false);
    385             return tLoginParams;
    386         }
    387         Properties prop = authorize(loginUrl, checkSigUrl);
    388         
    389         tLoginParams.setProp(prop);
    390         return tLoginParams;
    391     }
    392     
    393     /**
    394      * 初始化登陆,获取含有sessionkey的url提交链接
    395      * @param clientID 应用ID
    396      * @param redirectURI 应用回调地址
    397      * @return
    398      */
    399     private HashMap<String, String> getUrlMap(String clientID, String redirectURI) {
    400         
    401         String url = "https://open.t.qq.com/cgi-bin/oauth2/authorize?" 
    402                 + "client_id=" + clientID
    403                 + "&response_type=code"
    404                 + "&redirect_uri=" + redirectURI
    405                 + "&forcelogin=true";
    406         Header[] headers = new BasicHeader[]{
    407                 new BasicHeader(HttpHeaders.HOST, "open.t.qq.com")
    408         };
    409 
    410         String htmlDatas = httpGetDatas(url, headers);
    411         HashMap<String, String> map = new HashMap<String, String>();
    412         String data_redirect = null;
    413         String data_proxy = null;
    414         
    415         Document  document = Jsoup.parse(htmlDatas);
    416         Element element = document.getElementsByTag("noscript").first();
    417         data_redirect = element.attr("data-redirect");
    418         map.put("data-redirect", data_redirect);
    419         data_proxy = element.attr("data-proxy");
    420         map.put("data-proxy", data_proxy);
    421         return map;
    422     }
    423     
    424     /**
    425      * 预登陆腾讯微博,获取login_sig
    426      * @param urlMap 初始化登陆返回的urlMap
    427      * @return
    428      */
    429     private HashMap<String, String> preLogin(HashMap<String, String> urlMap) {
    430         
    431         String s_url_encode = null;
    432         String proxy_url_encode = null;
    433         String script = null;
    434         try {
    435             s_url_encode = URLEncoder.encode(urlMap.get("data-redirect"), "UTF-8");
    436             proxy_url_encode = URLEncoder.encode(urlMap.get("data-proxy"), "UTF-8");
    437         } catch (UnsupportedEncodingException e) {
    438             // TODO Auto-generated catch block
    439             e.printStackTrace();
    440         }
    441         String url = "https://ui.ptlogin2.qq.com/cgi-bin/login?appid=46000101"
    442                 + "&s_url=" + s_url_encode
    443                 + "&proxy_url=" + proxy_url_encode
    444                 + "&f_url=loginerroralert"
    445                 + "&style=13" 
    446                 + "&daid=6"
    447                 + "&pt_no_auth=1"
    448                 + "&hide_close_icon=1" 
    449                 + "&link_target=blank"
    450                 + "&target=blank"
    451                 + "&hide_title_bar=1"
    452                 + "&no_drop_domain=1"
    453                 + "&dummy=1"
    454                 + "&bgcolor=ffffff"
    455                 + "&r=" + Math.random();
    456         Header[] headers = new BasicHeader[]{
    457                 new BasicHeader(HttpHeaders.HOST, "ui.ptlogin2.qq.com")
    458         };
    459         String htmlDatas = httpGetDatas(url, headers);
    460         
    461         Document document = Jsoup.parse(htmlDatas);
    462         Element headElement = document.getElementsByTag("head").first();
    463         Element element = headElement.getElementsByTag("script").first();
    464         script = element.html();
    465         
    466         String login_sig = script.substring(script.indexOf("login_sig:"), script.indexOf("",clientip"));
    467         String login_sig_key = login_sig.substring(login_sig.indexOf(""") + 1);
    468         
    469         HashMap<String, String>    loginMap = new HashMap<String, String>();
    470         loginMap.put("login_sig", login_sig_key);
    471         loginMap.put("login_url", url);
    472         return loginMap;
    473     }    
    474     
    475     /**
    476      * 检查预登陆时是否需要验证码
    477      * @param dataRedirect 初始化登陆返回的map
    478      * @param username 用户名
    479      * @param loginSig TODO
    480      * @param loginUrl TODO
    481      * @return
    482      */
    483     private HashMap<String, String> isHasVC(String dataRedirect, String username, 
    484             String loginSig, String loginUrl){
    485         
    486         String url = null;
    487         try {
    488             url = "https://ssl.ptlogin2.qq.com/check?"
    489                     + "regmaster="
    490                     + "&uin=" + username
    491                     + "&appid=46000101"
    492                     + "&js_ver=10052"
    493                     + "&js_type=1"
    494                     + "&login_sig=" + loginSig
    495                     + "&u1=" + URLEncoder.encode(dataRedirect, "UTF-8")
    496                     + "&r=" + Math.random();
    497         } catch (UnsupportedEncodingException e) {
    498             // TODO Auto-generated catch block
    499             e.printStackTrace();
    500         }
    501         Header[] headers = new BasicHeader[]{
    502                 new BasicHeader(HttpHeaders.REFERER, loginUrl)
    503         };
    504         
    505         String htmlDatas = httpGetDatas(url, headers);
    506                 
    507         String str = htmlDatas.substring(htmlDatas.indexOf("(") + 1, htmlDatas.indexOf(");"));
    508         String[] strs = str.split(",");
    509         
    510         String isHasVC = strs[0].substring(strs[0].indexOf("'") + 1, strs[0].lastIndexOf("'"));
    511         HashMap<String,String> checkVCMap = new HashMap<String, String>();
    512         checkVCMap.put("isHasVC", isHasVC);        
    513         String vc = strs[1].substring(strs[1].indexOf("'") + 1, strs[1].lastIndexOf("'"));
    514         checkVCMap.put("vc", vc);        
    515         String saltUin = strs[2].substring(strs[2].indexOf("'") + 1, strs[2].lastIndexOf("'"));
    516         checkVCMap.put("saltUin", saltUin);
    517     
    518         return checkVCMap;
    519     }
    520     
    521     /**
    522      * 获取当前用户登陆所需要的验证码
    523      * @param username 用户名
    524      * @return
    525      */
    526     public String getVCode(String username) {
    527         
    528         String imageUrl = "https://ssl.captcha.qq.com/getimage?"
    529         + "uin=" +username
    530         + "&aid=46000101"
    531         + "&" + Math.random();
    532         
    533         return imageUrl;
    534     }
    535     
    536     /**
    537      * 保存验证码
    538      * @param url 验证码链接
    539      */
    540     public void saveVCodeImg(String url) {
    541         
    542         HttpGet getImages = new HttpGet(url);
    543         HttpResponse response = null;
    544         try {
    545             response = httpClient.execute(getImages);
    546             byte[] imageBytes = EntityUtils.toByteArray(response.getEntity());                
    547             FileOutputStream fileWrite = new FileOutputStream("vc.jpg");
    548             fileWrite.write(imageBytes);
    549             fileWrite.close();
    550         } catch (ClientProtocolException e) {
    551             // TODO Auto-generated catch block
    552             e.printStackTrace();
    553         } catch (IOException e) {
    554             // TODO Auto-generated catch block
    555             e.printStackTrace();
    556         }
    557     }
    558     
    559     /**
    560      * 模拟最终登陆
    561      * @param vc 验证码信息
    562      * @param dataRedirect 链接信息
    563      * @param username 用户名
    564      * @param password 密码
    565      * @param loginSig TODO
    566      * @param loginUrl TODO
    567      * @param saltUin TODO
    568      * @return
    569      */
    570     private String finalLogin(String vc, String saltUin, String dataRedirect, String username, 
    571             String password, String loginSig, String loginUrl){
    572         
    573         String p = null;
    574         try {
    575             p = TencentWeiboEncryption.getPassword(saltUin, password, vc);
    576         } catch (Exception e) {
    577             // TODO Auto-generated catch block
    578             e.printStackTrace();
    579         }
    580         String url = null;
    581         try {
    582             url = "https://ssl.ptlogin2.qq.com/login?"
    583                     + "u=" + URLEncoder.encode(username, "UTF-8")
    584                     + "&p=" + p
    585                     + "&verifycode=" + vc
    586                     + "&aid=46000101"
    587                     + "&u1=" + URLEncoder.encode(dataRedirect, "UTF-8")
    588                     + "&h=1"
    589                     + "&ptredirect=1"
    590                     + "&ptlang=2052"
    591                     + "&daid=6"
    592                     + "&from_ui=1"
    593                     + "&dumy="
    594                     + "&low_login_enable=0"
    595                     + "&regmaster="
    596                     + "&fp=loginerroralert"
    597                     + "&action=2-20-" + new Date().getTime()
    598                     + "&mibao_css="
    599                     + "&t=1"
    600                     + "&g=1"
    601                     + "&js_ver=10052"
    602                     + "&js_type=1"
    603                     + "&login_sig=" + loginSig
    604                     + "&pt_rsa=0";
    605         } catch (UnsupportedEncodingException e) {
    606             // TODO Auto-generated catch block
    607             e.printStackTrace();
    608         }
    609         
    610         Header[] headers = new BasicHeader[]{
    611             new BasicHeader(HttpHeaders.REFERER, loginUrl)
    612         };
    613         
    614         String htmlDatas = httpGetDatas(url, headers);
    615         String str = htmlDatas.substring(htmlDatas.indexOf("(") + 1, htmlDatas.indexOf(");"));
    616         String[] strs = str.split(",");
    617         
    618         String checkUrl = strs[2].substring(strs[2].indexOf("'") + 1, strs[2].lastIndexOf("'"));
    619         String loginResult = strs[4].substring(strs[4].indexOf("'") + 1, strs[4].lastIndexOf("'"));
    620         
    621         if (loginResult.equals("登录成功!")) {
    622             return checkUrl;
    623         }
    624         return loginResult;
    625     }
    626     
    627     
    628     /**
    629      * 获取最终授权
    630      * @param loginUrl
    631      * @param checkSigUrl
    632      * @return
    633      */
    634     private Properties authorize(String loginUrl, String checkSigUrl) {
    635         
    636         Properties prop = null;
    637         if (checkSigUrl != null) {
    638             Header[] headers = new BasicHeader[]{
    639                 new BasicHeader(HttpHeaders.REFERER, loginUrl)
    640             };
    641             String htmlDatas = httpGetDatas(checkSigUrl, headers);
    642             
    643             Document  document = Jsoup.parse(htmlDatas);
    644             Element element = document.getElementsByTag("meta").first();
    645             String content = element.attr("content");;
    646         
    647             String subContent = content.substring(content.indexOf("?") + 1);
    648             String propStr = subContent.replace("&", "
    ");
    649             
    650             prop = new Properties();
    651             InputStream stream = new ByteArrayInputStream(propStr.getBytes());
    652             try {
    653                 prop.load(stream);
    654             } catch (IOException e) {
    655                 // TODO Auto-generated catch block
    656                 e.printStackTrace();
    657             }
    658         }
    659         return prop;
    660     }
    661     
    662     
    663     /**
    664      * 提交URL,并获取页面数据(GET方式)
    665      * @param url 请求页面
    666      * @param headers http请求header
    667      * @return
    668      */
    669     private String httpGetDatas(String url,Header[] headers) {
    670         
    671         String response =null;
    672         HttpResponse httpResponse = null;
    673         if (url == null) {
    674             throw new NullPointerException("URL is null");
    675         }
    676         HttpGet httpGet = new HttpGet(url);
    677         httpGet.setHeaders(headers);
    678         
    679         try {
    680             httpResponse = httpClient.execute(httpGet);
    681             response = EntityUtils.toString(httpResponse.getEntity());
    682             
    683         } catch (ClientProtocolException e) {
    684             // TODO Auto-generated catch block
    685             e.printStackTrace();
    686         } catch (IOException e) {
    687             // TODO Auto-generated catch block
    688             e.printStackTrace();
    689         }
    690         return response;
    691     }
    692     
    693     /**
    694      * 初始化配置信息
    695      * @return
    696      */
    697     public Properties initProperties() {
    698         
    699         Properties properties = new Properties();
    700         InputStream inputStream = Thread.currentThread().
    701                 getContextClassLoader().getResourceAsStream("cfg.properties");
    702         try {
    703             properties.load(inputStream);
    704         } catch (IOException e) {
    705             // TODO Auto-generated catch block
    706             e.printStackTrace();
    707         }
    708         return properties;
    709     }
    710     
    711     public static void main(String[] args) {
    712 
    713         TencentWeiboLoginer loginer = new TencentWeiboLoginer();
    714         TLoginParams tLoginParams = loginer.doLogin("","");
    715             //有验证码时
    716         if (tLoginParams.getProp() == null) {
    717             String saltUin = tLoginParams.getSaltUin();
    718             String dataRedirect = tLoginParams.getDataRedirect();
    719             String loginSig = tLoginParams.getLoginSig();
    720             String loginUrl = tLoginParams.getLoginUrl();
    721             String imgUrl = tLoginParams.getImgURl();
    722             //要返回的验证码
    723             System.err.println(imgUrl);
    724             
    725             //测试再次获取验证码
    726             imgUrl = loginer.getVCode("");
    727             //保存验证码(用于测试并查看验证码)
    728             loginer.saveVCodeImg(imgUrl);
    729             
    730             Scanner input = new Scanner(System.in);
    731             String vc = input.nextLine();
    732             
    733             
    734             TLoginParams loginresult =loginer.doLoginByVC(vc, saltUin, dataRedirect, "", 
    735                     "", loginSig, loginUrl);
    736                 //如果验证码录入错误,则重新获取并返回验证码
    737             if (!loginresult.isLogin()) {
    738                 System.err.println("验证码错误!重新录入");
    739                 imgUrl = loginer.getVCode("");
    740                 loginer.saveVCodeImg(imgUrl);
    741                 Scanner input2 = new Scanner(System.in);
    742                 String vc1 = input2.nextLine();
    743                 Properties codeProp = loginer.doLoginByVC(vc1, saltUin, dataRedirect, "", 
    744                         "", loginSig, loginUrl).getProp();
    745                 System.out.println(TencentWeiboOAuth.getOAuthV2Instance(codeProp));
    746             }else {
    747                 //验证码正确则直接输出结果
    748                 Properties codeProp = loginresult.getProp();
    749                 System.out.println(TencentWeiboOAuth.getOAuthV2Instance(codeProp));
    750             }
    751             
    752         }else {
    753             //无验证码时
    754             Properties codeProp = tLoginParams.getProp();
    755             System.out.println(TencentWeiboOAuth.getOAuthV2Instance(codeProp));
    756         }
    757     }
    758 }
    759 
    760  
    761 
    762 上述代码完整模拟了腾讯微博的登陆过程,并最终获得授权
  • 相关阅读:
    一种神奇的双向循环链表C语言实现
    ucore_lab0
    使用展开操符作替代 .apply() (prefer-spread)
    使用剩余参数代替 arguments (prefer-rest-params)
    LeetCode 11. Container With Most Water 单调队列
    Codecademy网站安利 及 javaScript学习
    谈项目团队分工角色和改进
    谈大型项目开发的多个环境配合
    以api文档为中心--前后端分开发离新思维
    中国进出口商品交易会开发总结
  • 原文地址:https://www.cnblogs.com/zhengbing/p/3459249.html
Copyright © 2020-2023  润新知