admin 发表于 2016-8-26 16:40:10

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

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

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

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

package ly.entity;

import java.math.BigInteger;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

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

import ly.util.HttpUtil;
import ly.util.RegexUtil;
import ly.util.Security;
import ly.util.Util;

public class QQ extends HttpService{
    /** QQ帐号 **/
    private String uin;
    /** QQ密码 **/
    private String password;
///** QQ-ID **/
//private String aid;
    /** 登录验证字符串 **/
    private String login_sig;
    /** 登录状态 0=登录成功,否则失败**/
    private int status;
    /** 验证码 **/
    private String verify;
    /** 随机数 **/
    private String random;
    /** QQ空间请求的G_TK**/
    protected String g_tk;

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

    private String requestLoginPage(){
      String request="http://xui.ptlogin2.qq.com/cgi-bin/xlogin?";
      request+="proxy_url=http%3A//qzs.qq.com/qzone/v6/portal/proxy.html&";
      request+="daid=5&&";
      request+="hide_title_bar=1&";
      request+="low_login=0&";
      request+="qlogin_auto_login=1&";
      request+="no_verifyimg=1&";
      request+="link_target=blank&";
      request+="appid=549000912&";
      request+="style=22&";
      request+="target=self&";
      request+="s_url=http%3A%2F%2Fqzs.qq.com%2Fqzone%2Fv5%2Floginsucc.html%3Fpara%3Dizone&";
      request+="pt_qr_app=%E6%89%8B%E6%9C%BAQQ%E7%A9%BA%E9%97%B4&";
      request+="pt_qr_link=http%3A//z.qzone.com/download.html&";
      request+="self_regurl=http%3A//qzs.qq.com/qzone/v6/reg/index.html&";
      request+="pt_qr_help_link=http%3A//z.qzone.com/download.html";
      String response = this.requestGet(request);
      return response;
    }
    private String ptui_identifier="";
    private String appid="";
    private String ptui_version="";
    private String s_url="";
    private String lang="";
    private String daid="";
    private String pt_vcode_v1="";
    //cookie data
    private String pt_login_sig="";
    private void getValuesFromLoginPageResponse(String response){
      //1.
      ptui_identifier=RegexUtil.replaceStartEnd(response, "c_login_2.js?", "\",");
      ptui_identifier=RegexUtil.replaceString(ptui_identifier, "ptui_identifier=");
      System.out.println("ptui_identifier:");
      System.out.println(ptui_identifier);
      //2.
      appid=RegexUtil.replaceStartEnd(response, "appid:", "lang");
      appid = RegexUtil.replaceStartEnd(appid, "\"", "\"");
      //3.
      ptui_version=RegexUtil.replaceStartEnd(response, "ptui_version:", "isHttps");
      ptui_version = RegexUtil.replaceStartEnd(ptui_version, "\"", "\"");
      //4.
      s_url=RegexUtil.replaceStartEnd(response, "s_url:", "proxy_url");
      s_url = RegexUtil.replaceStartEnd(s_url, "\"", "\"");
      //s_url=s_url.replaceAll("\\x", "%");   //将/x转换为%
      s_url=s_url__change();
      //5.
      lang=RegexUtil.replaceStartEnd(response, "lang:", "style");
      lang = RegexUtil.replaceStartEnd(lang, "\"", "\"");
      //6.
      daid=RegexUtil.replaceStartEnd(response, "daid:", "regmaster");
      daid = RegexUtil.replaceStartEnd(daid, "\"", "\"");
      //7
      pt_vcode_v1=RegexUtil.replaceStartEnd(response, "pt_vcode_v1:", "pt_ver_md5");
      pt_vcode_v1 = RegexUtil.replaceStartEnd(pt_vcode_v1, "\"", "\"");

      //cookies
      String cookie=this.getCookie();
      System.out.println("getValuesFromLoginPageResponse:");
      System.out.println(cookie);
      //
      pt_login_sig=RegexUtil.replaceStartEnd(cookie, "pt_login_sig=", ";");
    }
    private String requsetJSFile(){
      String requset="http://imgcache.qq.com/ptlogin/ver/10157/js/c_login_2.js?";
      requset+="max_age=604800&";
      requset+="ptui_identifier="+ptui_identifier;
      String response = this.requestGet(requset);
      return response;

    }
    private String rsa_e;
    private void getValuesFromJSResponse(String response){
      //1.rsa_e
      rsa_e=RegexUtil.replaceStartEnd(response, "function ot", "}");
      rsa_e=RegexUtil.replaceStartEnd(rsa_e, "e=\"", "\"");
      //2.
    }
    private String requestCheckFile(){
      String request="http://check.ptlogin2.qq.com/check?";
      request+="regmaster=&";
      request+="pt_tea=2&";
      request+="pt_vcode="+pt_vcode_v1+"&";
      request+="uin="+uin+"&";
      request+="appid="+appid+"&";
      request+="js_ver"+ptui_version+"&";
      request+="js_type=1&";
      request+="login_sig="+pt_login_sig+"&";
      request+="u1="+s_url+"&";
      request+="r=0.3326206956990063&";//随机数
      request+="pt_uistyle=32";
      System.out.println("requestCheckFile requset");
      System.out.println(request);
      String response = this.requestGet(request);
      return response;
    }
    String verifyCode="";//校验码
    String check_arg1="";
    String check_arg2="";
    String check_arg3="";
    String check_arg4="";
    String check_arg5="";
    String ptvfsession="";
    private void getValuesFromCheckResponse(String response){
      //1.第一个参数
      inti1=response.indexOf("'");
      inti2=response.indexOf("'",i1+1);
      check_arg1=response.substring(i1+1,i2);
      //2.第二个参数
      i1=response.indexOf("'",i2+1);
      i2=response.indexOf("'",i1+1);
      check_arg2=response.substring(i1+1,i2);
      verifyCode=check_arg2;
      //3.第三个参数
      i1=response.indexOf("'",i2+1);
      i2=response.indexOf("'",i1+1);
      check_arg3=response.substring(i1+1,i2);
      //4.第四个参数
      i1=response.indexOf("'",i2+1);
      i2=response.indexOf("'",i1+1);
      check_arg4=response.substring(i1+1,i2);
      //4.第5个参数
      i1=response.indexOf("'",i2+1);
      i2=response.indexOf("'",i1+1);
      check_arg5=response.substring(i1+1,i2);

      //cookie
      String cookie=this.cookie;
      //
      ptvfsession=RegexUtil.replaceStartEnd(cookie, "ptvfsession=", ";");
    }
    private void requestSubmitPage_setCookie(){//设置cookie
      //一个String的例子
      //pt_local_token=-1902107632; PATH=/; DOMAIN=ptlogin2.qq.com;

      List<String> lists=new ArrayList<String>();
      //1.
      String ptui_loginuin="ptui_loginuin="+uin+"; PATH=/; DOMAIN=ptlogin2.qq.com;";
      lists.add(ptui_loginuin);
      cookie = HttpUtil.mergeCookies(cookie, lists);
    }
    private void requestSubmitPage_in_print(){
      System.out.println("-------requestSubmitPage_in_print------");
      System.out.println("cookie:"+cookie);
    }
    private String requestSubmitPage(){
      String request="http://ptlogin2.qq.com/login?";
      request+="u="+uin+"&";
      request+="verifycode="+verifyCode+"&";
      request+="pt_vcode_v1=0&";
      //request+="pt_verifysession_v1="+check_arg4+"&";
      request+="pt_verifysession_v1="+ptvfsession+"&";
      request+="p="+encryptedPassword+"&";
      request+="pt_randsalt="+check_arg5+"&";
      request+="u1="+s_url+"&";
      request+="ptredirect=0&";
      request+="h=1&";
      request+="t=1&";
      request+="g=1&";
      request+="from_ui=1&";
      request+="ptlang="+lang+"&";
      request+="action=1-28-"+MyDate.getMS_19700101()+"&";
      request+="js_ver="+ptui_version+"&";
      request+="js_type=1&";
      request+="login_sig="+pt_login_sig+"&";
      request+="pt_uistyle=32&";
      request+="aid="+appid+"&";
      request+="daid="+daid;
      System.out.println("request:"+request);
      String response = this.requestGet(request);
      return response;
    }
    public void getValueFromVerifyResponse(){
      if(check_arg2.length()>5){
            //cookie
            String cookie=this.getCookie();
            System.out.println(" cookie:");
            System.out.println(cookie);
            //
            ptvfsession=RegexUtil.replaceStartEnd(cookie, "verifysession=", ";");
      }

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

//      //如果有验证码,一定要在密码加密前搞定哟
//      if($verify.length()>5){
//          System.out.println("请进入"+Util.root+"\\verifyTemp\\verify.jpg"+"查看验证码,且在控制台输入验证码→回车");
//          $url = "http://captcha.qq.com/getimage?uin={0}&aid={1}&{2}";
//          $url = Util.fillString($url, $uin, $aid, new Random().nextDouble()+"");
//          this.requestDownload($url, Util.root+"\\verifyTemp\\verify.jpg");
//          //不解释,控制台输入验证码,突然感觉自己好水。。。话又说回来,如果使用B/S架构,这里还要重构。。额,好吧。。。
//          Scanner scanner = new Scanner(System.in);
//          $verify = scanner.nextLine();
//      }
    }
    private String encryptedPassword="";
    public int login(){
      //1.请求登录页面,并解析数据
      String loginPage_response=requestLoginPage();
      //System.out.println("login response:");
      //System.out.println(loginPage_response);
      getValuesFromLoginPageResponse(loginPage_response);
      //2.请求JS文件,并解析数据
      String js_reponse=requsetJSFile();
      //System.out.println("请求js的响应:");
      //System.out.println(js_reponse);
      getValuesFromJSResponse(js_reponse);
      //3.请求check文件,并解析数据
      String check_response=requestCheckFile();
      System.out.println("-----------check_response:");
      System.out.println(check_response);
      getValuesFromCheckResponse(check_response);
      //4.解析校验码
      requestVerifyCode();
      getValueFromVerifyResponse();
      //simulation();//为了debug,进行仿真。
      //5.加密密码
      encodePassword_in_print();
      encryptedPassword=encodePassword(password,check_arg3,verifyCode,false);
      System.out.println("最终的p:");
      System.out.println(encryptedPassword);
      //6.提交登录
      requestSubmitPage_setCookie();
      requestSubmitPage_in_print();
      this.submitResponse=requestSubmitPage();
      System.out.println("-----------submitresponse:");
      System.out.println(this.submitResponse);
      return this.status;
    }


