Exemple #1
0
        /// <summary>
        /// 使用票据凭证以 POST 方式调用微信 API
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="rawUrl"></param>
        /// <param name="request"></param>
        /// <returns></returns>
        private static T PostWechatApiWithToken <T>(string rawUrl, WechatApiRequestMessage request) where T : WechatApiResponseMessage
        {
            string accessToken = WechatApi.GetAccessToken();

            string url = rawUrl + "access_token=" + accessToken;

            T response = WechatApi.PostWechatApi <T>(url, request);

            if (response.ErrorCode.HasValue)
            {
                switch (response.ErrorCode.Value)
                {
                case 0:
                    break;

                // 如果是票据过期问题,则重新获取票据
                case 40001:
                case 40014:
                case 42001:
                    LogHelper.Info(string.Format("调用微信接口时票据凭证无效或过期, errcode : {0}, errmsg : {1}, interface : {2}", response.ErrorCode, response.ErrorMessage, url));

                    // 重新获取票据凭证
                    accessToken = WechatApi.RefreshAccessToken(accessToken);

                    url = rawUrl + "access_token=" + accessToken;

                    response = WechatApi.PostWechatApi <T>(url, request);

                    if (response.ErrorCode.HasValue && response.ErrorCode.Value != 0)
                    {
                        throw new WechatApiInvokeException(response.ErrorCode.Value, response.ErrorMessage, url);
                    }
                    break;

                default:
                    throw new WechatApiInvokeException(response.ErrorCode.Value, response.ErrorMessage, url);
                }
            }

            return(response);
        }
Exemple #2
0
        /// <summary>
        /// 通过 POST 方式调用微信 API
        /// </summary>
        /// <param name="body"></param>
        /// <returns></returns>
        private static T PostWechatApi <T>(string url, WechatApiRequestMessage request) where T : WechatApiResponseMessage
        {
            string body = WechatApi.HttpPost(url, JsonConvert.SerializeObject(request));

            return(JsonConvert.DeserializeObject <T>(body));
        }