java 访问 web qq协议(一)

本文介绍了如何使用Java访问Web QQ协议,首先展示了项目结构和登录二维码的获取过程,包括测试类的运行方法和二维码图片的保存路径。接着提到了HTTP请求的封装方法HttpClientUtil.doPost(),并警告不要修改请求URL后的参数。最后,作者指出该教程不会继续更新,因为已转向研究QQ安卓协议,Web QQ协议存在较多限制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


 

项目结构如下:

 

第一步,获取登录二维码

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协议限制太多

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值