    private void simulation(){
      password="密码";//password
      check_arg3="\\x00\\x00\\x00\\x00\\x2b\\x5b\\xa7\\x41";//salt
      verifyCode="!MDY";//verifycode
      rsa_e="9d270ada059af43fe7ba65753b56a5ec3ad564f5e7430471fd6bd80239417a7535d924b4457fc6efbe6c4a34aae741b2375c79e7213c52365b38821801ea9bd68c9ce49ad5033f51c86db51eaa398ba20176fe42bf3850106327a73537c3449e1665f83ac38ef20141bbe153d0c1ad08d936f1508274638b929479a84aae51c9bf6743b2716e27b8b916039c60bcfdde07ac529b0b35349e31f32de3f7eec8f5122f6a100e27744eacea269e4d65826324285f69dd78cb05b2cdd656c7c5ff20f882c7add04fbbc75bca7baeeae8a45d0d3eba18f487d1921437a584e54a4a59dcdcb8e78a2df777f8ec98cc0ca48a71c1b183d01c325a346dfca27b27bf77f9";
      rsa_private_e="6397266d5c3802a9acf496ef60175d370361d0857575271006059df26ace70086f83989b15a01036467ae1dbd6a28f239da9e126f40d021ca85a7b37339fb5fa9549a825e6f013e4841be237f8b05728e7bcbd0d17995c43c6528ec84efa5435cb03fbf7618ee9169acdb1d227ed54f6d290db04a66793de139a3c2ec6850a16cbd70b350dfafd5cd4b3d027e6e5344a9f8f25d35e5673e2348d0dbaa5f708fd5a293fd44d3bf6442bafe6fdfae1133d2c2e38a117535faa01a3191fb782301d812cc090ca55f27ac5599c2384657ce42ece9f961f376d2daf6ddee2ebd5ffa2dd691a4381b6d878547a18be5ef4a02442dd9c972dd6b22d2b0e30643b4d2ec1";
      hexRSAToDec="1a8677630794495cff8028dc5485c87a5419c428cd1e304f21b6a12cbad3ba5b83eebc7cbfa821cc854317979c06fbc702f9bd047c782d569c3d66d3a9df8eabb456d4b5bb21b6d09e27acc1f7c9642d71be998afc7d40d4df9cebe8f799142915e450323c88e54b3f461bc9cebe3bc6febd3ab0af3a2b4ef69e6d015db1f0c83f8f5bb3a03a8e7e4939986aba6a71306ea391b4a1e9ba438768212ac0bd6ae42a3d948e8472814ff83a8ea69760e28f78d507a1b0929d8fdcb5d158c558a9d7940f02932b776258e5e5aa7363913bb2a3e5050525c8aba1eb86f4cee4d3f8d7650a8e0cb411534d58b7dc01377518a5133af515f0b189eb06694b8cda120778";
    }
    private void encodePassword_in_print(){
      System.out.println("rsa_e:");
      System.out.println(rsa_e);
      System.out.println("password:"+password);
      System.out.println("cookie:"+cookie);
    }
    String hexRSAToDec=null;//如果无效,则设置为null或""
    private String rsa_private_e=null;//无效设置为null或""
    public String encodePassword(String password,String salt,String verifycode,Boolean isSafe){
      String password_md5_uppercase=MyMD5.MD5(password).toUpperCase();//o
      byte[] password_md5_uppsercase_hex=StringUtil.hexString2Hex_byte(password_md5_uppercase);//p

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

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


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

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

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

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

      byte[] tea_ciper_s2b=ArrayUtil.short2byte(tea_ciper);
      System.out.println("RSA要加密的明文:");
      ArrayUtil.outputHex(tea_ciper_s2b);
      //byte[] tea_ciper_s2b_byte=StringUtil.hexString2Hex_byte(tea_ciper_s2b);      
      String rsa_ciper=myRSA.encryptByPublicKey(tea_ciper_s2b);
      System.out.println("RSA加密后的密文:");
      System.out.println(rsa_ciper);

//=====================为了调试解密数据,如果外部指定要解密的数据,则解密外部数据,为了和js文件进行调试的手段
      if(hexRSAToDec!=null&&! hexRSAToDec.trim().equals("")){
            rsa_ciper=hexRSAToDec;
      }
      if(rsa_private_e!=null&&!rsa_private_e.trim().equals("")){
            System.out.println("要解密的数据:");
            System.out.println(rsa_ciper);
            byte[] debugDecText=myRSA.decryptByPrivateKey(rsa_ciper);
            System.out.println("debugDecText[解密后的数据,16进制显示,0x01显示为\"01\"]:");
            ArrayUtil.outputHex(debugDecText);
      }
//=====================为了调试      
      //BASE64加密
      System.out.println("base64要加密的数据");
      System.out.println(rsa_ciper);
      byte[] rsa_ciper_byte=StringUtil.hexString2Hex_byte(rsa_ciper);
      String mi_binary_base64=new String(Base64.encodeBase64(rsa_ciper_byte));
      String mi_binary_base64_replace=mi_binary_base64.replaceAll("/", "-");
      mi_binary_base64_replace=mi_binary_base64_replace.replaceAll("\\+", "*");
      mi_binary_base64_replace=mi_binary_base64_replace.replaceAll("=", "_");
      return mi_binary_base64_replace;

    }

