C# 使用way2sms api发送Sms

C# 使用way2sms api发送Sms,c#,asp.net,way2sms,C#,Asp.net,Way2sms,我想使用way2sms发送短信息。我尝试了以下代码 login.aspx.cs using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace WebApplication1 { public partial class Login : System.Web.

我想使用way2sms发送短信息。我尝试了以下代码

login.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
  public partial class Login : System.Web.UI.Page
  {
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnconnect_Click(object sender, EventArgs e)
    {
        Session["id"] = txtmobileno.Text;
        Session["pw"] = txtpw.Text;
        Response.Redirect("/send.aspx");
    }
  }
 }
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;

namespace WebApplication1
{
  public partial class send : System.Web.UI.Page
  {
    string mbno, mseg, ckuser, ckpass;
    private HttpWebRequest req;
    private CookieContainer cookieCntr;
    private string strNewValue;
    public static string responseee;
    private HttpWebResponse response;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["id"] == null && Session["pw"] == null)
        {
            Server.Transfer("login.aspx");
        }
        connect();

        try
        {

            lblError.Text = "";
            lblError.Visible = false;
            if (!(IsPostBack))
            {
                btnSend.Attributes.Add("onclick", "return Validate('" + txtTo.ClientID + "','" + txtMessage.ClientID + "');");
                txtMessage.Attributes.Add("onchange", "TextChange('" + txtMessage.ClientID + "','" + lblLeft.ClientID + "');");
                txtMessage.Attributes.Add("onkeyup", "TextChange('" + txtMessage.ClientID + "','" + lblLeft.ClientID + "');");
            }
        }
        catch (Exception ex)
        {
            lblError.Text = ex.Message;
            lblError.Visible = true;
        }
    }

    protected void btnSend_Click(object sender, EventArgs e)
    {
        try
        {
            mbno = txtTo.Text;
            mseg = txtMessage.Text;

            sendSms(mbno, mseg);
            txtTo.Text = "";
            txtMessage.Text = "";
        }
        catch (Exception ex)
        {
            lblError.Text = ex.Message;
            lblError.Visible = true;
        }
    }

    public void connect()
    {
        ckuser = Session["id"].ToString();
        ckpass = Session["pw"].ToString();

        try
        {
            this.req = (HttpWebRequest)WebRequest.Create("http://wwwd.way2sms.com/auth.cl");

            this.req.CookieContainer = new CookieContainer();
            this.req.AllowAutoRedirect = false;
            this.req.Method = "POST";
            this.req.ContentType = "application/x-www-form-urlencoded";
            this.strNewValue = "username=" + ckuser + "&password=" + ckpass;
            this.req.ContentLength = this.strNewValue.Length;
            StreamWriter writer = new StreamWriter(this.req.GetRequestStream(), Encoding.ASCII);
            writer.Write(this.strNewValue);
            writer.Close();
            this.response = (HttpWebResponse)this.req.GetResponse();
            this.cookieCntr = this.req.CookieContainer;
            this.response.Close();
            this.req = (HttpWebRequest)WebRequest.Create("http://wwwd.way2sms.com//jsp/InstantSMS.jsp?val=0");
            this.req.CookieContainer = this.cookieCntr;
            this.req.Method = "GET";
            this.response = (HttpWebResponse)this.req.GetResponse();
            responseee = new StreamReader(this.response.GetResponseStream()).ReadToEnd();
            int index = Regex.Match(responseee, "custf").Index;
            responseee = responseee.Substring(index, 0x12);
            responseee = responseee.Replace("\"", "").Replace(">", "").Trim();
            this.response.Close();

            pnlsend.Visible = true;
            lblErrormsg.Text = "connected";
        }
        catch (Exception ex)
        {
            lblErrormsg.Text = "Error connecting to the server...";
            Session["error"] = "Error connecting to the server...";
            lblError.Text = ex.ToString();

            lblError.Text= ex.ToString();
            //Server.Transfer("login.aspx");
        }
    }
    public void sendSms(string mbno, string mseg)
    {
        if ((mbno != "") && (mseg != ""))
        {
            try
            {
                this.req = (HttpWebRequest)WebRequest.Create("http://wwwd.way2sms.com//FirstServletsms?custid=");
                this.req.AllowAutoRedirect = false;
                this.req.CookieContainer = this.cookieCntr;
                this.req.Method = "POST";
                this.req.ContentType = "application/x-www-form-urlencoded";
                this.strNewValue = "custid=undefined&HiddenAction=instantsms&Action=" + responseee + "&login=&pass=&MobNo=" + this.mbno + "&textArea=" + this.mseg;

                string msg = this.mseg;
                string mbeno = this.mbno;

                this.req.ContentLength = this.strNewValue.Length;
                StreamWriter writer = new StreamWriter(this.req.GetRequestStream(), Encoding.ASCII);
                writer.Write(this.strNewValue);
                writer.Close();
                this.response = (HttpWebResponse)this.req.GetResponse();

                this.response.Close();
                lblErrormsg.Text = "Message Sent..... " + mbeno + ": " + msg;
            }
            catch (Exception)
            {
                lblErrormsg.Text = "Error Sending msg....check your connection...";
            }
        }
        else
        {
            lblErrormsg.Text = "Mob no or msg missing";
        }
    }

    protected void btnLogOut_Click(object sender, EventArgs e)
    {
        Session["id"] = null;
        Session["pw"] = null;
        Session["error"] = null;
        Server.Transfer("login.aspx");
    }

  }
}
send.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
  public partial class Login : System.Web.UI.Page
  {
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnconnect_Click(object sender, EventArgs e)
    {
        Session["id"] = txtmobileno.Text;
        Session["pw"] = txtpw.Text;
        Response.Redirect("/send.aspx");
    }
  }
 }
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;

