50,526
社区成员
发帖
与我相关
我的任务
分享
public static final String HEXSTRING = "0123456789ABCDEF";
//实例化认证对象
public static OAuthV2 oAuth = new OAuthV2();
//实例化链接对象
private static HttpClient client = new DefaultHttpClient();
// 初始oAuth应用信息
public static void init(/*OAuthV2 oAuth*/)
{
//客户端ID
oAuth.setClientId("801334560");
//客户端密码
oAuth.setClientSecret("fc1864532f5d29abd92b6ce52593f127");
//返回地址
oAuth.setRedirectUri("http://www.baidu.com");
}
/**
* 对密码进行加密
* @param qq
* http://check.ptlogin2.qq.com/check?uin={0}&appid=15000101&r={1 }
* 返回的第三个值
* @param password
* QQ密码
* @param verifycode
* 验证码
* @return 加密后的密码
* @throws UnsupportedEncodingException
* @throws Exception
*
*/
public static String GetPassword(String qq, String password,
String verifycode) throws Exception
{
String P = hexchar2bin(md5(password));
String U = md5(P + hexchar2bin(qq.replace("\\x", "").toUpperCase()));
String V = md5(U + verifycode.toUpperCase());
return V;
}
public static String md5(String originalText) throws Exception
{
byte buf[] = originalText.getBytes("ISO-8859-1");
StringBuffer hexString = new StringBuffer();
String result = "";
String digit = "";
try
{
MessageDigest algorithm = MessageDigest.getInstance("MD5");
algorithm.reset();
algorithm.update(buf);
byte[] digest = algorithm.digest();
for (int i = 0; i < digest.length; i++)
{
digit = Integer.toHexString(0xFF & digest[i]);
if (digit.length() == 1)
{
digit = "0" + digit;
}
hexString.append(digit);
}
result = hexString.toString();
}
catch (Exception ex)
{
result = "";
}
return result.toUpperCase();
}
public static String hexchar2bin(String md5str) throws UnsupportedEncodingException
{
ByteArrayOutputStream baos = new ByteArrayOutputStream(md5str.length() / 2);
for (int i = 0; i < md5str.length(); i = i + 2)
{
baos.write((HEXSTRING.indexOf(md5str.charAt(i)) << 4 | HEXSTRING
.indexOf(md5str.charAt(i + 1))));
}
return new String(baos.toByteArray(), "ISO-8859-1");
}
/***
* 模拟登录QQ
* @param qq QQ号码
* @param password QQ密码
* @throws Exception
*/
public static void login(String qq, String password) throws Exception
{
//创建一个连接对象
HttpGet get = new HttpGet("https://ssl.ptlogin2.qq.com/check?uin="+ qq + "&appid=46000101&ptlang=2052&js_type=2&js_ver=10009&r=0.7948186025712065");
//实例化链接对象
HttpResponse response = client.execute(get);
String entity = EntityUtils.toString(response.getEntity());
String[] checkNum = entity.substring(entity.indexOf("(") + 1,entity.lastIndexOf(")")).replace("'", "").split(",");
String pass = "";
String responseData = "";
// 获取验证码(如果有验证码输出到C:/code.jpg,查看后输入可继续执行
System.out.println(checkNum[0]);
System.out.println(checkNum[1]);
System.out.println(checkNum[2].trim());
System.out.println(checkNum[2].trim().replace("\\x", ""));
if ("1".equals(checkNum[0]))
{
// uin为qq号或者微博用户名
HttpGet getimg = new HttpGet("http://captcha.qq.com/getimage?aid=46000101&r=0.3478789969909082&uin=" + qq + "&vc_type=" + checkNum[1] + "");
HttpResponse response2 = client.execute(getimg);
//OutputStream os = new FileOutputStream("c:/code.jpg");
OutputStream os = new FileOutputStream("code.gif");
byte[] b = EntityUtils.toByteArray(response2.getEntity());
os.write(b, 0, b.length);
os.close();
System.out.println("在项目目录下找验证码文件!");
System.out.println("请输入验证码:");
Scanner in = new Scanner(System.in);
responseData = in.nextLine();
in.close();
}
else
{
responseData = checkNum[1];
}
/** *******************加密密码 ************************** */
pass = GetPassword(checkNum[2], password, responseData);
/** *********************** 登录 *************************** */
HttpGet getimg = new HttpGet("https://ssl.ptlogin2.qq.com/login?ptlang=2052&u="+ qq+ "&p="+ pass+ "&verifycode="+ responseData+ "&aid=46000101&target=top&u1=https%3A%2F%2Fopen.t.qq.com%2Fcgi-bin%2Foauth2%2Fauthorize%3Fclient_id%3D"
+ oAuth.getClientId()+ "%26response_type%3Dcode%26redirect_uri="+ oAuth.getRedirectUri()+ "&ptredirect=1&h=1&from_ui=1&dumy=&qlogin_param=abbfew=ddd&wording=%E6%8E%88%E6%9D%83&fp=loginerroralert&action=8-13-240977&g=1&t=1&dummy=&js_type=2&js_ver=10009");
HttpResponse response2 = client.execute(getimg);
HttpEntity httpentity = response2.getEntity();
System.out.println(EntityUtils.toString(httpentity));
}
/**
* 获取CODE
*标准的返回值URL:http://www.myurl.com/example?code=CODE&openid=OPENID&openkey=OPENKEY
*/
public static String getUrl() throws ClientProtocolException, IOException
{
String codeRequire="https://open.t.qq.com/cgi-bin/oauth2/authorize?"
+ "client_id="+ oAuth.getClientId()+ "&response_type=code&redirect_uri="+ oAuth.getRedirectUri()+ ""
+ "&wap=2&state=&forcelogin=false";
HttpGet getcode=new HttpGet(codeRequire);
//HttpGet getcode = new HttpGet("https://open.t.qq.com/cgi-bin/oauth2/authorize?client_id="+ oAuth.getClientId()+ "&response_type=code&redirect_uri="
// + oAuth.getRedirectUri()+ "&checkStatus=yes&appfrom=&g_tk&checkType=showAuth&state=");
HttpResponse response3 = client.execute(getcode);
HttpEntity entityqqq = response3.getEntity();
String entityxcc = EntityUtils.toString(entityqqq);
return entityxcc;
}