public class QQ {
public static final String QQNUM = "1985063628"; // QQ行号码
public static final String PASSWORD = "JDCZ1gsdps"; // 密码
private static DefaultHttpClient httpclient = new DefaultHttpClient();
private static boolean loginFlag = false;
private static List<Cookie> cookies;
private static HttpResponse response;
private static HttpGet request;
private static String verifyString;
private static String verifySession;
private static void getVerifyImage() throws Exception {
String url = "http://ptlogin2.qq.com/getimage?aid=8000108&0.7022592303274631";
HttpGet httpget = new HttpGet(url);
httpget.addHeader("Cookie", "");
httpget.setHeader("Accept", "text/html, */*");
httpget
.addHeader("User-Agent",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727)");
// httpget.addHeader("Connection", "close");
// httpget.setHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT
// 5.1; zh-CN; rv:1.8.1.20) Gecko/20081217 Firefox/2.0.0.20")
response = httpclient.execute(httpget);
Header[] headers = response.getHeaders("Set-Cookie");// .getAllHeaders();
for (Header h : headers) {
System.out.println(h);
}
verifySession = headers[0].getValue().split(";|=")[1];
InputStream in = response.getEntity().getContent();
File verifyFile = new File(QQ.class.getResource("output").getPath()
+ "/verifycode.jpg");
FileOutputStream out = new FileOutputStream(verifyFile);
byte[] buf = new byte[1024];
while (in.read(buf) != -1) {
out.write(buf);
}
out.flush();
System.out.println("验证码图片已生成。路径:" + verifyFile.getCanonicalPath());
System.out.println("请输入验证码:");
verifyString = new BufferedReader(new InputStreamReader(System.in))
.readLine();
while (verifyString.length() != 4) {
System.out.println("验证码长度有误,请输入4位验证码:");
verifyString = new BufferedReader(new InputStreamReader(System.in))
.readLine();
}
}
public static void doLogin() throws Exception {
if (!loginFlag) {
getVerifyImage();
HttpPost httpost = new HttpPost("http://ptlogin2.qq.com/login");
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("u", QQNUM));
nvps.add(new BasicNameValuePair("p", MD5Security.md5(MD5Security.md5_3(PASSWORD)
+ verifyString.toUpperCase())));
nvps.add(new BasicNameValuePair("verifycode", verifyString));
nvps.add(new BasicNameValuePair("aid", "8000108"));
nvps.add(new BasicNameValuePair("u1",
"http://imgcache.qq.com/qzone/v5/loginsucc.html"));
nvps.add(new BasicNameValuePair("fp", "loginerroralert"));
nvps.add(new BasicNameValuePair("h", "1"));
nvps.add(new BasicNameValuePair("ptredirect", "0"));
nvps.add(new BasicNameValuePair("ptlang", "0"));
nvps.add(new BasicNameValuePair("from_ui", "1"));
nvps.add(new BasicNameValuePair("dumy", "1"));
httpost
.setHeader("User-Agent",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727)");
httpost.setHeader("Cookie", "verifysession=" + verifySession + ";");
httpost
.setHeader(
"Referer",
"http://ui.ptlogin2.qq.com/cgi-bin/login?link_target=blank&target=self&appid=8000108&qlogin_jumpname=vipmyqq&f_url=loginerroralert&qlogin_auto_login=1&s_url=http%3A//imgcache.qq.com/qzone/v5/loginsucc.html&qlogin_param=jump_url%3D");
httpost.setHeader("Accept", "text/html, */*");
httpost.removeHeaders("Cookie2");
httpost.removeHeaders("Expect");
httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
response = httpclient.execute(httpost);
cookies = httpclient.getCookieStore().getCookies();
if (cookies.size() < 3) {
System.err.println("登录失败...");
loginFlag = false;
} else {
System.out.println("登录成功...");
for (Cookie c : cookies) {
System.out.println(c);
}
loginFlag = true;
}
}
}
public static void main(String[] args) throws Exception {
doLogin();
}