namespace WebApplication1
{
  public partial class send : System.Web.UI.Page
  {
    string mbno, mseg, ckuser, ckpass;
    private HttpWebRequest req;
    private CookieContainer cookieCntr;
    private string strNewValue;
    public static string responseee;
    private HttpWebResponse response;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["id"] == null && Session["pw"] == null)
        {
            Server.Transfer("login.aspx");
        }
        connect();

        try
        {

            lblError.Text = "";
            lblError.Visible = false;
            if (!(IsPostBack))
            {
                btnSend.Attributes.Add("onclick", "return Validate('" + txtTo.ClientID + "','" + txtMessage.ClientID + "');");
                txtMessage.Attributes.Add("onchange", "TextChange('" + txtMessage.ClientID + "','" + lblLeft.ClientID + "');");
                txtMessage.Attributes.Add("onkeyup", "TextChange('" + txtMessage.ClientID + "','" + lblLeft.ClientID + "');");
            }
        }
        catch (Exception ex)
        {
            lblError.Text = ex.Message;
            lblError.Visible = true;
        }
    }

    protected void btnSend_Click(object sender, EventArgs e)
    {
        try
        {
            mbno = txtTo.Text;
            mseg = txtMessage.Text;

            sendSms(mbno, mseg);
            txtTo.Text = "";
            txtMessage.Text = "";
        }
        catch (Exception ex)
        {
            lblError.Text = ex.Message;
            lblError.Visible = true;
        }
    }

    public void connect()
    {
        ckuser = Session["id"].ToString();
        ckpass = Session["pw"].ToString();

        try
        {
            this.req = (HttpWebRequest)WebRequest.Create("http://wwwd.way2sms.com/auth.cl");

            this.req.CookieContainer = new CookieContainer();
            this.req.AllowAutoRedirect = false;
            this.req.Method = "POST";
            this.req.ContentType = "application/x-www-form-urlencoded";
            this.strNewValue = "username=" + ckuser + "&password=" + ckpass;
            this.req.ContentLength = this.strNewValue.Length;
            StreamWriter writer = new StreamWriter(this.req.GetRequestStream(), Encoding.ASCII);
            writer.Write(this.strNewValue);
            writer.Close();
            this.response = (HttpWebResponse)this.req.GetResponse();
            this.cookieCntr = this.req.CookieContainer;
            this.response.Close();
            this.req = (HttpWebRequest)WebRequest.Create("http://wwwd.way2sms.com//jsp/InstantSMS.jsp?val=0");
            this.req.CookieContainer = this.cookieCntr;
            this.req.Method = "GET";
            this.response = (HttpWebResponse)this.req.GetResponse();
            responseee = new StreamReader(this.response.GetResponseStream()).ReadToEnd();
            int index = Regex.Match(responseee, "custf").Index;
            responseee = responseee.Substring(index, 0x12);
            responseee = responseee.Replace("\"", "").Replace(">", "").Trim();
            this.response.Close();

            pnlsend.Visible = true;
            lblErrormsg.Text = "connected";
        }
        catch (Exception ex)
        {
            lblErrormsg.Text = "Error connecting to the server...";
            Session["error"] = "Error connecting to the server...";
            lblError.Text = ex.ToString();

            lblError.Text= ex.ToString();
            //Server.Transfer("login.aspx");
        }
    }
    public void sendSms(string mbno, string mseg)
    {
        if ((mbno != "") && (mseg != ""))
        {
            try
            {
                this.req = (HttpWebRequest)WebRequest.Create("http://wwwd.way2sms.com//FirstServletsms?custid=");
                this.req.AllowAutoRedirect = false;
                this.req.CookieContainer = this.cookieCntr;
                this.req.Method = "POST";
                this.req.ContentType = "application/x-www-form-urlencoded";
                this.strNewValue = "custid=undefined&HiddenAction=instantsms&Action=" + responseee + "&login=&pass=&MobNo=" + this.mbno + "&textArea=" + this.mseg;

                string msg = this.mseg;
                string mbeno = this.mbno;

                this.req.ContentLength = this.strNewValue.Length;
                StreamWriter writer = new StreamWriter(this.req.GetRequestStream(), Encoding.ASCII);
                writer.Write(this.strNewValue);
                writer.Close();
                this.response = (HttpWebResponse)this.req.GetResponse();

                this.response.Close();
                lblErrormsg.Text = "Message Sent..... " + mbeno + ": " + msg;
            }
            catch (Exception)
            {
                lblErrormsg.Text = "Error Sending msg....check your connection...";
            }
        }
        else
        {
            lblErrormsg.Text = "Mob no or msg missing";
        }
    }

    protected void btnLogOut_Click(object sender, EventArgs e)
    {
        Session["id"] = null;
        Session["pw"] = null;
        Session["error"] = null;
        Server.Transfer("login.aspx");
    }

  }
}
但它不起作用。这段代码中有我必须做的任何更改吗?
请告诉我从
桌面
应用程序或
网络
应用程序

