HttpWebRequests + Window service.

mike55

Contributor
Joined
Mar 26, 2004
Messages
727
Location
Ireland
Hi

My windows service is capable of connecting to a third party service provider, www.clickatell.com in this case. I am using the third party to send out a number of sms text messages in the event that another service goes down or fails to reply within a specific timeframe. I am using a List<string> to store the list of number that I am sending to, and passing my message as a string.

The problem that I am having is that when I go to send the last message, I get a timeout exception. I have tried to get the service to sleep for 15 seconds before sending each message, but this has had no effect.

Here is the code that I am using:
Code:
        //Queue the message to the EMS personnel, and send it.
        private static void QueueEMSMessages(ClubtextMonitor.Modules.MonitorConnect myConnection, String sysMessage)
        {
            //Get the person who is to receive the message.
            List<string> msgDestination = myConnection.GetTechSupport();

           //Create the message and send it to the support person.
            foreach (string ems in msgDestination)
            {
                try
                {
                    ClubtextMonitor.Modules.Message errMessage = new ClubtextMonitor.Modules.Message(sysMessage, ems);
                    //Wait for 15 seconds before sending the message
                    System.Threading.Thread.Sleep(15000);
                    errMessage.sendMessage();
                }
                catch (Exception ex)
                {
                }
            }
        }

 //Allow the client to send the message for the user.
        public bool sendMessage()
        {
            //Parameter for storing the send message credentails
            string[] credentials;

            //Get the credentials necessary for sending the message.
            ClubtextMonitor.Modules.MonitorConnect connection = new ClubtextMonitor.Modules.MonitorConnect();
            credentials = connection.GetCredentials();

            bool outputValue = false;

            //Ensure that the correct number of credentials have been supplied.
            if (3 == credentials.Length)
            {
                outputValue = true;
                WebRequest objRequest;

                //The web request to be sent to the message gateway.
                string msgRequest;
                msgRequest = "http://api.clickatell.com/http/sendmsg?user=" + credentials[1].ToString() +
                                    "&password=" + credentials[2].ToString() + "&api_id=" + 
                                        credentials[0].ToString() +"&to=" + msgDestination 
                                            + "&text=" + msgText;

                //Send the message to the individual on tech. support.
                objRequest = System.Net.HttpWebRequest.Create(msgRequest);
                objRequest.GetResponse();
            }
            else
            {
                outputValue = false;
            }
            
            return outputValue;
        }

Mike55.
 
Not a clue if this will help but you are creating a response object for each call of the sendMessage method that you are never using - if you try either calling response.Close() before exiting the method or just not creating the response does that fix the problem?
 
Thanks for the reply PlausiblyDamp, I should have mentioned that my two methods are located in two different objects so I need to createa response object. Based on what you are saying, I should locate this line to outside the foreach loop and only create one object instead of three. I will also try the response.Close() option.

Again many thanks.

Mike55.
 
Back
Top