0

我需要从 asp.net 向不同的电话号码发送短信,但我完全不知道发送短信。但是通过谷歌我得到了这段代码:

   using System;
   using System.Collections.Generic;
   using System.Configuration;
   using System.Data;
   using System.Linq;
   using System.Web;
   using System.Web.Security;
   using System.Web.UI;
   using System.Web.UI.HtmlControls;
   using System.Web.UI.WebControls;
   using System.Web.UI.WebControls.WebParts;
   using System.Xml.Linq;
   using System.Net;


 namespace TESTING
  {
public partial class SendSMS : System.Web.UI.Page
{
    string uid;
    string password;
    string message;
    string no;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    public void send()
    {
    HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create("http://ubaid.tk/sms/sms.aspx?uid=" + uid + "&pwd=" + password + "&msg=" + message + "&phone=" + no + "&provider=fullonsms");
    HttpWebResponse myResp = (HttpWebResponse)myReq.GetResponse();
    System.IO.StreamReader respStreamReader = new System.IO.StreamReader(myResp.GetResponseStream());
    string responseString = respStreamReader.ReadToEnd();
    respStreamReader.Close();
    myResp.Close();
}

protected void Button1_Click(object sender, EventArgs e)
{
    try
    {
        uid = "";
        password = "";
        message = MessageTextBox.Text;
        no = MobileNumberTextBox.Text;
        send();
        MessageTextBox.Text = "";
        MobileNumberTextBox.Text = "";
    }
    catch (Exception ex)
    {
        ex.Message.ToString();            
    }

}

 }
 }

我有一个这样的asp代码:

  <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SendSMS.aspx.cs" Inherits="TESTING.SendSMS" %>

  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
   <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
     <title></title>
    </head>
  <body>
   <form id="form1" runat="server">
    <div>
      <asp:Label ID="Label1" runat="server" Text="Mobile No"></asp:Label>
       <asp:TextBox ID="MobileNumberTextBox" runat="server"></asp:TextBox>
        <br />
          <asp:Label ID="Label2" runat="server" Text="Message"></asp:Label>
            &nbsp;&nbsp;
            <asp:TextBox ID="MessageTextBox" runat="server" TextMode="MultiLine">                     </asp:TextBox>
        <br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:Button ID="Button1" runat="server" Text="Send Sms" OnClick="Button1_Click" />
      </div>
     </form>
   </body>

这段代码不适用于发送短信。我需要一个代码示例来发送短信。你的帮助对我来说意义重大。

4

2 回答 2

1

您没有将 message 、 password 、 no 、 uid 的文本框值传递给send()函数。aspx 文件看起来不错。在cs文件中进行如下更改。

为了减少代码,我用下载 url 的类替换HttpWebRequestWebClient类。
WebClient类提供了类提供的大部分功能 HttpWebRequest

有关两者之间的比较,请参阅此讨论线程。

   using System;
   using System.Collections.Generic;
   using System.Configuration;
   using System.Data;
   using System.Linq;
   using System.Web;
   using System.Web.Security;
   using System.Web.UI;
   using System.Web.UI.HtmlControls;
   using System.Web.UI.WebControls;
   using System.Web.UI.WebControls.WebParts;
   using System.Xml.Linq;
   using System.Net;


 namespace TESTING
{
public partial class SendSMS : System.Web.UI.Page
{
    string uid;
    string password;
    string message;
    string no;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    public void send(string message ,string no,string password,string uid )
    {

     using (WebClient cli = new WebClient())
    {
      url =@"http://ubaid.tk/sms/sms.aspx?uid=" + uid + "&pwd=" + password + "&msg=" + message + "&phone=" + no + "&provider=fullonsms";
      cli.DownloadString(url);

    }

    }

protected void Button1_Click(object sender, EventArgs e)
{
    try
    {
        uid = "";
        password = "";
        message = MessageTextBox.Text;
        no = MobileNumberTextBox.Text;
        send(message ,no,password,uid );
        MessageTextBox.Text = "";
        MobileNumberTextBox.Text = "";
    }
    catch (Exception ex)
    {
        ex.Message.ToString();            
    }

}

 }
 }

我使用了命名空间中WebClient存在的类,该类通过其函数System.Net下载字符串 url 中指定的网址 。DownloadString()

请参阅此处了解 WebClient 类

于 2013-10-04T06:10:22.660 回答
0

您需要获得更好的短信服务提供商。我已经检查了您在上面的示例中使用的Ubaid,它看起来像

a) 您没有提供用户名和密码,并且

b)这项服务并不可靠。

我看过他们网站上的评论,很多人说它不起作用。

尝试在 Google 上搜索SMS 提供程序 API

于 2013-10-04T11:55:59.843 回答