公众号API接口调用次数清零

最近在做微信公众号开发,过程中遇到了下面的问题:

{"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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

九成宫醴泉铭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值