项目结构如下:
第一步,获取登录二维码
package com.koow.kkwwo.qq.web;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import com.koow.kkwwo.https.HttpClientUtil;
/**
* 获取登录二维码
*
* @author Koow
*
*/
public class GetQRCode {
public static String getQRCode(String fileUrl) {
try {
HttpResponse response = (HttpResponse) HttpClientUtil.doPost(
"https://ssl.ptlogin2.qq.com/ptqrshow?appid=501004106&e=2&l=M&s=3&d=72&v=4&t=0.08012284431105776&daid=164&pt_3rd_aid=0",
null, "utf-8", 3);
HttpEntity resEntity = response.getEntity();
InputStream is = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
is = resEntity.getContent();
byte[] buf = new byte[1024];
int readBytes = -1;
while ((readBytes = is.read(buf)) != -1) {
baos.write(buf, 0, readBytes);
}
} finally {
if (baos != null) {
baos.close();
}
if (is != null) {
is.close();
}
}
byte[] imageArray = baos.toByteArray();
File imageFile = new File(fileUrl);
// 创建输出流
FileOutputStream outStream;
outStream = new FileOutputStream(imageFile);
// 写入数据
outStream.write(imageArray);
// 关闭输出流
outStream.close();
Header[] headers = response.getHeaders("Set-Cookie");
for (int i = 0; i < headers.length; i++) {
String cookie = headers[i].getValue();
String[] cookievalues = cookie.split(";");
for (int j = 0; j < cookievalues.length; j++) {
String[] keyPair = cookievalues[j].split("=");
String key = keyPair[0].trim();
String value = keyPair.length > 1 ? keyPair[1].trim() : "";
if (key.equals("qrsig")) {
return value;
}
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
测试类如下
package com.koow.kkwwo.test;
import com.koow.kkwwo.qq.web.GetQRCode;
/**
* 测试
*
* @author Koow
*/
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
String qrsig=GetQRCode.getQRCode("E://a.png");
System.out.println("qrsig="+qrsig);
}
}
右键运行,这样二维码图片就保存在了E盘下面
并获取到下一步所需参数qrsig
HttpClientUtil.doPost() 是封装的一个http请求类
至于请求的url后面那些参数,不要随意乱动。带着就行,固定写死
测试前注意jar包的引用与加载
2018-12-18 :PS:不会再更新了,博主现在转研究qq安卓协议,web qq协议限制太多