一、问题描述
最近公司有个需求是,从小程序哪里获取二维码,然后从二维码中解析出URL来,自己定制下二维码。。。嫌微信生成的码太难看了。。。微信小程序的获取二维码的文档如下:https://developers.weixin.qq.com/miniprogram/dev/api/qrcode.html 看着这个文档。。。就想吐槽下。。写的太含糊了。
竟然没写返回值是什么。。最后得知,它是以流的形式返回。。。文档里有三A、B、C三种类型的接口,不清楚微信怎么想的。。。分这么多种接口,做什么。。A,B接口有生成次数限制,B接口没有限制。。
测试代码如下:
public class Test {
public static void main(String[] args) {
try {
URL url = new URL("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=你的access_token");
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");// 提交模式
// conn.setConnectTimeout(10000);//连接超时 单位毫秒
// conn.setReadTimeout(2000);//读取超时 单位毫秒
// 发送POST请求必须设置如下两行
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
// 获取URLConnection对象对应的输出流
PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
// 发送请求参数
JSONObject paramJson = new JSONObject();
paramJson.put("scene", "a=1234567890");
paramJson.put("page", "pages/index/index");
paramJson.put("width", 430);
paramJson.put("auto_color", true);
/**
* line_color生效
* paramJson.put("auto_color", false);
* JSONObject lineColor = new JSONObject();
* lineColor.put("r", 0);
* lineColor.put("g", 0);
* lineColor.put("b", 0);
* paramJson.put("line_color", lineColor);
* */
printWriter.write(paramJson.toString());
// flush输出流的缓冲
printWriter.flush();
//开始获取数据
BufferedInputStream bis = new BufferedInputStream(httpURLConnection.getInputStream());
OutputStream os = new FileOutputStream(new File("/Users/Xxxx/Music/abc.png"));
int len;
byte[] arr = new byte[1024];
while ((len = bi