Example #1
0
        public ParsGreenResult <ResponseModel> InvokeSendSms(SendSmsModel model)
        {
            var url            = ParsGreenUrlConfig.GetSendUrl(_token, model.ToNumber, model.Body, _defaultNumber);
            var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);

            httpWebRequest.Method = "Get";
            HttpWebResponse httpResponse;

            try
            {
                httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            }
            catch
            {
                return(ParsGreenResult <ResponseModel> .Failed(new ParsGreenError
                {
                    Code = "-1000",
                    Description = "Could not send request."
                }));
            }
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream() ?? throw new InvalidOperationException()))
            {
                var result = streamReader.ReadToEnd();
                return(PrepairModel(result));
            }
        }
Example #2
0
        public async Task <ParsGreenResult <ResponseModel> > InvokeSendSmsAsync(SendSmsModel model)
        {
            var url = ParsGreenUrlConfig.GetSendUrl(_token, model.ToNumber, model.Body, _defaultNumber);

            if (model.ToNumber.Length != 11 || !model.ToNumber.StartsWith("09"))
            {
                return(ParsGreenResult <ResponseModel> .Failed(new ParsGreenError
                {
                    Code = "-1003",
                    Description = "Could not send message. Possibly not valid number."
                }));
            }

            if (string.IsNullOrWhiteSpace(model.Body))
            {
                return(ParsGreenResult <ResponseModel> .Failed(new ParsGreenError
                {
                    Code = "-1004",
                    Description = "Could not send empty message."
                }));
            }

            if (model.Body.Length > _maxLength)
            {
                return(ParsGreenResult <ResponseModel> .Failed(new ParsGreenError
                {
                    Code = "-1003",
                    Description = "Could not send message larger than 60 chars"
                }));
            }

            var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);

            httpWebRequest.Method = "Get";
            HttpWebResponse httpResponse;

            try
            {
                httpResponse = (HttpWebResponse)await httpWebRequest.GetResponseAsync();
            }
            catch
            {
                return(ParsGreenResult <ResponseModel> .Failed(new ParsGreenError
                {
                    Code = "-1000",
                    Description = "Could not send request."
                }));
            }
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream() ?? throw new InvalidOperationException()))
            {
                var result = streamReader.ReadToEnd();
                return(PrepairModel(result));
            }
        }