Table of Contents [Hide/Show]
AuthenticationReturn valuesComplete examples Simple example for retrieving contact 123 as XML Retrieve, Add, Update and Delete
Intouch user: MrJohnson Intouch domain: acme.com Intouch password: abc123
string authInfo = Convert.ToBase64String(Encoding.Default.GetBytes("MrJohnson@acme.com:abc123")); webRequest.Headers["Authorization"] = "Basic " + authInfo;
//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()); }
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>"; }