C# script: Send SMS with the HTTP API

This C# script sends an SMS messages from a C# forms application to the HTTP API of Diafaan SMS Server.

using System;
using System.Net;
using System.Web;
using System.IO;
using System.Windows.Forms;

namespace WindowsFormsApplication1 {
	public partial class Form1 : Form {
		public Form1()
		{
			InitializeComponent();
		}

		private void button1_Click(object sender, EventArgs e)
		{
			string result = SendMessage("192.168.0.10", 9710, "admin", "password", "+44xxxxxxxxxx", "My message");
			MessageBox.Show(result);
		}
		private string SendMessage(string host, int port, string userName, string password, string number, string message)
		{
			string result = "ERROR";

			// Create the base URI to send a message with the Diafaan SMS Server HTTP API
			string uri = "http://" + host + ":" + port.ToString() + "/http/send-message/";
			// Add the HTTP query to the base URI
			uri += "?username=" + HttpUtility.UrlEncode(userName);
			uri += "&password=" + HttpUtility.UrlEncode(password);
			uri += "&to=" + HttpUtility.UrlEncode(number);
			uri += "&message=" + HttpUtility.UrlEncode(message);

			// Send the HTTP request to Diafaan SMS Server
			WebRequest request = WebRequest.Create(uri);
			using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) {
				// Get the HTTP response from Diafaan SMS Server
				Stream dataStream = response.GetResponseStream();
				using (StreamReader reader = new StreamReader(dataStream)) {
					result = reader.ReadToEnd();
				}
			}
			return result;
		}
	}
}