示例#1
0
 public AliyunSmsException(Exception exception,
                           string response, AliyunSmsResponse responseObject)
     : base("Failed to request api with action SendSms with response "
            + (response ?? string.Empty), exception)
 {
     ResponseContent = response;
     ResponseObject  = responseObject;
 }
示例#2
0
        private async Task <AliyunSmsResponse> InvokeAliyunSmsApiAsync(string signedParameters)
        {
            string            responseContent = null;
            AliyunSmsResponse smsResponse     = null;

            try
            {
                var request = new HttpRequestMessage(HttpMethod.Post, AliSmsHost)
                {
                    Content = new StringContent(signedParameters, Encoding.ASCII, "application/x-www-form-urlencoded")
                };
                request.Headers.UserAgent.TryParseAdd("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36");

                HttpResponseMessage response = await _httpClient.SendAsync(request, CancellationToken.None);

                responseContent = await response.Content.ReadAsStringAsync();

                smsResponse = JsonConvert.DeserializeObject <AliyunSmsResponse>(responseContent);
                return(smsResponse);
            }
            catch (HttpRequestException ex)
            {
                throw new AliyunSmsException(ex, responseContent, smsResponse);
            }
            catch (WebException ex)
            {
                try
                {
                    string resp;
                    using (var sr = new StreamReader(ex.Response.GetResponseStream()))
                    {
                        resp = sr.ReadToEnd();
                    }

                    throw new AliyunSmsException(ex, resp, null);
                }
                catch
                {
                    throw new AliyunSmsException(ex, null, null);
                }
            }
            catch (JsonException jsonException)
            {
                throw new AliyunSmsException(jsonException, responseContent, null);
            }
        }
示例#3
0
        public async Task SendVerificationCodeAsync(string phoneNumber, string code)
        {
            string smsTemplateParams = JsonConvert.SerializeObject(new { code });
            var    parameters        = new Dictionary <string, string>
            {
                { "PhoneNumbers", phoneNumber },
                { "SignName", _aliyunSmsOptions.SmsServiceSignName },
                { "TemplateCode", _aliyunSmsOptions.SmsServiceTemplateCode },
                { "TemplateParam", smsTemplateParams },
                { "OutId", Guid.NewGuid().ToString() }
            };
            string signed = SignRequestParameters(parameters);

            AliyunSmsResponse response = await InvokeAliyunSmsApiAsync(signed);

            if (response.Code != "OK")
            {
                throw new AliyunSmsException(null,
                                             response.Code + " " + response.Message,
                                             response);
            }
        }