最近在做微信公众号开发,过程中遇到了下面的问题:
{"errcode":45009,"errmsg":"reach max api daily quota limit hint: [_1qXIa02861466]"}
于是参照了一下别人的文章:https://zhuanlan.zhihu.com/p/33286178,写了一个将调用次数清零的方法:
package com.zkdx.hangul_service.test;
import com.alibaba.fastjson.JSON;
import net.sf.json.JSONObject;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class ResetWechatAPICall {
private static JSONObject sendPost(String accessToken, String appid) {
String url = "https://api.weixin.qq.com/cgi-bin/clear_quota?access_token=" + accessToken;
net.sf.json.JSONObject jsonObject = null;
//0.准备好json请求参数
Map<String, String> paramMap = new HashMap<String, String>();
paramMap.put("appid", appid);
Object data = com.alibaba.fastjson.JSON.toJSON(paramMap);
//1.生成一个请求
HttpPost httpPost = new HttpPost(url);
//2.配置请求属性
//2.1 设置请求超时时间
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(100000).setConnectTimeout(100000).build();
httpPost.setConfig(requestConfig);
//2.2 设置数据传输格式-json
httpPost.addHeader("Content-Type", "application/json");
//2.3 设置请求参数
StringEntity requestEntity = new StringEntity(JSON.toJSONString(data), "utf-8");
httpPost.setEntity(requestEntity);
//3.发起请求,获取响应信息
//3.1 创建httpClient
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
try {
//4. 发起请求,获取响应信息
response = httpClient.execute(httpPost, new BasicHttpContext());
// 请求成功
if (HttpStatus.SC_OK == response.getStatusLine().getStatusCode()) {
// 5. 取得请求内容
HttpEntity entity = response.getEntity();
if (entity != null) {
String result = EntityUtils.toString(entity, "UTF-8");
jsonObject = net.sf.json.JSONObject.fromObject(result);
}
if (entity != null) {
entity.consumeContent();
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (response != null) {
try {
// 6. 释放资源
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
try {
if (jsonObject.has("errcode") && jsonObject.get("errcode").equals("48006")) {
throw new Exception("forbid to clear quota because of reaching the limit");
}
} catch (Exception e) {
e.printStackTrace();
}
return jsonObject;
}
public static void main(String[] args) {
JSONObject result = sendPost("25_PFIHNixNXCbywVSiql5Y8pZc3mZ9YOrMN5E-nhfn2OJxcE8HYTC0pqn07WWaiYZUgMAs3u4b5zL7Ct-XNQ30LQ1aqtKYKDWfsoHEXqz4bSQP3DiH2aBVu_U-5CPZdkdhGImKslObWocCqjEPVOOiAEAEXX");
System.out.println(result);
}
}
需要的jar文件下载地址: https://download.csdn.net/download/qq_40723748/11789546