コード例 #1
0
        public static async Task <BatchGetMaterialsJson> BatchGetMaterials(string accessToken, string materialType, int offset, int count)
        {
            string api = "http://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=ACCESSTOKEN";

            api = api.Replace("ACCESSTOKEN", accessToken);

            var content = new
            {
                type   = materialType,
                offset = offset,
                count  = count
            }.ToJson();

            var requestMessage = new HttpRequestMessage(HttpMethod.Post, api);

            requestMessage.Content = new StringContent(content);
            var client   = new HttpClient();
            var response = await client.SendAsync(requestMessage);

            if (!response.IsSuccessStatusCode)
            {
                //Logger.LogError($"An error occurred while retrieving the user profile: the remote server returned a {response.StatusCode} response with the following payload: {response.Headers.ToString()} {await response.Content.ReadAsStringAsync()}");
                throw new HttpRequestException("An error occured while retrieving the user profile.");
            }

            var responseString = await response.Content.ReadAsStringAsync();

            var payload   = JsonDocument.Parse(responseString);
            int errorCode = WeixinApiHelper.GetErrorCode(payload);

            if (errorCode != 0)
            {
                var errorMessage = WeixinApiHelper.GetErrorMessage(payload);
                //Logger.LogError($"The remote server returned an error while retrieving the user profile. {errorCode} {errorMessage}");
                throw new Exception($"The remote server returned an error while retrieving the user profile. {errorCode} {errorMessage}");
            }
            var result = responseString.FromJson <BatchGetMaterialsJson>();

            return(result);
        }
コード例 #2
0
        public static async Task <MaterialCountJson> GetMaterialCount(string accessToken)
        {
            string api = "http://api.weixin.qq.com/cgi-bin/material/get_materialcount?access_token=ACCESSTOKEN";

            api = api.Replace("ACCESSTOKEN", accessToken);

            var client   = new HttpClient();
            var response = await client.GetAsync(api);

            if (!response.IsSuccessStatusCode)
            {
                //Logger.LogError($"An error occurred while retrieving the user profile: the remote server returned a {response.StatusCode} response with the following payload: {response.Headers.ToString()} {await response.Content.ReadAsStringAsync()}");
                throw new HttpRequestException("An error occured while retrieving the user profile.");
            }

            var payload   = JsonDocument.Parse(await response.Content.ReadAsStringAsync());
            int errorCode = WeixinApiHelper.GetErrorCode(payload);

            if (errorCode != 0)
            {
                var errorMessage = WeixinApiHelper.GetErrorMessage(payload);
                //Logger.LogError($"The remote server returned an error while retrieving the user profile. {errorCode} {errorMessage}");
                throw new Exception($"The remote server returned an error while retrieving the user profile. {errorCode} {errorMessage}");
            }


            int voiceCount, videoCount, imageCount, newsCount;

            if (MediaApiHelper.GetAllCounts(payload, out voiceCount, out videoCount, out imageCount, out newsCount))
            {
                return(new MaterialCountJson
                {
                    VoiceCount = voiceCount,
                    VideoCount = videoCount,
                    ImageCount = imageCount,
                    NewsCount = newsCount
                });
            }
            throw new Exception($"The remote server returned an unknown formatted string while retrieving the count of materials.");
        }