I'm sending some SMS using HttpWebRequest
. But some times the messages appear to send double, three times and more reviewing all the proccess appear to be ok.
The only thing i think is HttpWebRequest is working asynchronus.
here is my code,
public bool sendSmsGateway(string tcDestino, string tcMensaje, string tcTitulo = "")
{
bool lReturn = false;
string contenido = HttpUtility.UrlEncode(tcMensaje);
string lcTitulo = "SMS Sender";
if (!String.IsNullOrEmpty(tcTitulo))
{
lcTitulo = tcTitulo;
}
lcTitulo = HttpUtility.UrlEncode(lcTitulo);
string fechaEnvio = DateTime.Now.ToString("s");
string url = string.Format(StrSMSGatewayURL, StrSMSGatewayUsuario, StrSMSGatewayPassword, tcDestino, contenido, lcTitulo, fechaEnvio);
HttpWebRequest enviar = (HttpWebRequest)WebRequest.Create(url);
string respuesta = new StreamReader(enviar.GetResponse().GetResponseStream()).ReadToEnd();
StrSMSGatewayRespuesta = respuesta.Trim();
if (StrSMSGatewayRespuesta == StrSMSGatewayRespuestaOk.Trim())
{
lReturn = true;
}
return lReturn;
}
this code runs in loop routine that send the message and mark it as sent when:
string respuesta = new StreamReader(enviar.GetResponse().GetResponseStream()).ReadToEnd();
return the propper code.
My question is.. there is a way to stop the proccess, or force the code to wait until the (httpWebRequest send the message and get the response)
or (timeout succeed)