获取公众号的二维码的ticket
我是用的永久的二维码获取方式
这里的scene_id不能重复,生成的二维码才不一样
public String getGZHCodeByPubKey(String sceneId) throws IOException {
HttpUtil httpUtil = new HttpUtil();
String url = Mark.wx_create_qrcode+"?access_token="+getAccessToken();
String json = "{\"expire_seconds\": 604800, \"action_name\": \"QR_LIMIT_SCENE\", \"action_info\": {\"scene\": {\"scene_id\":"+sceneId+"}}}";;
String result = httpUtil.HttpPostWithJson(url,json);
Map<String, Object> map = JSONObject.parseObject(result, Map.class);
if (!map.containsKey("ticket")) {
throw new ServiceException("错误信息:" + map.get("errmsg"));
}
String ticket = (String)map.get("ticket");
return ticket;
}
其中需要用到的方法
//post请求参数为json格式
public static String HttpPostWithJson(String url, String json) {
String returnValue = "这是默认返回值,接口调用失败";
CloseableHttpClient httpClient = HttpClients.createDefault();
ResponseHandler<String> responseHandler = new BasicResponseHandler();
try{
//第一步:创建HttpClient对象
httpClient = HttpClients.createDefault();
//第二步:创建httpPost对象
HttpPost httpPost = new HttpPost(url);
//第三步:给httpPost设置JSON格式的参数
StringEntity requestEntity = new StringEntity(json,"utf-8");
requestE