zoukankan      html  css  js  c++  java
  • 公众号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

    苟利国家生死以, 岂因祸福避趋之
  • 相关阅读:
    主从复制(多台服务器搭建环境)【十五】
    Linux中kill,pkill,killall和xkill命令汇总讲解
    redis持久化机制【十三】
    守护进程详解及创建,daemon()使用
    【small项目】MySQL第二天早上第一次连接超时报错,解决方法com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:
    Redis事务【十二】
    乐观锁和悲观锁的区别
    Redis 命令与连接【十一】
    Redis 配置【十】
    DB_Links创建际删除
  • 原文地址:https://www.cnblogs.com/chintsai/p/11829200.html
Copyright © 2011-2022 走看看