公衆號API接口調用次數清零

最近在作微信公衆號開發,過程當中遇到了下面的問題:java

{"errcode":45009,"errmsg":"reach max api daily quota limit hint: [_1qXIa02861466]"}apache

因而參照了一下別人的文章:https://zhuanlan.zhihu.com/p/33286178,寫了一個將調用次數清零的方法:json

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/11789546api

相關文章
相關標籤/搜索