posts - 0, comments - 1573, trackbacks - 0

Sending a CISCO IP Phone a simple command using C#

Below you will find a simple application to send a simple command via a CiscoIPPhoneText XML statement.

For more details on CISCOIPPhoneText and other XML commands see http://www.cisco.com/en/US/docs/voice_ip_comm/cuipph/all_models/xsi/6_0/english/programming/guide/XSIbook.html

Below is a screen shot of the output on a 7960 series phone

IPPhoneMsg

and the console application when running

image

 

HTTP Server Requests (HTTP POST)

The following description designates how an HTTP server request is made to the phone via an HTTP POST operation:

  1. The server performs an HTTP POST in response to a case-sensitive URL of the phone with this format: http://x.x.x.x/CGI/Execute, where x.x.x.x represents the IP address of the destination Cisco Unified IP Phone.

    The form that is posted should have a case-sensitive form field name called "XML" that contains the desired XML object. For any HTTP POST operation, the server must provide basic HTTP authentication information with the POST. The provided credentials must be of a user in the global directory with a device association with the target phone.

    If the credentials are invalid, or the Authentication URL is not set properly in the Cisco Unified Communications Manager Administration, the phone will return a CiscoIPPhoneError with a value of 4 (Authentication Error) and processing will stop.
  2. The phone processes the supported HTTP headers
  3. The phone parses and validates the XML object
  4. The phone presents data and options to the user, or in the case of a CiscoIPPhoneExecute object, begins executing the URIs.

 

APP.CONFIG

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="CallManagerPushUsername" value="ccrowe-test"></add>
    <add key="CallManagerPushPassword" value="123456"></add>
  </appSettings>

</configuration>

 

Program.CS

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Web;

namespace TestApplication_SendSimpleMessageToPhone
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Send a simple XML Command to a CISCO IP Phone");
            Console.WriteLine();

            string IPAddressOfPhone = "10.3.21.228";
            int HTTPServerTimeoutMS = 30000;
            string PhoneURL = string.Format("http://{0}/CGI/Execute", IPAddressOfPhone);
            string PushXML = "";
            string ResponseXML = "";
            string ErrorCode = "";

            PushXML = @"<CiscoIPPhoneText>";
            PushXML += @"<Title>Title Text Goes Here</Title>";           // Optional Field
            PushXML += @"<Prompt>The prompt text goes here</Prompt>";    // Optional Field
            PushXML += @"<Text>The text to be displayed as the message body</Text>";

            PushXML += @"<SoftKeyItem>"; // Optional Field
            PushXML += @"<Name>Cancel</Name>";
            PushXML += @"<URL>SoftKey:Cancel</URL>";
            PushXML += @"<Position>4</Position>";
            PushXML += @"</SoftKeyItem>";

            PushXML += @"</CiscoIPPhoneText>";

            Console.WriteLine("Sending the following POST data:");
            Console.WriteLine();
            Console.WriteLine(PushXML);
            Console.WriteLine();

            PushXML = "XML=" + HttpUtility.UrlEncode(PushXML);


            WebResponse resp = null;
            try
            {
                ServicePointManager.Expect100Continue = false;
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(PhoneURL);
                req.Timeout = HTTPServerTimeoutMS;
                req.Method = "POST";
                req.Credentials = GetCredentials();
                req.Accept = "*/*";
                req.ContentType = "application/x-www-form-urlencoded";
                req.KeepAlive = false;
                req.Expect = "";

                byte[] bytes = null;
                bytes = System.Text.Encoding.UTF8.GetBytes(PushXML);
                Stream outputStream = req.GetRequestStream();
                outputStream.Write(bytes, 0, bytes.Length);
                outputStream.Close();

                resp = req.GetResponse();
                Stream s = resp.GetResponseStream();
                StreamReader stm = new StreamReader(s);
                if (stm == null)
                {
                    ErrorCode = "Timed out or no response!";
                }
                else
                {
                    ResponseXML = stm.ReadToEnd();
                    stm.Close();
                }
                s.Close();
                resp.Close();
            }
            catch (System.Net.WebException ex)
            {
                string StatusCodeString = ex.Response == null ? "" : (ex.Response as HttpWebResponse).StatusCode.ToString();
                int StatusCodeNumber = ex.Response == null ? -1 : Convert.ToInt32((ex.Response as HttpWebResponse).StatusCode);

                ErrorCode = string.Format("Web Exception : {0}\r\n\r\n" +
                                          "HTTP Status : {1} ({2})\r\n\r\n" +
                                          "Stack Trace:\r\n\r\n{3}",
                                          ex.Message.ToString(),
                                          StatusCodeString, StatusCodeNumber,
                                          ex.ToString());
            }
            catch (Exception ex)
            {
                ErrorCode = string.Format("Exception : {0}\r\n\r\n" +
                                          "Stack Trace:\r\n{1}",
                                          ex.Message.ToString(),
                                          ex.ToString());
            }
            if (ErrorCode.Length > 0)
                Console.WriteLine(ErrorCode);
            else
            {
                Console.WriteLine();
                Console.WriteLine("Received the following XML response");
                Console.WriteLine();
                Console.WriteLine(ResponseXML);
            }
            Console.WriteLine();
            Console.WriteLine("Completed... click ENTER to exit");
            Console.ReadLine();
        }

        private static ICredentials GetCredentials()
        {
            string Username = System.Configuration.ConfigurationManager.AppSettings["CallManagerPushUsername"];
            string Password = System.Configuration.ConfigurationManager.AppSettings["CallManagerPushPassword"];
            return new NetworkCredential(Username, Password);
        }
    }
}

Print | posted on Tuesday, October 28, 2008 12:06 PM |

Feedback

Gravatar

# re: Sending a CISCO IP Phone a simple command using C#

I am new with the Call Manager/IP phone and your article was a great kick for getting started but I still have some problems running this application.

my phone doesn't require an authentication, but still I keep on receiving an error authorization message:
"<CiscoIPPhoneError Number=\"4\" />"

can you help me?



11/4/2008 4:15 AM | inadler
Gravatar

# re: Sending a CISCO IP Phone a simple command using C#

is there a way to start a phone call using this method?
5/30/2009 12:18 AM | Marcuzzo
Gravatar

# re: Sending a CISCO IP Phone a simple command using C#

Hi I used your sample for CiscoIPPhoneExecute with
PushXML = @"<CiscoIPPhoneExecute>";
PushXML += @"<ExecuteItem URL='http://....any URL' Priority='0' />";
PushXML += @"</CiscoIPPhoneExecute>";
All works fine.
But when I change URL to URI command, for example
PushXML += @"<ExcecuteItem URL=\""Init:AppStatus\"" Priority=\""0\"" />";
it always failed with error 1
Please help, I think exist problem with encoding of URI
Thanks
6/16/2009 7:00 PM | Vladimir

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 6 and 5 and type the answer here:

Powered by:
Powered By Subtext Powered By ASP.NET