Hello Friends,

I am sending 5 sms from my asp.net application in c# at a time using HttpWebRequest. Its running fine but sometimes sms sending fails becoz of this error:

Error : The underlying connection was closed: An unexpected error occurred on a receive. 

Code for sending sms:
I tried webreq.KeepAlive = true; but still it doesn't work...

-----------------------------------------------------------------------------------------------------------
Page curent = new Page();
string SmsUrl = url + parameter;
HttpWebRequest webreq = WebRequest.Create(SmsUrl) as HttpWebRequest;
webreq.ContentType = "application/x-www-form-urlencoded";//"text/html; charset=UTF-8";
webreq.Method = "POST";
byte[] bytes = Encoding.ASCII.GetBytes(parameter);
webreq.Timeout = 10000000;
webreq.KeepAlive = true;
Stream os = null;

WebResponse webresponce = webreq.GetResponse();
if (webresponce == null)
{
  return null;
}
StreamReader sr = new StreamReader(webresponce.GetResponseStream());
res = sr.ReadToEnd();
Console.WriteLine(res);
return res;

-----------------------------------------------------------------------------------------------------------

Please advice something...

Recommended Answers

All 3 Replies

I would suggest using an exception handler(Try-Catch)

I have already used try catch block statement.

In catch block only it give me error.

Error : The underlying connection was closed: An unexpected error occurred on a receive.

When you catch the error you could try to restart the connection, and retry sending whichever one failed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.