我正在为我们的应用程序编写一个SMS函数。没有错误,但它不符合我的期望。 使用数据集我得到多个手机号码,然后我需要将消息传递给所有这些手机号码。
1.使用Response.Redirect仅发送1条消息而其他消息未发送。(在发送第1条消息后,它将转到该页面)
下面的部分编码
DataSet DistDs = _distsms.GetAllDistributionList(UnitId, isShot, gameId, animalTypeId);
if(DistDs.Tables[0].Rows.Count > 0)
{
ContactNo = Convert.ToInt32(DistDs.Tables[0].Rows[0]["ContactNumber"]);
foreach (DataRow row in DistDs.Tables[0].Rows)
{
if (row["ContactNumber"].ToString() != "")
{
try
{
Response.Redirect("http://sms.gatewaysite.com/api/mt?msisdn=" + row["ContactNumber"].ToString() +
"&body=" + msgOut + "&sender=" + shortcode +
"&key=ertyertyer&product_id=4563456&operator=" + oppp + "&country=aaaaa");
}
catch(Exception ee)
{
string a = ee.Message;
//continue;
}
}
}
}
答案 0 :(得分:1)
Response.Redirect
就是这样 - 它会重定向整个响应。
对于您要执行的操作,请使用HttpWebRequest
答案 1 :(得分:0)
将数据发送到远程服务器是不好的方法。尝试使用Web服务调用或Web Api调用。您可以在哪里以JSON格式发送数据。 AFIK您要结束响应流。
或者您可以通过HTTP WebRequest
来自MSDN
WebRequest request = WebRequest.Create ("http://www.contoso.com/PostAccepter.aspx ");
// Set the Method property of the request to POST.
request.Method = "POST";
// Create POST data and convert it to a byte array.
string postData = "This is a test that posts this string to a Web server.";
byte[] byteArray = Encoding.UTF8.GetBytes (postData);
// Set the ContentType property of the WebRequest.
request.ContentType = "application/x-www-form-urlencoded";
// Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length;
// Get the request stream.
Stream dataStream = request.GetRequestStream ();
// Write the data to the request stream.
dataStream.Write (byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close ();
// Get the response.
WebResponse response = request.GetResponse ();
// Display the status.
Console.WriteLine (((HttpWebResponse)response).StatusDescription);
// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream ();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader (dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd ();
// Display the content.
Console.WriteLine (responseFromServer);
// Clean up the streams.
reader.Close ();
dataStream.Close ();
response.Close ();