Mudassir Hasan
Mudassir Hasan

Reputation: 28741

Ensure download was completed

I am using a for loop in which Webclient class Downloadstring() method is used to load a external url . The Url has been provided by SMS service provider in which Mobile number and the message to be transmitted is added.
But message is not being submitted to SMS gateway for all mobile numbers specified in phNos[] array ie downloading Url is skipped for some numbers . This occurs mostly for mobile numbers at end of array.

How can I ensure that program waits until url is loaded for particular number and then the program progresses forward.

WebClient cli = new WebClient();
for (i=0;i<phNos.Length;i++)
{
    string url = @"http://example.com?DestNo=" + phNos[i] + "&msg=" + message; 
    cli.DownloadString(url);  
}

Alternately I have also used System.Net.HttpWebRequest but the problem persist.

  for (i=0;i<phNos.Length;i++)
    {
        string url = @"http://example.com?DestNo=" + phNos[i] + "&msg=" + message; 
        Uri targetUri = new Uri(url);
        HttpWebRequest hwb = (HttpWebRequest)HttpWebRequest.Create(targetUri);

        System.Net.HttpWebResponse response = hwb1.GetResponse();
        int status = (int)response.StatusCode;

        if (status == 200)
        {

            Response.Write("Successfully transmitted" + status);
        }
    }

Is there any other alternative method to ensure message is submitted 100 %.

Upvotes: 0

Views: 218

Answers (2)

rene
rene

Reputation: 42414

I would instantiate a webclient for every call and dispose it after downloadstring is called, like so

foreach(var phone in phNos)
{
  using(WebClient cli= new WebClient())
  {
     url = String.Format(@"http://aaa.bbb.ccc.ddd?DestNo={0}&msg={1}", phone, message); 
     string result = cli.DownloadString(url); 
     // check if result has somekind of errorreport maybe? 
     Trace.WriteLine(result);  // optionally write it to a trace file
  }
}

Getting it disposed explicitely might help in also closing underlying networkconnections more quickly because I suspect the sheer number of connections are causing the issue. Throttling might also be an option (send less calls to the gateway per minute)

If this are 10000 or 100000 calls the network components between you and the sms gateway can be the culprit. Think off adsl modems/vpn software/routing issues or even the sms-gateway itself.

If that still doesn't resolve the issue: try Fiddler and or Wireshark to deeply inspect http traffic or even tcp/ip traffic.

Upvotes: 1

damix911
damix911

Reputation: 4443

Your code looks fine. DownloadString is blocking and if an error occurs it should raise an exception. How does the SMS gateway respond to your request? You should have a look at their documentation, because probably you can write a function that tests whether everything worked fine or not.

const int MAX_RETRY = 10;
WebClient cli= new WebClient();

for(i=0;i<phNos.Length;i++)
{
    url = @"http://aaa.bbb.ccc.ddd?DestNo=" + phNos[i] + "&msg=" + message;

    int cntRetry = 0;

    while (!TestResult(cli.DownloadString(url)) && cntRetry < MAX_RETRY)
        ++cntRetry;
}

The problem could be that you are submitting too many requests to the gateway in a very short time. You could try to put some Thread.Sleep(1000) calls somewhere and see if things get any better.

WebClient cli= new WebClient();
for(i=0;i<phNos.Length;i++)
{
    Thread.Sleep(1000);
    url = @"http://aaa.bbb.ccc.ddd?DestNo=" + phNos[i] + "&msg=" + message; 
    cli.DownloadString(url);  
}

You could also combine the two above examples, using maybe lower values for MAX_RETRY and Thread.Sleep.

const int MAX_RETRY = 5;
WebClient cli= new WebClient();

for(i=0;i<phNos.Length;i++)
{
    url = @"http://aaa.bbb.ccc.ddd?DestNo=" + phNos[i] + "&msg=" + message;

    int cntRetry = 0;

    while (!TestResult(cli.DownloadString(url)) && cntRetry < MAX_RETRY) {
        Thread.Sleep(500);
        ++cntRetry;
    }
}

Upvotes: 1

Related Questions