C# samples

This document contains several "how to" samples written in C# using standard .NET libraries

Build Authentication header

According to Authentication rules, any HTTP requests must contain a authorization header. This sample contains a function which returns a valid authorization string from user name and password (or token).

public string GetAuthorizationHeader(string UserName, string PasswordOrToken)
{ if(string.IsNullOrEmpty(UserName))
{ throw new ArgumentNullException("UserName"); }

if(string.IsNullOrEmpty(PasswordOrToken))
{ throw new ArgumentNullException("PasswordOrToken"); }

// password must not contain ':' (colon)
if(PasswordOrToken.IndexOf(":") > -1)
{ throw new ArgumentException("PasswordOrToken cannot contain colon ':' "); }

// build plain string - ie: "john.doe@profit365.eu:SamplePassword123"
var _plain = string.Format("{0}:{1}", UserName, PasswordOrToken);

// get plain text bytes
var _bytes = System.Text.Encoding.UTF8.GetBytes(_plain);

// convert bytes back to string using Base64 encoding
return System.Convert.ToBase64String(_bytes);
}

Get list of invoices

Create simple HTTP request, append Authentication headers and properly send request.

// create new HTTP request
var request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create("https://api.profit365.eu/1.6/sales/invoices/1");

// set all authentication data to request
request.Headers["Authorization"] = GetAuthorizationHeader("YourUserName", "YourPasswordOrToken") ;
request.Headers["ClientID"] = "";
request.Headers["ClientSecret"] = "";
request.Headers["CompanyID"] = Guid.Parse("");

System.Net.HttpWebResponse _response = null;
System.IO.Stream _dataStream = null;
System.IO.StreamReader _reader = null;

try
{
// sent request to API and retrieve response
_response = (System.Net.HttpWebResponse)request.GetResponse();
_dataStream = _response.GetResponseStream();
_reader = new System.IO.StreamReader(_dataStream);
var _responseString = _reader.ReadToEnd();

Console.Write(_responseString);
}
catch (System.Net.WebException _ex)
{

_response = (System.Net.HttpWebResponse)_ex.Response;
_dataStream = _response.GetResponseStream();
_reader = new System.IO.StreamReader(_dataStream);
var _responseString = _reader.ReadToEnd();

Console.Write(_responseString);
}
finally
{
_reader.Close();
_response.Close();
_dataStream.Close();
}