0

We want to split messages into multiple (sms)text messages if the characters exceed 136.

But we also want to make sure that the second message arrives after the first, and before third.

So far, from the few tests We've done, it seems to work like this, but to make sure we still want to add a delay of maybe 3 seconds or so.

is Thread.Sleep the correct way to do it, or is there another solution than adding a delay?

This is the code :

    // Split message to send them as individual text messages
    string[] messages = splitMessage(request["message"], 132);
    for (int i = 0; i < messages.Length; i++)
    {
        HttpWebRequest _wr = (HttpWebRequest)WebRequest.Create(url);

        // Use the CredentialCache so you can attach the authentication to the request
        CredentialCache mycache = new CredentialCache { { new Uri(url), "Basic", new NetworkCredential(_username, _password) } };

        // use Basic Authentication
        _wr.Credentials = mycache;
        _wr.Method = "POST";
        _wr.ContentType = "application/json";
        _wr.Timeout = -1;   // not sure if necessary but it's not harmful

        // _data variable holds the data to be pushed to the server in the request.
        // this piece of data holds the request to send a text message from the application
        string _data = "{\"outboundSMSMessageRequest\":" +
                        "{\"address\": \"" + formatedAddress + "\",\"requestId\": \"" + request["requestId"] + "\"" +
                        ",\"outboundSMSTextMessage\": \"" + messages[i] + "\",\"senderAddress\": \"" + request["sender"] + "\"" +
                        _optional + "}}";

        //get a reference to the request-stream, and write the POST data (_data) to it
        using (var s = new StreamWriter(_wr.GetRequestStream()))
        {
                s.Write(_data);
        }

        //get response-stream, and use a streamReader to read the content
        try
        {
                HttpWebResponse _response = (HttpWebResponse)_wr.GetResponse();
        }
        catch (WebException ex)
        {
                using (StreamWriter file = new StreamWriter(@"D:\WEB\SMS\jsonResponse.txt", true))
                {
                        file.WriteLine("No good");
                }
                // Log exception and throw as for GET example above
                Trace.WriteLine("No good");
                Trace.WriteLine(ex);
        }

        using (Stream s = _wr.GetResponse().GetResponseStream())
        {
                using (StreamReader sr = new StreamReader(s))
                {
                        var jsonData = sr.ReadToEnd();
                        Trace.WriteLine("Server response: " + jsonData);
                        //decode jsonData with javascript serializer

                        using (StreamWriter file = new StreamWriter(@"D:\WEB\SMS\jsonResponse.txt", true))
                        {
                                file.WriteLine("Server response: " + jsonData);
                        }
                }
        }
}
user3542112
  • 253
  • 1
  • 4
  • 13
  • Yes, your solution is fine. However, it would be better if you could assign a sequence number to each chunk so there won't be any lost data and you can sort it on the receive program. – brz Oct 27 '14 at 16:02
  • unfortunately we are posting json to a 3rd party and there is no sequence number parameter. (but we are adding 1/3 2/3 3/3 in the message for the user) – user3542112 Oct 27 '14 at 16:07
  • Either the third party API will send the messages in the order they are received, in which case you just need to send them sequentially without a delay, or it doesn't in which case introducing a delay is probably no more reliable than just sending them sequentially any way. – Ben Robinson Oct 27 '14 at 16:10

1 Answers1

0

I'd rather check the documentation of your sms sender API, as it may be written there that they do not guarantee the order of sms messages - they can have inner queue and the response from the server may mean that they have enqueued the messege, not that they have delivered it. In this case adding a delay of 3, 5 or even 10 seconds won't help.

ZuoLi
  • 383
  • 2
  • 14
  • Its also the provider of the user. It can very well happen, that everything is in order until it reaches it "last hop" and then get "rearranged" there => you can NEVER guarantee the order of an SMS (in a reasonable short amount of time)... – Christoph Fink Oct 27 '14 at 16:17
  • Yes, I agree: if you rely on some 3d party who deliveres messages, you can not be sure of how it is done. – ZuoLi Oct 27 '14 at 16:20
  • And if you do not have your own phone-network it can't be done without one ;-)... – Christoph Fink Oct 27 '14 at 16:47
  • From the tests I've done so far it seems each message is delivered about 1 minute after the other in order(even without adding a delay). The documentation does not mention sequence/order or multiple text messaging. So we have no choice but to assume that sometimes it can be unordered. – user3542112 Oct 27 '14 at 17:44
  • It seems my carrier is the one sending sms 1 minute later, where as another carrier is instant, i'm going to accept this answer as the order is most likely not reliable. (Although it seems to be in order most of the time) – user3542112 Oct 27 '14 at 21:00