我想发送SMS
给大量用户(4000 个用户),所以我将以下方法放在循环中:
protected int SendSMS(string url)
{
// Now to Send Data.
StreamWriter writer = null;
StringBuilder postData = new StringBuilder();
Uri myUri = new Uri(url);
postData.Append(HttpUtility.ParseQueryString(myUri.Query).Get("Username"));
postData.Append(HttpUtility.ParseQueryString(myUri.Query).Get("Password"));
postData.Append(HttpUtility.ParseQueryString(myUri.Query).Get("Sender"));
postData.Append(HttpUtility.ParseQueryString(myUri.Query).Get("Recipients"));
postData.Append(HttpUtility.ParseQueryString(myUri.Query).Get("MessageData"));
string webpageContent = string.Empty;
byte[] byteArray = Encoding.UTF8.GetBytes(postData.ToString());
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.ContentLength = webRequest.ContentLength = byteArray.Length;
writer = new StreamWriter(webRequest.GetRequestStream());
try
{
using (Stream webpageStream = webRequest.GetRequestStream())
{
webpageStream.Write(byteArray, 0, byteArray.Length);
}
using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse())
{
using (StreamReader reader = new StreamReader(webResponse.GetResponseStream()))
{
webpageContent = reader.ReadToEnd();
//TODO:parse webpagecontent: iF response contains "OK"
if (webpageContent.Contains("OK")) return 1;
else return 0;
}
}
//return 1;
}
catch (Exception ee)
{
ErrMapping.WriteLog(url);
string error = ee.Message + "<br><br>Stack Trace : " + ee.StackTrace;
ErrMapping.WriteLog(error);
return -1;
}
}
在特定数量的用户(如 65 个用户)之后,没有sms
为其余用户发送
我得到以下异常:
Error Message:Thread was being aborted.<br><br>Stack Trace : at System.Net.UnsafeNclNativeMethods.OSSOCK.recv(IntPtr socketHandle, Byte* pinnedBuffer, Int32 len, SocketFlags socketFlags)
at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags, SocketError& errorCode)
at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.PooledStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at System.Net.Connection.SyncRead(HttpWebRequest request, Boolean userRetrievedStream, Boolean probeRead)
at System.Net.ConnectStream.ProcessWriteCallDone(ConnectionReturnResult returnResult)
at System.Net.HttpWebRequest.CheckDeferredCallDone(ConnectStream stream)
at System.Net.HttpWebRequest.GetResponse()
at SendSMS_EmailUI.Frm_SMS_send.SendSMS(String url)