Follow

Code Examples

The WebServices API is commonly used through another programming language or environment, such as .NET or Java. Compile and run the following simple example programs to make sure your License file is valid and that endpoint connectivity is functioning

This simple Java console example requires the open source Apache Axis SOAP client libraries (http://ws.apache.org/axis/) and the whatcountsapi.jar.  The following files must be added to the classpath:

  • whatcountsapi.jar
  • axis.jar
  • commons-discovery.jar
  • common-logging.jar
  • jaxrpc.jar
  • saaj.jar
  • xml-api.jar
  • xercesImpl.jar

When executed from a console the example should produce results similar to the following:

WhatCounts API version = 1.1 Session started, cookie = E4F3F9A0D0247600004D9BD052 Session ended.

The session cookie will be different each time you run the program.

Example

import java.io.*;
import java.net.*;
import whatcounts.api.client.soap.*;
/** * Example showing basic usage of the WhatCountsAPI using SOAP with Java bindings. * It uses the Axis (Apache SOAP) client package. */
public class Example1 { public static void main( String[] args ) throws Exception {
// Read the license file used to authenticate a session
File file = new File( "whatcounts_api_license.txt" );
byte[] buf = new byte[(int)file.length()];
try { FileInputStream fs = new FileInputStream( file );
int n = fs.read( buf );
} catch ( IOException e ) {
// Error - file not found...
throw e;
} String license = new String( buf );

// the serviceURL may be different if your company has its own WhatCounts Broadcaster
String serviceUrl = "http:[siteURL]/webservices/WhatCountsAPI";

// Create an API stub that is bound to the SOAP service endpoint.
WhatCountsAPI api = null;
try { URL endpoint = new java.net.URL( serviceUrl );
api = (WhatCountsAPI) new WhatCountsAPIServiceLocator().getWhatCountsAPI(endpoint);
} catch ( javax.xml.rpc.ServiceException e ) {
// Error - API not available
throw e;
}
// Get the current API version
String version = api.getVersion();
System.out.println( "WhatCounts API version = " + version );

// Begin a session
String cookie = api.beginSession( license );
if ( cookie != null ) { System.out.println( "Session started, cookie = " + cookie );

// -------------------
// Make API calls here
// -------------------
// End the session
api.endSession( cookie );
System.out.println( "Session ended." );
} else System.out.println( "Can't open an API session." );
}
}  

C# Example

Create a Web Reference to compile and run this example. Be sure to copy your license file to the same directory that you will run the example from.  To compile this program, use the .NET command line compiler:

csc /r:System.Web.Services.dll /r:WhatCountsAPIService.dll Example1.cs

When executed the example should produce results similar to the following:

WhatCounts API version = 1.1 Session started, cookie = E4F3F9A0D0247600004D9BD052 Session ended.

The session cookie will be different each time you run the program.

Example

using System;
using System.IO;
namespace WhatCountsAPIExample { class Example1 { [STAThread] static void Main(string[] args) {
// Read the license file
StreamReader sr = File.OpenText( "whatcounts_api_license.txt" );
string license = sr.ReadToEnd();

// Create a proxy endpoint to the remote SOAP API
WhatCountsAPIService api = new WhatCountsAPIService();

// if you are not using api.whatcounts.net, you'll need to set an explicit endpoint URL, e.g.:
api.Url = "http://[siteURL]/webservices/WhatCountsAPI";

// Get the current API version
string version = api.getVersion();
Console.WriteLine( "WhatCounts API version = {0}", version );

// Start a session and then end the session just to validate the license file.
string cookie = api.beginSession( license );
if ( cookie != null ) { Console.WriteLine( "Session started, cookie = {0}", cookie );

//------------------------------
// API calls can be done here...
//------------------------------
api.endSession( cookie );
Console.WriteLine( "Session ended." );
} else Console.WriteLine( "Can't open an API session." );
} } }

Was this article helpful?
0 out of 0 found this helpful
Have more questions? Submit a request

Comments

Powered by Zendesk