Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 4 Next »

When writing clients in C# to consume the API, You can use the ServiceStack client (easy way) or use the .NET web client (harder way).

Using the ServiceStack Client

  1. Install the ServiceStackVS extension
  2. Add the nuget package ServiceStack.Client
  3. Add References to System.Runtime.Serialization and System.Net
  4. Right-click on the project and choose "Add ServiceStack Reference..."
  5. Enter the address of the host and name the service
  6. Add the following Code:

    Using ServiceStack.Client
    var client = new ServiceStack.JsonServiceClient("http://api.jiwa.com.au");
    var authResponse = client.Send<ServiceStack.AuthenticateResponse>(new ServiceStack.Authenticate()
    {
    	provider = "credentials",
    	UserName = "api",
    	Password = "password",
    	RememberMe = true
    });
    
    // Read a debtor
    var serviceStackDebtorResponse = client.Get(new DebtorGetRequest { AccountNo = "1001" });

Using the .NET WebClient

  1. Add References to System.Runtime.Serialization and System.Net
  2. Add the nuget package Newtonsoft.Json
  3. Create a cookie aware WebClient using this code:

    Cookie Aware WebClient
    public class CookieAwareWebClient : WebClient
    {
    	public CookieAwareWebClient()
    	{
    		CookieContainer = new CookieContainer();
    	}
    	public CookieContainer CookieContainer { get; private set; }
    
    	protected override WebRequest GetWebRequest(Uri address)
    	{
    		var request = (HttpWebRequest)base.GetWebRequest(address);
    		request.CookieContainer = CookieContainer;
    		return request;
    	}
    }
  4. Add the following code:

    Using WebClient
    using (var webclient = new CookieAwareWebClient())
    {
    	var reqparm = new System.Collections.Specialized.NameValueCollection();
    	reqparm.Add("username", "api");
    	reqparm.Add("password", "password");
    	byte[] responsebytes = webclient.UploadValues("http://api.jiwa.com.au/auth", "POST", reqparm);
    	string responsebody = Encoding.UTF8.GetString(responsebytes);
    
    	ServiceStack.AuthenticateResponse webClientAuthResponse = Newtonsoft.Json.JsonConvert.DeserializeObject<ServiceStack.AuthenticateResponse>(responsebody);
    
    	// Read a debtor
    	responsebody = webclient.DownloadString("http://api.jiwa.com.au:80/debtor/1001");                
    	JiwaFinancials.Jiwa.JiwaServiceModel.DebtorGetResponse webClientDebtorGetResponse = Newtonsoft.Json.JsonConvert.DeserializeObject<JiwaFinancials.Jiwa.JiwaServiceModel.DebtorGetResponse>(responsebody);
    }

Filter by label

There are no items with the selected labels at this time.

  • No labels