C# 响应。重定向仅发送1条消息
我正在为我们的应用程序编写一个短信功能。没有错误,但它没有达到我的期望。 我使用数据集获取多个手机号码,然后我需要向所有这些手机号码传递消息 1.使用响应。重定向仅发送1条消息,其他消息未发送。(发送第一条消息后,转到该页面) 下面是部分代码C# 响应。重定向仅发送1条消息,c#,c#-4.0,C#,C# 4.0,我正在为我们的应用程序编写一个短信功能。没有错误,但它没有达到我的期望。 我使用数据集获取多个手机号码,然后我需要向所有这些手机号码传递消息 1.使用响应。重定向仅发送1条消息,其他消息未发送。(发送第一条消息后,转到该页面) 下面是部分代码 DataSet DistDs = _distsms.GetAllDistributionList(UnitId, isShot, gameId, animalTypeId); if(DistDs.Tables[0].Rows.Count > 0) {
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;
}
}
}
}
Response.Redirect
就是这样做的-它重定向整个响应
对于您试图执行的操作,请使用
HttpWebRequest
将数据发送到远程服务器是一种糟糕的方式。尝试使用Web服务调用或Web Api调用。您可以在其中以JSON格式发送数据。AFIK您正在结束响应流
或者,您可以通过调用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 ();
请查看MSDN上的说明-可能这不是您需要的方法。现在还不清楚您试图用代码实现什么(其行为与您编写的代码完全相同)。@AlexeiLevenkov假设数据集返回2行,然后我接受这2个手机号码(msisdn)并使用response.redirect将mesage发送到所有这些号码。但只发送了1条消息..response.redirect不发送SMS或任何消息,它只会立即返回302 responce,并将您提供的位置作为参数。我不知道您是否需要使用此呼叫,但就您的示例而言,它的行为完全符合预期。可能您需要按照答案建议从服务器发送请求(使用
WebClient
/HttpWebRequest
)。。。