发送短信的其他方法:

我不知道如何发送短信,但我找到的最简单的短信API是
代码也非常简单

public static void sendSMS(string Recepient, string Message)
{
    //Recepient is the cellno in string format
    StringBuilder sb = new StringBuilder();
    sb.Append("http://www.mymobileapi.com/api5/http5.aspx?");
    sb.Append("Type=sendparam");
    sb.Append("&username={yourusername}");//add your username here
    sb.Append("&password={yourpassword}");//add your password here
    sb.AppendFormat("&numto={0}", Recepient);
    string message = HttpUtility.UrlEncode(Message, ASCIIEncoding.ASCII);
    sb.AppendFormat("&data1={0}", message);

    try
    {
        ////Create the request and send data to the SMS Gateway Server by HTTP connection
        HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(sb.ToString());
        //Get response from the SMS Gateway Server and read the answer
        HttpWebResponse myResp = (HttpWebResponse)myReq.GetResponse();
        System.IO.StreamReader respStreamReader = new System.IO.StreamReader(myResp.GetResponseStream());
        string responseString = respStreamReader.ReadToEnd();
        respStreamReader.Close();
        myResp.Close();
    }
    catch (Exception ex)
    {

    }
}

检查这个我已经检查了这个网站。但在我的应用程序中,如果任何人在way2sms或任何其他SMS站点上没有帐户,则使用此应用程序,我将为他提供在way2sms站点上创建帐户的便利。一旦他创建了自己的帐户,他就可以使用自己的登录id和密码使用该应用程序发送短信。“但它不起作用”并不能很好地描述你所面临的问题。请发布您收到的任何错误/警告/异常消息。是的,请向我们显示错误消息。API文件在哪里?此文件目前未解决此问题all@Seabizkit那么你显然没有读完整的问题。“请告诉我从桌面应用程序或web应用程序发送短信的其他方式”@Seabizkit编辑。我同意-这不是一个有用的解决方案。