    /**
   * 通过说说ID
   * 赞一个说说
   *
   * 1.需要登录状态 g_tk
   * 2.发送1次http请求
   *
   * @param curkey 说说IDexample:7b6eb61aa5616f52da7c0b00
   *
   */
    public void praise(String curkey){
      if(this.status != 0) return;
      String $url = "http://w.qzone.qq.com/cgi-bin/likes/internal_dolike_app?g_tk="+this.g_tk;
      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}";

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

    public String getUin() {
      return uin;
    }

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


    public String getPassword() {
      return password;
    }

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

//public String getAid() {
//      return aid;
//}
//
//public void setAid(String aid) {
//      this.aid = aid;
//}

    public String getLogin_sig() {
      return login_sig;
    }

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

    public int getStatus() {
      return status;
    }

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

    public String getVerify() {
      return verify;
    }

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

    public String getRandom() {
      return random;
    }

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

    public String getCookie() {
      return cookie;
    }

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

    public String getG_tk() {
      return g_tk;
    }

    public void setG_tk(String g_tk) {
      this.g_tk = g_tk;
    }
    private String s_url__change(){//将\x 转换为 %
      String temp="";
      for(int i=0;i<s_url.length()-1;i++){
            if((s_url.charAt(i)=='\\')&&(s_url.charAt(i+1)=='x')){
                temp+="%";
                i++;
            }else{
                temp+=s_url.charAt(i);
            }
      }
      return temp;
    }

}

