This page contains clientside code that demonstrates how to use the
Intouch REST API with C#.
Authentication
Authentication is done with basic http authentication. The authentication header must be on the format user@logondomain:password. For the following user:
Intouch user: MrJohnson
Intouch domain: acme.com
Intouch password: abc123
the code to construct the authorization header should look like this:
string authInfo = Convert.ToBase64String(Encoding.Default.GetBytes("MrJohnson@acme.com:abc123"));
webRequest.Headers["Authorization"] = "Basic " + authInfo;
Return values
Return values is specified by setting the Accept and Content-type headers to specify that you want to send and receive XML (or JSon) data. For XML, use "application/xml". For JSON, use "application/json".
Complete examples
Simple example for retrieving contact 123 as XML
//Create webrequest
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("http://intouchapi.pswin.com/1/contacts/123");
webRequest.ContentType = "application/xml";
webRequest.Accept = "application/xml";
webRequest.Method = "GET";
//Add basic authorization
string authInfo = Convert.ToBase64String(Encoding.Default.GetBytes("MrJohnson@acme.com:abc123"));
webRequest.Headers["Authorization"] = "Basic " + authInfo;
//Retrieve group
using (WebResponse response = webRequest.GetResponse())
using (Stream responseStream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(responseStream))
{
Console.WriteLine(reader.ReadToEnd());
}
Retrieve, Add, Update and Delete¶
public static void GetGroup51()
{
var request = PrepareRequest("http://intouchapi.pswin.com/1/groups/51", "GET");
Console.WriteLine(GetResponseFromServer(request));
}
public void EditGroup51()
{
var request = PrepareRequest("http://intouchapi.pswin.com/1/groups/51/", "PUT");
WriteRequestToServer(request, GetGroupAsXML());
Console.WriteLine(GetResponseFromServer(request));
}
public void AddNewGroup()
{
var request = PrepareRequest("http://intouchapi.pswin.com/1/groups/", "POST");
WriteRequestToServer(request, GetGroupAsXML());
Console.WriteLine(GetResponseFromServer(request));
}
public void DeleteGroup51()
{
var request = PrepareRequest("http://intouchapi.pswin.com/1/groups/51/", "DELETE");
WriteRequestToServer(request, ""));
Console.WriteLine(GetResponseFromServer(request));
}
private static WebRequest PrepareRequest(string url, string httpMethod)
{
//Create webrequest
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.ContentType = "application/xml";
webRequest.Accept = "application/xml";
webRequest.Method = httpMethod;
//Add basic authorization
string authInfo = Convert.ToBase64String(Encoding.Default.GetBytes("MrJohnson@acme.com:abc123"));
webRequest.Headers["Authorization"] = "Basic " + authInfo;
return webRequest;
}
private static void WriteRequestToServer(WebRequest webRequest, string parameters)
{
byte[] bytes = Encoding.ASCII.GetBytes(parameters);
webRequest.ContentLength = bytes.Length;
using (Stream os = webRequest.GetRequestStream())
{
os.Write(bytes, 0, bytes.Length);
os.Close();
}
}
private static string GetResponseFromServer(WebRequest webRequest)
{
using (WebResponse response = webRequest.GetResponse())
using (Stream responseStream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(responseStream))
{
return reader.ReadToEnd();
}
}
private static string GetGroupAsXML()
{
return "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
"<Group xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.datacontract.org/2004/07/PSWinCom.Intouch.API.Resources\">" +
"<Description></Description>" +
"<GroupId>51</GroupId>" +
"<IsPrivate>false</IsPrivate>" +
"<Name>min gruppe</Name>" +
"<OrganizationId>1234</OrganizationId>" +
"<UserId>5678</UserId>" +
"</Group>";
}