Beispiel #1
0
        /// <summary>
        /// Sends the message given.
        /// Note that it may be divided into several messages due to length limits.
        /// </summary>
        /// <param name="message">The message to send</param>
        /// <returns>A <see cref="NexmoResponse"/> object which contains information about the sent message(s)</returns>
        public NexmoResponse SendMessage(NexmoMessage message)
        {
            // Create a copy, since the NexmoMessage class is mutable.
            var copy = new NexmoMessage(message);

            var hwr = HttpWebRequest.Create(BaseUrl) as HttpWebRequest;
            hwr.Method = "POST";
            hwr.ContentType = "application/x-www-form-urlencoded";

            var postdatatxt = string.Format(CultureInfo.InvariantCulture,
                "username={0}&password={1}&from={2}&to={3}&text={4}",
                HttpUtility.UrlEncode(Username),
                HttpUtility.UrlEncode(Password),
                HttpUtility.UrlEncode(copy.From),
                HttpUtility.UrlEncode(copy.To),
                HttpUtility.UrlEncode(copy.Text));

            var postdata = Encoding.GetBytes(postdatatxt);

            hwr.ContentLength = postdata.LongLength;

            var stream = hwr.GetRequestStream();
            stream.Write(postdata, 0, postdata.Length);
            stream.Close();

            try
            {
                using (var response = hwr.GetResponse() as HttpWebResponse)
                {
                    try
                    {
                        var deserializer = new DataContractJsonSerializer(typeof(NexmoResponse));
                        return deserializer.ReadObject(response.GetResponseStream()) as NexmoResponse;
                    }
                    catch (Exception ex)
                    {
                        throw new Exception("Could not parse the json response from Nexmo", ex);
                    }
                }
            }
            catch (WebException ex)
            {
                throw new Exception("Could not fetch response from Nexmo", ex);
            }
        }
Beispiel #2
0
 /// <summary>
 /// Copy-constructor, needed since this class is mutable
 /// </summary>
 /// <param name="orig">The NexmoMessage to copy</param>
 internal NexmoMessage(NexmoMessage orig)
 {
     this.From = orig.From;
     this.To = orig.To;
     this.Text = orig.Text;
 }