然后再来看对上面代码的使用:
publicvoid login()
    {
      URI uri=null;
      HttpTool tool=new HttpTool();
      QQ qq = new QQ(this.qq,this.password);
      qq.login();//登录
      String cc=qq.submitResponse;
      int start=cc.indexOf("http://");

      cc=cc.substring(start);
      int end=cc.indexOf(',')-1;
      cc=cc.substring(0, end);
      System.out.println("cc:"+cc);
      try {
            Thread.sleep(5000);
      } catch (Exception e) {
            e.printStackTrace();
      }
      this.geth=tool.httponline(qq.getCookie());
      this.skey=geth.getFirstHeader("skey").getValue();
      this.g_tk=tool.GetG_TK(this.skey);
         CloseableHttpClient httpclient = HttpClients.createDefault();
         URI urc=null;
      try {
            urc = new URI(cc);
      } catch (URISyntaxException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
      }
         this.geth.setURI(urc);
         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");

         System.out.println("Executing request " + geth.getURI());//开始
         String html="";
         try {
            CloseableHttpResponse response = httpclient.execute(geth);
             System.out.println(response.getStatusLine());
             HttpEntity entity = response.getEntity();
             html=EntityUtils.toString(entity, "utf-8");
      } catch (ClientProtocolException e) {
            e.printStackTrace();
      } catch (IOException e) {
            e.printStackTrace();
      }
         this.client=httpclient;
         System.out.println(html);
    }贴出整个登陆的代码:
