代码中调用微信订阅号中<删除永久素材>接口
https://developers.weixin.qq.com/doc/offiaccount/Asset_Management/Deleting_Permanent_Assets.html
简要说明:
- 请求方式:post
- 有传参数,post传参
配置文件:
#删除永久素材
delMaterial=https://api.weixin.qq.com/cgi-bin/material/del_material?access_token=
调用代码:
@ResponseBody
@RequestMapping(value = "/delMaterial", produces = "text/plain;charset=UTF-8")
public JSONObject delMaterial(HttpServletRequest request, HttpServletResponse response, HttpSession session
// @RequestHeader("Authorization") String authToken,
, String accessToken, String media_id) throws Exception {
log.debug("*******************删除永久素材***********************");
// 测试号
String url = delMaterial + accessToken;
System.out.println(url);
JSONObject json = new JSONObject();
json.put("media_id", media_id);
String str = postByJson(url, json);
JSONObject auth = JSONObject.parseObject(str);
return auth;
}
代码中调用postByJson方法
public static String postByJson(String url, JSONObject jsonObject) throws ParseException, IOException {
String body = "";
// 创建httpclient对象
CloseableHttpClient client = HttpClients.createDefault();
// 创建post方式请求对象
HttpPost httpPost = new HttpPost(url);
// 装填参数
StringEntity s = new StringEntity(jsonObject.toString(), "utf-8");
s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
// 设置参数到请求对象中
httpPost.setEntity(s);
System.out.println("请求地址:" + url);
//System.out.println("请求参数:"+nvps.toString());
// 设置header信息
// 指定报文头【Content-type】、【User-Agent】
// httpPost.setHeader("Content-type", "application/x-www-form-urlencoded");
httpPost.setHeader("Content-type", "application/json");
httpPost.setHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
// 执行请求操作,并拿到结果(同步阻塞)
CloseableHttpResponse response = client.execute(httpPost);
// 获取结果实体
HttpEntity entity = response.getEntity();
if (entity != null) {
// 按指定编码转换结果实体为String类型
body = EntityUtils.toString(entity, "utf-8");
}
EntityUtils.consume(entity);
// 释放链接
response.close();
return body;
}
(当时写的时候,忘记存参考地址了,如果有知道的,谢谢提醒~)
希望可以帮到你(~ ̄▽ ̄)~
加油o(* ̄3 ̄)o