0

I am sending a message to multiple phone numbers . Mobile numbers are stored in an array .

string phNums =  "91999999999,9199999998....";.
string[] phNos = phNums.Split(',');

But message doesn't reach all of the recipients , mostly to the numbers that are present near end of array. The message are sent via a URL provided by SMS service provider in which the phone number and the message is embedded.

 for (int i = 0; i < phNos.Length; i++)
  {
    url = @"http://aaa.bbb.ccc.dd/HTTPMTAPI?User=abc&Password=pqr&FromAddr=xyzSMS&DestNo=" + phNos[i] + "&msg=" + message;
    Uri targetUri1 = new Uri(url);
    System.Net.HttpWebRequest hwb1;
    hwb1 = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(targetUri1);
    hwb1.GetResponse();
  }

As an alternate , I also used Webclient() but still successful message delivery is not guaranteed.

  for (int i = 0; i < phNos.Length; i++)
  {
    WebClient cli= new WebClient();
  url = @"http://aaa.bbb.ccc.dd/HTTPMTAPI?User=abc&Password=pqr&FromAddr=xyzSMS&DestNo=" + phNos[i] + "&msg=" + message;
    cli.DownloadString(url);
  }

How to ensure that message delivery is not skipped . Like only if successful response is received on downloading the URL , the loop should progress to next mobile number and so on. If there is any other possible mechanism , please do suggest. Thanks

Mudassir Hasan
  • 28,083
  • 20
  • 99
  • 133
  • 1
    Are you sure you're not missing a query string parameter after the base URL and before the `phNos[i]` is added into the `url` string? Looks like the service you are trying to use operates based on query strings, because you have the `&` for the `msg` param, but no `?` or parameter name for the phone number. I tried to google for the service youre using, but every sms provider I found that uses the &msg parameter required phone number to be passed as a query string as well, not just a number appended to the base URL. – Unicorno Marley Feb 14 '13 at 04:11
  • Yes sir there is &msg parameter , I shortened the url for posting on forum..Please see updated url in post – Mudassir Hasan Feb 14 '13 at 04:27
  • 1
    you might need to actually read the response to see if you're getting an error, such as the service throttling you for sending too many numbers at a time. – Jimmy Feb 14 '13 at 04:45
  • Can I make the loop wait until I get response from loading url and then proceed to next mobile number.. – Mudassir Hasan Feb 14 '13 at 04:46

2 Answers2

2

I think this is what you want to do:

for (int i = 0; i < phNos.Length; i++)
{
    url = @"http://aaa.bbb.ccc.dd/HTTPMTAPI?User=abc&Password=pqr&FromAddr=xyzSMS&DestNo=" + phNos[i] + "&msg=" + message;
    Uri targetUri1 = new Uri(url);
    System.Net.HttpWebRequest hwb1;
    hwb1 = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(targetUri1);
    System.Net.HttpWebResponse response = hwb1.GetResponse();
    if (response != null)
        {
            int status = (int)response.StatusCode; // this changes the status 
                                                   // from text response to the
                                                   // number, like 404
            if (status == 404//or anything else you want to test//)
               {
                    // put your retry logic here, make sure you add a way to break 
                    // so you dont infinitely loop if the service is down or something
               }
        }
}
Unicorno Marley
  • 1,744
  • 1
  • 14
  • 17
  • what is the status code if url is loaded successfully so that i can test against it. – Mudassir Hasan Feb 14 '13 at 09:53
  • 1
    Here is a list of all the HTTP status codes: http://www.w3.org/Protocols/HTTP/HTRESP.html . Status 200 (OK) is *probably* what you want to test for, but I would recommend you step through the code a few times to see exactly what your service returns. Put the break point on `response != null` and `if (status =` and check the values in the debugger. – Unicorno Marley Feb 14 '13 at 14:08
0

URLs have a length constraint. Your are likely hitting this limit and your are losing the trailing phone numbers as a result. Your best bet would be to break your requests into multiple requests of a certain size.

According to the following SO, it may be good practice to limit your request so that the url does not surpass 2000 characters.
What is the maximum length of a URL in different browsers?

Community
  • 1
  • 1
Khan
  • 17,904
  • 5
  • 47
  • 59