package zjt.client.http;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;

import ly.entity.QQ;


public class Main {
    HttpGet geth=new HttpGet();
    CloseableHttpClient client=null;
    String qq="2651116759";
    String password="xxxxxxx";
    String skey="";
    String g_tk="";
    publicvoid login()
    {
      URI uri=null;
      HttpTool tool=new HttpTool();
      QQ qq = new QQ(this.qq,this.password);
      qq.login();//登录
      String cc=qq.submitResponse;
      int start=cc.indexOf("http://");

      cc=cc.substring(start);
      int end=cc.indexOf(',')-1;
      cc=cc.substring(0, end);
      System.out.println("cc:"+cc);
      try {
            Thread.sleep(5000);
      } catch (Exception e) {
            e.printStackTrace();
      }
      this.geth=tool.httponline(qq.getCookie());
      this.skey=geth.getFirstHeader("skey").getValue();
      this.g_tk=tool.GetG_TK(this.skey);
         CloseableHttpClient httpclient = HttpClients.createDefault();
         URI urc=null;
      try {
            urc = new URI(cc);
      } catch (URISyntaxException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
      }
         this.geth.setURI(urc);
         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");

         System.out.println("Executing request " + geth.getURI());//开始
         String html="";
         try {
            CloseableHttpResponse response = httpclient.execute(geth);
             System.out.println(response.getStatusLine());
             HttpEntity entity = response.getEntity();
             html=EntityUtils.toString(entity, "utf-8");
      } catch (ClientProtocolException e) {
            e.printStackTrace();
      } catch (IOException e) {
            e.printStackTrace();
      }
         this.client=httpclient;
         System.out.println(html);
    }
    /**
   * 用户主页
   * @param httpclient
   * @return
   */
    public CloseableHttpClient userPage(CloseableHttpClient httpclient,String userqq)
    {
      URI uri=null;
      try {
            uri = new URI("http://user.qzone.qq.com/"+userqq);
      } catch (URISyntaxException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
      }
      geth.setURI(uri);
      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");
      // System.out.println("Executing request " + geth.getURI());//开始
         String html="";
         try {
            CloseableHttpResponse response = httpclient.execute(geth);
            // System.out.println(response.getStatusLine());
             HttpEntity entity = response.getEntity();
             html=EntityUtils.toString(entity, "utf-8");
      } catch (ClientProtocolException e) {
            e.printStackTrace();
      } catch (IOException e) {
            e.printStackTrace();
      }
       // System.out.println(html);
      return httpclient;
    }
    /**
   * 获取用户基本信息的接口
   * @param httpclient
   * @param userstr
   * @return
   */
    public String getUserInfo(CloseableHttpClient httpclient,String userstr)
    {
      URI uri=null;
      try {
            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);
      } catch (URISyntaxException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
      }
      geth.setURI(uri);
      geth.setHeader("Content-Type","application/x-javascript; charset=utf-8");
      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");


