Skip to content

October 19, 2013

Version 2 ,Sample Code for Send sms via Bulk SMS API Using C#

Modified version of old post “Sample Code for Send sms via Bulk SMS API Using C#

Signup from a bulk sms provider.and modify the code according to that http api link.

 

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

public partial class Default2 : System.Web.UI.Page
{
   // public string api = "mydomain.co.in/sendsms/myusername/mypassword/SenderID/number(s)/message/route";

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnClear_Click(object sender, EventArgs e)
    {
        txtName.Text = "";
        txtPassword.Text = "";
        txtDomain.Text = "";
        txtSenderId.Text = "";
        txtMobile.Text = "";
        txtMessage.Text = "";
    }
    protected void btnSend_Click(object sender, EventArgs e)
    {
        try
        {
          string  mobile = txtMobile.Text;
          string message = txtMessage.Text;
          string username = txtName.Text;
          string password = txtPassword.Text;
          string _sender = txtSenderId.Text;
          string domian = txtDomain.Text;
          string route = txtrout.Text;

          string api = "http://" + domian + "/sendsms/" + username + "/" + password + "/" + _sender + "/" + mobile + "/" + message + "/" + route + "";
          Response.Write(apiFunction(api));
        }
        catch
        {
        }
    }
    protected String apiFunction(string strapi)
    {
        try
        {
            string url = strapi;
            HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
            string getDetails = "Argument=SomeTestArguments";
            ASCIIEncoding sendEncoding = new ASCIIEncoding();
            byte[] byte1 = sendEncoding.GetBytes(getDetails);
            myHttpWebRequest.Method = "POST";
            myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
            myHttpWebRequest.ContentLength = getDetails.Length;
            Stream sendStream = myHttpWebRequest.GetRequestStream();
            sendStream.Write(byte1, 0, byte1.Length);
            sendStream.Close();
            WebResponse myWebResponse = myHttpWebRequest.GetResponse();
            Stream ReceiveStream = myWebResponse.GetResponseStream();
            Encoding readEncoding = System.Text.Encoding.GetEncoding("utf-8");
            StreamReader readStream = new StreamReader(ReceiveStream, readEncoding);
            Char[] read = new Char[256];
            int count = readStream.Read(read, 0, 256);
            String str = "";
            if (count > 0)
            {
                str = new String(read, 0, count);
            }
            readStream.Close();
            myWebResponse.Close();
            return str;// this is the response
        }
        catch (Exception exp)
        {
            string error_msg = exp.Message;
            return "failed";
        }
    }

}

Untitled-1

Demo         Download

Read more from C#.NET