阿里云服务器免费领卷啦。

捡代码论坛-最全的游戏源码下载技术网站!

 找回密码
 立 即 注 册

QQ登录

只需一步,快速开始

搜索
关于源码区的附件失效或欺骗帖, 处理办法
查看: 10497|回复: 0

完美解决QQ空间登陆的代码 使用技术:httpclient

[复制链接]

4208

主题

210

回帖

12万

积分

管理员

管理员

Rank: 9Rank: 9Rank: 9

积分
126189
QQ
发表于 2016-8-26 16:40:10 | 显示全部楼层 |阅读模式
完美解决QQ空间登陆的代码 使用技术:httpclient

在很久之前,试着抓取一些用户的喜好,来着卖钱,穷。于是从qq空间开刀,研究qq空间的登陆,当然部分qq空间的加密技术是源于网络老司机的带路,也得到了一位网友的帮助。对于新入手的小白们,我这里就不很详细的讲解网络请求这些基础了。

首先看看我对加密的处理(此处有部分加密的解决办法是网友提供)

  1. package ly.entity;

  2. import java.math.BigInteger;
  3. import java.net.URLEncoder;
  4. import java.security.MessageDigest;
  5. import java.security.NoSuchAlgorithmException;
  6. import java.security.interfaces.RSAPrivateKey;
  7. import java.security.interfaces.RSAPublicKey;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. import java.util.Random;
  11. import java.util.Scanner;

  12. import org.apache.commons.codec.binary.Base64;

  13. import ly.util.HttpUtil;
  14. import ly.util.RegexUtil;
  15. import ly.util.Security;
  16. import ly.util.Util;

  17. public class QQ extends HttpService{
  18.     /** QQ帐号 **/
  19.     private String uin;
  20.     /** QQ密码 **/
  21.     private String password;
  22. //  /** QQ-ID **/
  23. //  private String aid;
  24.     /** 登录验证字符串 **/
  25.     private String login_sig;
  26.     /** 登录状态 0=登录成功,否则失败**/
  27.     private int status;
  28.     /** 验证码 **/
  29.     private String verify;
  30.     /** 随机数 **/
  31.     private String random;
  32.     /** QQ空间请求的G_TK**/
  33.     protected String g_tk;

  34.     //public QQ(){} 外部不能调用默认构造函数,除非显示的声明。
  35.     public String submitResponse="";
  36.     public QQ(String uin,String password){
  37.         this.uin = uin;
  38.         this.password = password;
  39.     }

  40.     private String requestLoginPage(){
  41.         String request="http://xui.ptlogin2.qq.com/cgi-bin/xlogin?";
  42.         request+="proxy_url=http%3A//qzs.qq.com/qzone/v6/portal/proxy.html&";
  43.         request+="daid=5&&";
  44.         request+="hide_title_bar=1&";
  45.         request+="low_login=0&";
  46.         request+="qlogin_auto_login=1&";
  47.         request+="no_verifyimg=1&";
  48.         request+="link_target=blank&";
  49.         request+="appid=549000912&";
  50.         request+="style=22&";
  51.         request+="target=self&";
  52.         request+="s_url=http%3A%2F%2Fqzs.qq.com%2Fqzone%2Fv5%2Floginsucc.html%3Fpara%3Dizone&";
  53.         request+="pt_qr_app=%E6%89%8B%E6%9C%BAQQ%E7%A9%BA%E9%97%B4&";
  54.         request+="pt_qr_link=http%3A//z.qzone.com/download.html&";
  55.         request+="self_regurl=http%3A//qzs.qq.com/qzone/v6/reg/index.html&";
  56.         request+="pt_qr_help_link=http%3A//z.qzone.com/download.html";
  57.         String response = this.requestGet(request);
  58.         return response;
  59.     }
  60.     private String ptui_identifier="";
  61.     private String appid="";
  62.     private String ptui_version="";
  63.     private String s_url="";
  64.     private String lang="";
  65.     private String daid="";
  66.     private String pt_vcode_v1="";
  67.     //cookie data
  68.     private String pt_login_sig="";
  69.     private void getValuesFromLoginPageResponse(String response){
  70.         //1.
  71.         ptui_identifier=RegexUtil.replaceStartEnd(response, "c_login_2.js?", "",");
  72.         ptui_identifier=RegexUtil.replaceString(ptui_identifier, "ptui_identifier=");
  73.         System.out.println("ptui_identifier:");
  74.         System.out.println(ptui_identifier);
  75.         //2.
  76.         appid=RegexUtil.replaceStartEnd(response, "appid:", "lang");
  77.         appid = RegexUtil.replaceStartEnd(appid, """, """);
  78.         //3.
  79.         ptui_version=RegexUtil.replaceStartEnd(response, "ptui_version:", "isHttps");
  80.         ptui_version = RegexUtil.replaceStartEnd(ptui_version, """, """);
  81.         //4.
  82.         s_url=RegexUtil.replaceStartEnd(response, "s_url:", "proxy_url");
  83.         s_url = RegexUtil.replaceStartEnd(s_url, """, """);
  84.         //s_url=s_url.replaceAll("\\x", "%");   //将/x转换为%
  85.         s_url=s_url__change();
  86.         //5.
  87.         lang=RegexUtil.replaceStartEnd(response, "lang:", "style");
  88.         lang = RegexUtil.replaceStartEnd(lang, """, """);
  89.         //6.
  90.         daid=RegexUtil.replaceStartEnd(response, "daid:", "regmaster");
  91.         daid = RegexUtil.replaceStartEnd(daid, """, """);
  92.         //7
  93.         pt_vcode_v1=RegexUtil.replaceStartEnd(response, "pt_vcode_v1:", "pt_ver_md5");
  94.         pt_vcode_v1 = RegexUtil.replaceStartEnd(pt_vcode_v1, """, """);

  95.         //cookies
  96.         String cookie=this.getCookie();
  97.         System.out.println("getValuesFromLoginPageResponse[cookie]:");
  98.         System.out.println(cookie);
  99.         //[1]
  100.         pt_login_sig=RegexUtil.replaceStartEnd(cookie, "pt_login_sig=", ";");
  101.     }
  102.     private String requsetJSFile(){
  103.         String requset="http://imgcache.qq.com/ptlogin/ver/10157/js/c_login_2.js?";
  104.         requset+="max_age=604800&";
  105.         requset+="ptui_identifier="+ptui_identifier;
  106.         String response = this.requestGet(requset);
  107.         return response;

  108.     }
  109.     private String rsa_e;
  110.     private void getValuesFromJSResponse(String response){
  111.         //1.rsa_e
  112.         rsa_e=RegexUtil.replaceStartEnd(response, "function ot", "}");
  113.         rsa_e=RegexUtil.replaceStartEnd(rsa_e, "e="", """);
  114.         //2.
  115.     }
  116.     private String requestCheckFile(){
  117.         String request="http://check.ptlogin2.qq.com/check?";
  118.         request+="regmaster=&";
  119.         request+="pt_tea=2&";
  120.         request+="pt_vcode="+pt_vcode_v1+"&";
  121.         request+="uin="+uin+"&";
  122.         request+="appid="+appid+"&";
  123.         request+="js_ver"+ptui_version+"&";
  124.         request+="js_type=1&";
  125.         request+="login_sig="+pt_login_sig+"&";
  126.         request+="u1="+s_url+"&";
  127.         request+="r=0.3326206956990063&";//随机数
  128.         request+="pt_uistyle=32";
  129.         System.out.println("requestCheckFile requset");
  130.         System.out.println(request);
  131.         String response = this.requestGet(request);
  132.         return response;
  133.     }
  134.     String verifyCode="";//校验码
  135.     String check_arg1="";
  136.     String check_arg2="";
  137.     String check_arg3="";
  138.     String check_arg4="";
  139.     String check_arg5="";
  140.     String ptvfsession="";
  141.     private void getValuesFromCheckResponse(String response){
  142.         //1.第一个参数
  143.         int  i1=response.indexOf("'");
  144.         int  i2=response.indexOf("'",i1+1);
  145.         check_arg1=response.substring(i1+1,i2);
  146.         //2.第二个参数
  147.         i1=response.indexOf("'",i2+1);
  148.         i2=response.indexOf("'",i1+1);
  149.         check_arg2=response.substring(i1+1,i2);
  150.         verifyCode=check_arg2;
  151.         //3.第三个参数
  152.         i1=response.indexOf("'",i2+1);
  153.         i2=response.indexOf("'",i1+1);
  154.         check_arg3=response.substring(i1+1,i2);
  155.         //4.第四个参数
  156.         i1=response.indexOf("'",i2+1);
  157.         i2=response.indexOf("'",i1+1);
  158.         check_arg4=response.substring(i1+1,i2);
  159.         //4.第5个参数
  160.         i1=response.indexOf("'",i2+1);
  161.         i2=response.indexOf("'",i1+1);
  162.         check_arg5=response.substring(i1+1,i2);

  163.         //cookie
  164.         String cookie=this.cookie;
  165.         //[1]
  166.         ptvfsession=RegexUtil.replaceStartEnd(cookie, "ptvfsession=", ";");
  167.     }
  168.     private void requestSubmitPage_setCookie(){//设置cookie
  169.         //一个String的例子
  170.         //pt_local_token=-1902107632; PATH=/; DOMAIN=ptlogin2.qq.com;

  171.         List<String> lists=new ArrayList<String>();
  172.         //1.
  173.         String ptui_loginuin="ptui_loginuin="+uin+"; PATH=/; DOMAIN=ptlogin2.qq.com;";
  174.         lists.add(ptui_loginuin);
  175.         cookie = HttpUtil.mergeCookies(cookie, lists);
  176.     }
  177.     private void requestSubmitPage_in_print(){
  178.         System.out.println("-------requestSubmitPage_in_print------");
  179.         System.out.println("cookie:"+cookie);
  180.     }
  181.     private String requestSubmitPage(){
  182.         String request="http://ptlogin2.qq.com/login?";
  183.         request+="u="+uin+"&";
  184.         request+="verifycode="+verifyCode+"&";
  185.         request+="pt_vcode_v1=0&";
  186.         //request+="pt_verifysession_v1="+check_arg4+"&";
  187.         request+="pt_verifysession_v1="+ptvfsession+"&";
  188.         request+="p="+encryptedPassword+"&";
  189.         request+="pt_randsalt="+check_arg5+"&";
  190.         request+="u1="+s_url+"&";
  191.         request+="ptredirect=0&";
  192.         request+="h=1&";
  193.         request+="t=1&";
  194.         request+="g=1&";
  195.         request+="from_ui=1&";
  196.         request+="ptlang="+lang+"&";
  197.         request+="action=1-28-"+MyDate.getMS_19700101()+"&";
  198.         request+="js_ver="+ptui_version+"&";
  199.         request+="js_type=1&";
  200.         request+="login_sig="+pt_login_sig+"&";
  201.         request+="pt_uistyle=32&";
  202.         request+="aid="+appid+"&";
  203.         request+="daid="+daid;
  204.         System.out.println("request:"+request);
  205.         String response = this.requestGet(request);
  206.         return response;
  207.     }
  208.     public void getValueFromVerifyResponse(){
  209.         if(check_arg2.length()>5){
  210.             //cookie
  211.             String cookie=this.getCookie();
  212.             System.out.println("[getValueFromVerifyResponse] cookie:");
  213.             System.out.println(cookie);
  214.             //[1]
  215.             ptvfsession=RegexUtil.replaceStartEnd(cookie, "verifysession=", ";");
  216.         }

  217.     }
  218.     public void requestVerifyCode(){//验证码处理
  219.         if(check_arg2.length()>5){
  220.             String request="http://captcha.qq.com/getimage?";
  221.             request+="uin="+uin+"&";//用户名
  222.             request+="aid="+appid+"&";//appid
  223.             request+="cap_cd="+check_arg2+"&";//check的第二个参数
  224.             request+="0.7347680128262537";//随机数,这里写死
  225.             this.requestDownload(request,Util.root+"\\verifyTemp\\verify.jpg");//将文件下载到本地
  226.             System.out.println("校验码文件的地址为:");
  227.             System.out.println(Util.root+"\\verifyTemp\\verify.jpg");
  228.             System.out.println("请手动输入校验码 :");
  229.             Scanner scanner = new Scanner(System.in);
  230.             verifyCode=scanner.nextLine();
  231.         }

  232. //      //如果有验证码,一定要在密码加密前搞定哟
  233. //      if($verify.length()>5){
  234. //          System.out.println("请进入"+Util.root+"\\verifyTemp\\verify.jpg"+"查看验证码,且在控制台输入验证码→回车");
  235. //          $url = "http://captcha.qq.com/getimage?uin={0}&aid={1}&{2}";
  236. //          $url = Util.fillString($url, $uin, $aid, new Random().nextDouble()+"");
  237. //          this.requestDownload($url, Util.root+"\\verifyTemp\\verify.jpg");
  238. //          //不解释,控制台输入验证码,突然感觉自己好水。。。话又说回来,如果使用B/S架构,这里还要重构。。额,好吧。。。
  239. //          Scanner scanner = new Scanner(System.in);
  240. //          $verify = scanner.nextLine();
  241. //        }
  242.     }
  243.     private String encryptedPassword="";
  244.     public int login(){
  245.         //1.请求登录页面,并解析数据
  246.         String loginPage_response=requestLoginPage();
  247.         //System.out.println("login response:");
  248.         //System.out.println(loginPage_response);
  249.         getValuesFromLoginPageResponse(loginPage_response);
  250.         //2.请求JS文件,并解析数据
  251.         String js_reponse=requsetJSFile();
  252.         //System.out.println("请求js  的响应:");
  253.         //System.out.println(js_reponse);
  254.         getValuesFromJSResponse(js_reponse);
  255.         //3.请求check文件,并解析数据
  256.         String check_response=requestCheckFile();
  257.         System.out.println("-----------check_response:");
  258.         System.out.println(check_response);
  259.         getValuesFromCheckResponse(check_response);
  260.         //4.解析校验码
  261.         requestVerifyCode();
  262.         getValueFromVerifyResponse();
  263.         //simulation();//为了debug,进行仿真。
  264.         //5.加密密码
  265.         encodePassword_in_print();
  266.         encryptedPassword=encodePassword(password,check_arg3,verifyCode,false);
  267.         System.out.println("最终的p:");
  268.         System.out.println(encryptedPassword);
  269.         //6.提交登录
  270.         requestSubmitPage_setCookie();
  271.         requestSubmitPage_in_print();
  272.         this.submitResponse=requestSubmitPage();
  273.         System.out.println("-----------submit  response:");
  274.         System.out.println(this.submitResponse);
  275.         return this.status;
  276.     }


  277.     private void simulation(){
  278.         password="密码";//password
  279.         check_arg3="\\x00\\x00\\x00\\x00\\x2b\\x5b\\xa7\\x41";//salt
  280.         verifyCode="!MDY";//verifycode
  281.         rsa_e="9d270ada059af43fe7ba65753b56a5ec3ad564f5e7430471fd6bd80239417a7535d924b4457fc6efbe6c4a34aae741b2375c79e7213c52365b38821801ea9bd68c9ce49ad5033f51c86db51eaa398ba20176fe42bf3850106327a73537c3449e1665f83ac38ef20141bbe153d0c1ad08d936f1508274638b929479a84aae51c9bf6743b2716e27b8b916039c60bcfdde07ac529b0b35349e31f32de3f7eec8f5122f6a100e27744eacea269e4d65826324285f69dd78cb05b2cdd656c7c5ff20f882c7add04fbbc75bca7baeeae8a45d0d3eba18f487d1921437a584e54a4a59dcdcb8e78a2df777f8ec98cc0ca48a71c1b183d01c325a346dfca27b27bf77f9";
  282.         rsa_private_e="6397266d5c3802a9acf496ef60175d370361d0857575271006059df26ace70086f83989b15a01036467ae1dbd6a28f239da9e126f40d021ca85a7b37339fb5fa9549a825e6f013e4841be237f8b05728e7bcbd0d17995c43c6528ec84efa5435cb03fbf7618ee9169acdb1d227ed54f6d290db04a66793de139a3c2ec6850a16cbd70b350dfafd5cd4b3d027e6e5344a9f8f25d35e5673e2348d0dbaa5f708fd5a293fd44d3bf6442bafe6fdfae1133d2c2e38a117535faa01a3191fb782301d812cc090ca55f27ac5599c2384657ce42ece9f961f376d2daf6ddee2ebd5ffa2dd691a4381b6d878547a18be5ef4a02442dd9c972dd6b22d2b0e30643b4d2ec1";
  283.         hexRSAToDec="1a8677630794495cff8028dc5485c87a5419c428cd1e304f21b6a12cbad3ba5b83eebc7cbfa821cc854317979c06fbc702f9bd047c782d569c3d66d3a9df8eabb456d4b5bb21b6d09e27acc1f7c9642d71be998afc7d40d4df9cebe8f799142915e450323c88e54b3f461bc9cebe3bc6febd3ab0af3a2b4ef69e6d015db1f0c83f8f5bb3a03a8e7e4939986aba6a71306ea391b4a1e9ba438768212ac0bd6ae42a3d948e8472814ff83a8ea69760e28f78d507a1b0929d8fdcb5d158c558a9d7940f02932b776258e5e5aa7363913bb2a3e5050525c8aba1eb86f4cee4d3f8d7650a8e0cb411534d58b7dc01377518a5133af515f0b189eb06694b8cda120778";
  284.     }
  285.     private void encodePassword_in_print(){
  286.         System.out.println("rsa_e:");
  287.         System.out.println(rsa_e);
  288.         System.out.println("password:"+password);
  289.         System.out.println("cookie:"+cookie);
  290.     }
  291.     String hexRSAToDec=null;//如果无效,则设置为null或""
  292.     private String rsa_private_e=null;//无效设置为null或""
  293.     public String encodePassword(String password,String salt,String verifycode,Boolean isSafe){
  294.         String password_md5_uppercase=MyMD5.MD5(password).toUpperCase();//o
  295.         byte[] password_md5_uppsercase_hex=StringUtil.hexString2Hex_byte(password_md5_uppercase);//p

  296.         byte[] salt_hex=StringUtil.jsHexString2Hex(salt);//将"\x50"--->0x50;java无法自动识别\x,js遇见\x的字符串,就认为为16进制

  297.         byte[] salt_hex__password_md5_uppsercase_hex=ArrayUtil.combine2bytes(password_md5_uppsercase_hex,salt_hex);//r


  298.         String key=MyMD5.MD5(salt_hex__password_md5_uppsercase_hex).toUpperCase();//r

  299.         String verifycode_uppercase_strToBytes=StringUtil.strToBytes(verifycode.toUpperCase());//s
  300.         String verifycode_uppercase_strToBytes__hexLength=StringUtil.getStringLength(verifycode_uppercase_strToBytes,4);//a
  301.         //String delete__salt=StringUtil.strToBytes(salt_hex);
  302.         String plainText=password_md5_uppercase+StringUtil.strToBytes(salt_hex)+verifycode_uppercase_strToBytes__hexLength+verifycode_uppercase_strToBytes;

  303.         //TEA加密
  304.         QQTEA qqTEA=new QQTEA();
  305.         qqTEA.initKEY(key);
  306.         qqTEA.encrypt(plainText);//16进制字符串格式  eg "a23ddf"
  307.         short[] tea_ciper=qqTEA.getCiperText();

  308.         MyRSA myRSA=new MyRSA(rsa_e,"10001",rsa_private_e);

  309.         byte[] tea_ciper_s2b=ArrayUtil.short2byte(tea_ciper);
  310.         System.out.println("RSA要加密的明文[16进制显示,0x01显示为"01"]:");
  311.         ArrayUtil.outputHex(tea_ciper_s2b);
  312.         //byte[] tea_ciper_s2b_byte=StringUtil.hexString2Hex_byte(tea_ciper_s2b);        
  313.         String rsa_ciper=myRSA.encryptByPublicKey(tea_ciper_s2b);
  314.         System.out.println("RSA加密后的密文:");
  315.         System.out.println(rsa_ciper);

  316. //=====================为了调试解密数据,如果外部指定要解密的数据,则解密外部数据,为了和js文件进行调试的手段
  317.         if(hexRSAToDec!=null&&! hexRSAToDec.trim().equals("")){
  318.             rsa_ciper=hexRSAToDec;
  319.         }
  320.         if(rsa_private_e!=null&&!rsa_private_e.trim().equals("")){
  321.             System.out.println("要解密的数据:");
  322.             System.out.println(rsa_ciper);
  323.             byte[] debugDecText=myRSA.decryptByPrivateKey(rsa_ciper);
  324.             System.out.println("debugDecText[解密后的数据,16进制显示,0x01显示为"01"]:");
  325.             ArrayUtil.outputHex(debugDecText);
  326.         }
  327. //=====================为了调试      
  328.         //BASE64加密
  329.         System.out.println("base64要加密的数据");
  330.         System.out.println(rsa_ciper);
  331.         byte[] rsa_ciper_byte=StringUtil.hexString2Hex_byte(rsa_ciper);
  332.         String mi_binary_base64=new String(Base64.encodeBase64(rsa_ciper_byte));
  333.         String mi_binary_base64_replace=mi_binary_base64.replaceAll("/", "-");
  334.         mi_binary_base64_replace=mi_binary_base64_replace.replaceAll("\\+", "*");
  335.         mi_binary_base64_replace=mi_binary_base64_replace.replaceAll("=", "_");
  336.         return mi_binary_base64_replace;

  337.     }

  338.     /**
  339.      * 通过说说ID
  340.      * 赞一个说说
  341.      *
  342.      * 1.需要登录状态 g_tk
  343.      * 2.发送1次http请求
  344.      *
  345.      * @param curkey 说说ID  example:7b6eb61aa5616f52da7c0b00
  346.      *
  347.      */
  348.     public void praise(String curkey){
  349.         if(this.status != 0) return;
  350.         String $url = "http://w.qzone.qq.com/cgi-bin/likes/internal_dolike_app?g_tk="+this.g_tk;
  351.         String $data = "fid={0}&opuin={1}&abstime=1383191804&active=0&appid=311&curkey=http://user.qzone.qq.com/448163451/mood/{0}&qzreferrer=http://user.qzone.qq.com/{1}&typeid=0&unikey=http://user.qzone.qq.com/448163451/mood/{0}";

  352.         $data = Util.fillString($data, curkey, this.getUin());
  353.         this.requestPost($url, $data);
  354.     }

  355.     public String getUin() {
  356.         return uin;
  357.     }

  358.     public void setUin(String uin) {
  359.         this.uin = uin;
  360.     }


  361.     public String getPassword() {
  362.         return password;
  363.     }

  364.     public void setPassword(String password) {
  365.         this.password = password;
  366.     }

  367. //  public String getAid() {
  368. //      return aid;
  369. //  }
  370. //
  371. //  public void setAid(String aid) {
  372. //      this.aid = aid;
  373. //  }

  374.     public String getLogin_sig() {
  375.         return login_sig;
  376.     }

  377.     public void setLogin_sig(String login_sig) {
  378.         this.login_sig = login_sig;
  379.     }

  380.     public int getStatus() {
  381.         return status;
  382.     }

  383.     public void setStatus(int status) {
  384.         this.status = status;
  385.     }

  386.     public String getVerify() {
  387.         return verify;
  388.     }

  389.     public void setVerify(String verify) {
  390.         this.verify = verify;
  391.     }

  392.     public String getRandom() {
  393.         return random;
  394.     }

  395.     public void setRandom(String random) {
  396.         this.random = random;
  397.     }

  398.     public String getCookie() {
  399.         return cookie;
  400.     }

  401.     public void setCookie(String cookie) {
  402.         this.cookie = cookie;
  403.     }

  404.     public String getG_tk() {
  405.         return g_tk;
  406.     }

  407.     public void setG_tk(String g_tk) {
  408.         this.g_tk = g_tk;
  409.     }
  410.     private String s_url__change(){//将\x 转换为 %
  411.         String temp="";
  412.         for(int i=0;i<s_url.length()-1;i++){
  413.             if((s_url.charAt(i)=='\\')&&(s_url.charAt(i+1)=='x')){
  414.                 temp+="%";
  415.                 i++;
  416.             }else{
  417.                 temp+=s_url.charAt(i);
  418.             }
  419.         }
  420.         return temp;
  421.     }

  422. }
复制代码


然后再来看对上面代码的使用:
  1. public  void login()
  2.     {
  3.         URI uri=null;
  4.         HttpTool tool=new HttpTool();
  5.         QQ qq = new QQ(this.qq,this.password);
  6.         qq.login();//登录
  7.         String cc=qq.submitResponse;
  8.         int start=cc.indexOf("http://");

  9.         cc=cc.substring(start);
  10.         int end=cc.indexOf(',')-1;
  11.         cc=cc.substring(0, end);
  12.         System.out.println("cc:"+cc);
  13.         try {
  14.             Thread.sleep(5000);
  15.         } catch (Exception e) {
  16.             e.printStackTrace();
  17.         }
  18.         this.geth=tool.httponline(qq.getCookie());  
  19.         this.skey=geth.getFirstHeader("skey").getValue();
  20.         this.g_tk=tool.GetG_TK(this.skey);
  21.          CloseableHttpClient httpclient = HttpClients.createDefault();
  22.          URI urc=null;
  23.         try {
  24.             urc = new URI(cc);
  25.         } catch (URISyntaxException e1) {
  26.             // TODO Auto-generated catch block
  27.             e1.printStackTrace();
  28.         }
  29.          this.geth.setURI(urc);
  30.          geth.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.93 Safari/537.36");

  31.          System.out.println("Executing request " + geth.getURI());//开始
  32.          String html="";
  33.          try {
  34.             CloseableHttpResponse response = httpclient.execute(geth);
  35.              System.out.println(response.getStatusLine());
  36.              HttpEntity entity = response.getEntity();  
  37.              html=EntityUtils.toString(entity, "utf-8");
  38.         } catch (ClientProtocolException e) {
  39.             e.printStackTrace();
  40.         } catch (IOException e) {
  41.             e.printStackTrace();
  42.         }
  43.          this.client=httpclient;
  44.          System.out.println(html);
  45.     }
复制代码
贴出整个登陆的代码:
  1. package zjt.client.http;

  2. import java.io.IOException;
  3. import java.net.URI;
  4. import java.net.URISyntaxException;

  5. import org.apache.http.HttpEntity;
  6. import org.apache.http.client.ClientProtocolException;
  7. import org.apache.http.client.methods.CloseableHttpResponse;
  8. import org.apache.http.client.methods.HttpGet;
  9. import org.apache.http.impl.client.CloseableHttpClient;
  10. import org.apache.http.impl.client.HttpClients;
  11. import org.apache.http.util.EntityUtils;
  12. import org.apache.log4j.Logger;

  13. import ly.entity.QQ;


  14. public class Main {
  15.     HttpGet geth=new HttpGet();
  16.     CloseableHttpClient client=null;
  17.     String qq="2651116759";
  18.     String password="xxxxxxx";
  19.     String skey="";
  20.     String g_tk="";
  21.     public  void login()
  22.     {
  23.         URI uri=null;
  24.         HttpTool tool=new HttpTool();
  25.         QQ qq = new QQ(this.qq,this.password);
  26.         qq.login();//登录
  27.         String cc=qq.submitResponse;
  28.         int start=cc.indexOf("http://");

  29.         cc=cc.substring(start);
  30.         int end=cc.indexOf(',')-1;
  31.         cc=cc.substring(0, end);
  32.         System.out.println("cc:"+cc);
  33.         try {
  34.             Thread.sleep(5000);
  35.         } catch (Exception e) {
  36.             e.printStackTrace();
  37.         }
  38.         this.geth=tool.httponline(qq.getCookie());  
  39.         this.skey=geth.getFirstHeader("skey").getValue();
  40.         this.g_tk=tool.GetG_TK(this.skey);
  41.          CloseableHttpClient httpclient = HttpClients.createDefault();
  42.          URI urc=null;
  43.         try {
  44.             urc = new URI(cc);
  45.         } catch (URISyntaxException e1) {
  46.             // TODO Auto-generated catch block
  47.             e1.printStackTrace();
  48.         }
  49.          this.geth.setURI(urc);
  50.          geth.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.93 Safari/537.36");

  51.          System.out.println("Executing request " + geth.getURI());//开始
  52.          String html="";
  53.          try {
  54.             CloseableHttpResponse response = httpclient.execute(geth);
  55.              System.out.println(response.getStatusLine());
  56.              HttpEntity entity = response.getEntity();  
  57.              html=EntityUtils.toString(entity, "utf-8");
  58.         } catch (ClientProtocolException e) {
  59.             e.printStackTrace();
  60.         } catch (IOException e) {
  61.             e.printStackTrace();
  62.         }
  63.          this.client=httpclient;
  64.          System.out.println(html);
  65.     }
  66.     /**
  67.      * 用户主页
  68.      * @param httpclient
  69.      * @return
  70.      */
  71.     public CloseableHttpClient userPage(CloseableHttpClient httpclient,String userqq)
  72.     {
  73.         URI uri=null;
  74.         try {
  75.             uri = new URI("http://user.qzone.qq.com/"+userqq);
  76.         } catch (URISyntaxException e1) {
  77.             // TODO Auto-generated catch block
  78.             e1.printStackTrace();
  79.         }
  80.         geth.setURI(uri);
  81.         geth.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.93 Safari/537.36");
  82.         // System.out.println("Executing request " + geth.getURI());//开始
  83.          String html="";
  84.          try {
  85.             CloseableHttpResponse response = httpclient.execute(geth);
  86.             // System.out.println(response.getStatusLine());
  87.              HttpEntity entity = response.getEntity();  
  88.              html=EntityUtils.toString(entity, "utf-8");
  89.         } catch (ClientProtocolException e) {
  90.             e.printStackTrace();
  91.         } catch (IOException e) {
  92.             e.printStackTrace();
  93.         }
  94.        // System.out.println(html);
  95.         return httpclient;
  96.     }
  97.     /**
  98.      * 获取用户基本信息的接口
  99.      * @param httpclient
  100.      * @param userstr
  101.      * @return
  102.      */
  103.     public String getUserInfo(CloseableHttpClient httpclient,String userstr)
  104.     {
  105.         URI uri=null;
  106.         try {
  107.             uri = new URI("http://base.s21.qzone.qq.com/cgi-bin/user/cgi_userinfo_get_all?uin="+userstr+"&vuin="+this.qq+"&fupdate=1&rd=0."+System.currentTimeMillis()+"&g_tk="+this.g_tk);
  108.         } catch (URISyntaxException e1) {
  109.             // TODO Auto-generated catch block
  110.             e1.printStackTrace();
  111.         }
  112.         geth.setURI(uri);
  113.         geth.setHeader("Content-Type","application/x-javascript; charset=utf-8");
  114.         geth.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.93 Safari/537.36");


  115.         //System.out.println("Executing request " + geth.getURI());//开始
  116.          String html="";
  117.          try {
  118.             CloseableHttpResponse response = httpclient.execute(geth);
  119.             // System.out.println(response.getStatusLine());
  120.              HttpEntity entity = response.getEntity();  
  121.              html=EntityUtils.toString(entity, "utf-8");
  122.         } catch (ClientProtocolException e) {
  123.             e.printStackTrace();
  124.         } catch (IOException e) {
  125.             e.printStackTrace();
  126.         }
  127.         // System.out.println(html);
  128.         return html;
  129.     }
  130.     /**
  131.      * 获得用户感兴趣的数据
  132.      * @param httpclient
  133.      * @param userstr
  134.      * @return
  135.      */
  136.     public String getUserInster(CloseableHttpClient httpclient,String userstr)
  137.     {
  138.         URI uri=null;
  139.         try {
  140.             uri = new URI("http://page.qq.com/cgi-bin/profile/interest_get?uin="+userstr+"&vuin="+this.qq+"&flag=1&fupdate=1&rd=0."+System.currentTimeMillis()+"&g_tk="+this.g_tk);
  141.         } catch (URISyntaxException e1) {
  142.             // TODO Auto-generated catch block
  143.             e1.printStackTrace();
  144.         }
  145.         geth.setURI(uri);
  146.         geth.setHeader("Content-Type","application/x-javascript; charset=utf-8");
  147.         geth.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.93 Safari/537.36");


  148.         //System.out.println("Executing request " + geth.getURI());//开始
  149.          String html="";
  150.          try {
  151.             CloseableHttpResponse response = httpclient.execute(geth);
  152.             // System.out.println(response.getStatusLine());
  153.              HttpEntity entity = response.getEntity();  
  154.              html=EntityUtils.toString(entity, "utf-8");
  155.         } catch (ClientProtocolException e) {
  156.             e.printStackTrace();
  157.         } catch (IOException e) {
  158.             e.printStackTrace();
  159.         }
  160.          //System.out.println(html);
  161.         return html;
  162.     }
  163.     /**
  164.      * 获取用户发表的说说
  165.      * @param args
  166.      */
  167.     public String getUserShuoshuo(CloseableHttpClient httpclient,String userstr)
  168.     {
  169.         URI uri=null;
  170.         try {
  171.             uri = new URI("http://ic2.qzone.qq.com/cgi-bin/feeds/feeds_html_act_all?uin="+this.qq+"&hostuin="+userstr+"&scope=0&filter=all&flag=1&refresh=0&firstGetGroup=0&mixnocache=0&scene=0&begintime=undefined&icServerTime=&start=10&count=10&sidomain=ctc.qzonestyle.gtimg.cn&useutf8=1&outputhtmlfeed=1&refer=2&r=0."+System.currentTimeMillis()+"&g_tk="+this.g_tk);
  172.         } catch (URISyntaxException e1) {
  173.             // TODO Auto-generated catch block
  174.             e1.printStackTrace();
  175.         }
  176.         geth.setURI(uri);
  177.         geth.setHeader("Content-Encoding","gzip");
  178.         geth.setHeader("Content-Type","application/x-javascript; charset=utf-8");
  179.         geth.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.93 Safari/537.36");


  180.         //System.out.println("Executing request " + geth.getURI());//开始
  181.          String html="";
  182.          try {
  183.             CloseableHttpResponse response = httpclient.execute(geth);
  184.             // System.out.println(response.getStatusLine());
  185.              HttpEntity entity = response.getEntity();  
  186.              html=EntityUtils.toString(entity, "utf-8");
  187.         } catch (ClientProtocolException e) {
  188.             e.printStackTrace();
  189.         } catch (IOException e) {
  190.             e.printStackTrace();
  191.         }
  192.         return html;
  193.     }
  194.     public static void main(String[] args)
  195.     {
  196.         Main main=new Main();
  197.         main.login();
  198.         for(int i=1;i<=2;i++)
  199.         {

  200.             String qq=i+"";
  201.             Task task=new Task();
  202.             task.main=main;
  203.             task.zoneno=qq;
  204.             task.start();
  205.         }

  206.     }
  207. }
复制代码


以上代码就已经解决了qq空间的登陆问题,当然有人可以说,登陆不就是一个cookie的问题吗,直接在浏览器里干到代码里面。如果是这样,就是去了对破解登陆的乐趣了。

再来说抓取用户的数据,qq空间是一个十分麻烦的网页,好多杂功能,在网页解析和json解析的地方特别麻烦,因此找一个好的接口,十分重要。

2016-07-22 15:07:57 [ Thread-0:9902 ] - [ INFO ] 63820@|1006800002,1006666003,2359374995,995872784,442015966,2252577017,690434952,2490068617,1911138953,1379986183,1005200001,88882222,536264574@|

上一条就是我获取的一个用户的信息,此人qq号码为63820,他关注过的qq认证空间为1006800002,1006666003,2359374995,995872784,442015966,2252577017,690434952,2490068617,1911138953,1379986183,1005200001,88882222,536264574,目前还没有抓取到他的说说。这样的数据已经是可以当作营销数据了。我已抓取了N条了,无奈我的性格,干到一半就不想干了,也就没有去想着抓取一票数据来卖钱,罢了,公开这些代码,让大家一起快乐的玩耍吧,项目中有我的腾讯的云mysql,使用时悠着点,不要删除数据哦。

百度运地址:http://pan.baidu.com/s/1i5uIFZ3
密码:zu3o
欢迎加入中国顶尖的java爬虫群:142351055


原始地址:

http://blog.csdn.net/qq_19383667/article/details/52157432









捡代码论坛-最全的游戏源码下载技术网站! - 论坛版权郑重声明:
1、本主题所有言论和图片纯属会员个人意见,与本论坛立场无关
2、本站所有主题由该帖子作者发表,该帖子作者与捡代码论坛-最全的游戏源码下载技术网站!享有帖子相关版权
3、捡代码论坛版权,详细了解请点击。
4、本站所有内容均由互联网收集整理、网友上传,并且以计算机技术研究交流为目的,仅供大家参考、学习,不存在任何商业目的与商业用途。
5、若您需要商业运营或用于其他商业活动,请您购买正版授权并合法使用。 我们不承担任何技术及版权问题,且不对任何资源负法律责任。
6、如无法链接失效或侵犯版权,请给我们来信:jiandaima@foxmail.com

回复

使用道具 举报

*滑块验证:
您需要登录后才可以回帖 登录 | 立 即 注 册

本版积分规则

技术支持
在线咨询
QQ咨询
3351529868

QQ|手机版|小黑屋|捡代码论坛-专业源码分享下载 ( 陕ICP备15015195号-1|网站地图

GMT+8, 2024-4-27 16:50

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表