0
Answer

send sms via web application

rinku kansara

rinku kansara

11y
1.1k
1
i have code to send sms via way2sms but i cant send it.
it generate neither error nor output.
pls help me

using System;
using System.Net;
using System.IO;
using System.Text;

public partial class sms : System.Web.UI.Page
{
    string ckuser, ckpass;
    string id, ps, mbno, mseg;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnsend_Click(object sender, EventArgs e)
    {
        try
        {
            id = txtuname.Text;
            ps = txtpwd.Text;
            mbno = txtmobileno.Text;
            mseg = txtmessage.Text;
            Session["id"] = id;
            Session["pw"] = ps;
            connect();
            send(mbno, mseg);
        }
        catch (Exception ex)
        {
            lblError.Text = ex.Message;
            lblError.Visible = true;
        }
    }

    private void send(string mbno, string mseg)
    {
        if ((mbno != "") && (mseg != ""))
        {
            try
            {
                Random rand = new Random();
                int rnum = rand.Next(1, 8);
                string url = "http://site" + rnum + ".way2sms.com/content/index.html";
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                req.Method = "POST";
                string str = "username=" + txtuname.Text + "&password=" + txtpwd.Text;
                req.ContentType = "application/x-www-form-urlencoded";

                string mssg = txtmessage.Text;
                string mbeno = txtmobileno.Text;

                req.ContentLength = str.Length;
                StreamWriter writer = new StreamWriter(req.GetRequestStream(), Encoding.ASCII);
                writer.Write(str);
                writer.Close();
                HttpWebResponse res = (HttpWebResponse)req.GetResponse();
                res.Close();
                lblError.Text = "Message Sent..... " + mbeno + ": " + mssg;
            }
            catch (Exception)
            {
                lblError.Text = "Error Sending msg....check your connection...";
            }
        }
        else
        {
            lblError.Text = "Mob no or msg missing";
        }
    }

    private void connect()
    {
        ckuser = Session["id"].ToString();
        ckpass = Session["pw"].ToString();
        try
        {
            Random rand = new Random();
            int rnum = rand.Next(1, 8);
            string url = "http://site" + rnum + ".way2sms.com/content/index.html";
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = "POST";
            string str = "username=" + txtuname.Text + "&password=" + txtpwd.Text;
            req.ContentType = "application/x-www-form-urlencoded";
            req.ContentLength = str.Length;
            string refer = "http://site" + rnum + ".way2sms.com/";
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            StreamReader reader = new StreamReader(res.GetResponseStream());
            String resstr = reader.ReadToEnd();
            Response.Write(resstr);
            reader.Close();
            Response.Write("Connect....");
        }
        catch (Exception e)
        {
            Response.Write("Error:" + e);
        }
    }
    protected void btnclr_Click(object sender, EventArgs e)
    {
        txtuname.Text = "";
        txtpwd.Text = "";
        txtmobileno.Text = "";
        txtmessage.Text = "";
    }
}