- Code: Select all
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Security;
using System.IO;
using System.Xml;
namespace KES.SwitchVox
{
/* += SwitchVox .NET API =+
* www.keslabs.com
*
* Created in 45 minutes... you've been warned.
*
* // GENERIC API USAGE ---
NameValueCollection params = new NameValueCollection();
myCol.Add("name", "value");
myCol.Add("name", "value");
myCol.Add("name", "value");
SwitchVoxAPI api = new SwitchVoxAPI("myhostnameOrIP", "myUser", "myPass");
if (api.Request("one.of.the.methods", params))
Console.Write(api.Response);
// SPECIFIC API USAGE ---
private int? getUserAccountId(string ext)
{
NameValueCollection parameters = new NameValueCollection();
SwitchVoxAPI api = new SwitchVoxAPI("192.168.0.1", ext, "myPassword");
if (api.Request("switchvox.users.getMyInfo", parameters))
{
int accountId;
XDocument document = XDocument.Parse(api.Response, LoadOptions.None);
if (int.TryParse(document.Descendants("extension").First().Attribute("account_id").Value, out accountId))
{
return accountId;
}
else
{
return null;
}
}
else
{
return null;
}
}
*/
public class SwitchVoxAPI
{
public string Username;
public string Password;
public string Hostname;
public Uri Uri
{
get
{
return new Uri(string.Format("https://{0}/xml", Hostname));
}
}
public string Response
{
private set;
get;
}
public SwitchVoxAPI(string hostname, string username, string password)
{
Hostname = hostname;
Username = username;
Password = password;
}
public Boolean Request(string method, NameValueCollection arguments)
{
if (method == null || method == string.Empty)
throw new NullReferenceException();
if (arguments == null)
throw new NullReferenceException();
try
{
XmlDocument doc = new XmlDocument();
XmlElement request = doc.CreateElement("request");
request.SetAttribute("method", method);
XmlElement parameters = doc.CreateElement("parameters");
foreach (String name in arguments.AllKeys)
{
XmlElement arg = doc.CreateElement(name);
arg.InnerText = arguments[name];
parameters.AppendChild(arg);
}
request.AppendChild(parameters);
doc.AppendChild(request);
return Send(doc.OuterXml);
}
catch
{
return false;
}
}
public Boolean Send(string xmlPackage)
{
try
{
if (Hostname == null || Hostname == string.Empty)
throw new NullReferenceException();
if (Username == null || Username == string.Empty)
throw new NullReferenceException();
if (Password == null || Password == string.Empty)
throw new NullReferenceException();
Response = string.Empty;
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(Uri);
CookieContainer cookies = new CookieContainer();
// allows for validation of SSL conversations -- Only for trusted hosts!
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
CredentialCache creds = new CredentialCache();
creds.Add(Uri, "Digest", new NetworkCredential(Username, Password));
request.Credentials = creds;
request.Method = WebRequestMethods.Http.Post;
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; GTB6.3; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
request.PreAuthenticate = true;
request.CookieContainer = cookies;
request.ContentLength = xmlPackage.Length;
request.ContentType = "application/xml";
StreamWriter writer = new StreamWriter(request.GetRequestStream());
writer.Write(xmlPackage);
writer.Close();
HttpWebResponse oResponse = (HttpWebResponse)request.GetResponse();
if (oResponse.StatusCode != HttpStatusCode.OK)
{
return false;
}
StreamReader reader = new StreamReader(oResponse.GetResponseStream());
Response = reader.ReadToEnd();
// Check for result or error
// response > result
// response > err
oResponse.Close();
return true;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message + ex.ToString());
return false;
}
}
}
}