      //System.out.println("Executing request " + geth.getURI());//开始
         String html="";
         try {
            CloseableHttpResponse response = httpclient.execute(geth);
            // System.out.println(response.getStatusLine());
             HttpEntity entity = response.getEntity();
             html=EntityUtils.toString(entity, "utf-8");
      } catch (ClientProtocolException e) {
            e.printStackTrace();
      } catch (IOException e) {
            e.printStackTrace();
      }
      // System.out.println(html);
      return html;
    }
    /**
   * 获得用户感兴趣的数据
   * @param httpclient
   * @param userstr
   * @return
   */
    public String getUserInster(CloseableHttpClient httpclient,String userstr)
    {
      URI uri=null;
      try {
            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);
      } catch (URISyntaxException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
      }
      geth.setURI(uri);
      geth.setHeader("Content-Type","application/x-javascript; charset=utf-8");
      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");


      //System.out.println("Executing request " + geth.getURI());//开始
         String html="";
         try {
            CloseableHttpResponse response = httpclient.execute(geth);
            // System.out.println(response.getStatusLine());
             HttpEntity entity = response.getEntity();
             html=EntityUtils.toString(entity, "utf-8");
      } catch (ClientProtocolException e) {
            e.printStackTrace();
      } catch (IOException e) {
            e.printStackTrace();
      }
         //System.out.println(html);
      return html;
    }
    /**
   * 获取用户发表的说说
   * @param args
   */
    public String getUserShuoshuo(CloseableHttpClient httpclient,String userstr)
    {
      URI uri=null;
      try {
            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);
      } catch (URISyntaxException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
      }
      geth.setURI(uri);
      geth.setHeader("Content-Encoding","gzip");
      geth.setHeader("Content-Type","application/x-javascript; charset=utf-8");
      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");


      //System.out.println("Executing request " + geth.getURI());//开始
         String html="";
         try {
            CloseableHttpResponse response = httpclient.execute(geth);
            // System.out.println(response.getStatusLine());
             HttpEntity entity = response.getEntity();
             html=EntityUtils.toString(entity, "utf-8");
      } catch (ClientProtocolException e) {
            e.printStackTrace();
      } catch (IOException e) {
            e.printStackTrace();
      }
      return html;
    }
    public static void main(String[] args)
    {
      Main main=new Main();
      main.login();
      for(int i=1;i<=2;i++)
      {

            String qq=i+"";
            Task task=new Task();
            task.main=main;
            task.zoneno=qq;
            task.start();
      }

    }
}

以上代码就已经解决了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]
查看完整版本: 完美解决QQ空间登陆的代码 使用技术:httpclient