微信 getUnlimited()调用小程序码
Java SpringBoot 2.2.2
1、工具类 Util
/* 发送 post请求 用HTTPclient 发送请求*/
public static byte[] post(String URL,String json) {
String obj = null;
InputStream inputStream = null;
Buffer reader = null;
byte[] data = null;
// 创建默认的httpClient实例.
CloseableHttpClient httpclient = HttpClients.createDefault();
// 创建httppost
HttpPost httppost = new HttpPost(URL);
httppost.addHeader("Content-type", "application/json;charset=utf-8");
httppost.setHeader("Accept", "application/json");
try {
StringEntity s = new StringEntity(json,Charset.forName("utf-8"));
s.setContentEncoding("utf-8");
httppost.setEntity(s);
CloseableHttpResponse response = httpclient.execute(httppost);
try {
// 获取相应实体
HttpEntity entity = response.getEntity();
if (entity != null) {
inputStream = entity.getContent();
data = readInputStream(inputStream);
}
return data;
} finally {
response.close();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭连接,释放资源
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return data;
}
/** 将流 保存为数据数组
* @param inStream
* @return
* @throws Exception
*/
public static byte[] readInputStream(InputStream inStream) throws Exception {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
// 创建一个Buffer字符串
byte[] buffer = new byte[1024];
// 每次读取的字符串长度,如果为-1,代表全部读取完毕
int len = 0;
// 使用一个输入流从buffer里把数据读取出来
while ((len = inStream.read(buffer)) != -1) {
// 用输出流往buffer里写入数据,中间参数代表从哪个位置开始读,len代表读取的长度
outStream.write(buffer, 0, len);
}
// 关闭输入流
inStream.close();
// 把outStream里的数据写入内存
return outStream.toByteArray();
}
2、Controller
Unlimited --> 封装的请求实体
/**
* 获取小程序码
* */
@PostMapping("/AppletsController/GetUnlimited")
@ResponseBody
public byte[] GetUnlimited(@ModelAttribute Unlimited unlimited,@RequestParam String access_token){
if(unlimited!=null){
Map<String ,Object> map = new HashMap<>();
map.put("scene", unlimited.getScene());
map.put("width", unlimited.getWidth());
map.put("page",unlimited.getPage());
String jsonString = JSON.toJSONString(map);
System.out.println("jsonString---->"+jsonString);
byte[] unlimited1 = WeChatPushUtil.post("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+access_token,jsonString);
return unlimited1;
}else {
return null;
}
}
3、Unlimited 封装实体
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Unlimited implements Serializable {
private static final long serialVersionUID = 3908180766294241032L;
private String scene; //最大32个可见字符,只支持数字,大小写英文以及部分特殊字符:!#$&'()*+,/:;=?@-._~,其它字符请自行编码为合法字符(因不支持%,中文无法使用 urlencode 处理,请使用其他编码方式)
private String page; //必须是已经发布的小程序存在的页面(否则报错)例如 pages/index/index,
private Integer width; //二维码的宽度,单位 px,最小 280px,最大 1280px
private boolean auto_color; //自动配置线条颜色,如果颜色依然是黑色,则说明不建议配置主色调,默认 false
private Object line_color; //auto_color 为 false 时生效,使用 rgb 设置颜色 例如 {"r":"xxx","g":"xxx","b":"xxx"} 十进制表示
private boolean is_hyaline; //是否需要透明底色,为 true 时,生成透明底色的小程序
}
** 实体使用的是 lombok 这里也可以用传统的 实体 ,就需要生成 get 和 set 方法 **
最后可能遇到的BUG:
1.再调用后可能会返回乱码的情况: 如下
����JFIF��C��C��"��
���}!1AQa"q2���#B��R��$3br�
%&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz���������������������������������������������������������������������������
���w!1AQaq"2�B���� #3R�br�
$4�%�&'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz��������������������������������������������������������������������������?��(��(��(��(��(��(��(��(��(����Q�³!*��t%]N �+��� ����;���
�"�*־7~�_�3���g���
解决方法:
小程序端设置:
responseType: 'arraybuffer', //设置响应类型
arraybuffer: true, //转码
2.再调用后可能会返回JSON: 如下
{"errcode":40001,"errmsg":"invalid credential, access_token is invalid or not latest rid: 601a1dd5-264b258f-505988f2"}
这里意思是请求的格式问题 ,微信的POST 请求 需要传json格式的数据。
下面是返回错误的常见数值: