Create project, Compile imc-rs client codes, Run codes and view result – HP Intelligent Management Center Licenses User Manual

Page 23

Advertising
background image

1.

Enter "http://imc_host:port/imcrs/application.wadl" in the address bar of your browser
(Firefox is recommended), where imc_host is the hostname or IP address of the iMC server,
and port is the service port number (8080 by default).

2.

In the identity authentication dialog box that appears, enter "admin" as the username and
password, and click OK.

3.

If the browser displays an XML document with "<application>" as the root node, iMC-RS is
ready for you to use. If you encounter an error (such as a 404 error) in step 2 or 3, check
your iMC version and service.

Create project

Launch the Eclipse environment and create a new project.
1.

Select File > New > Java Project.

2.

Type the project name such as "MyRestClient" and click Next.

3.

Click the Libraries tab, click Add External JARs, and add the decompressed Apache HttpClient
files httpcore-4.1.jar, httpclient-4.1.jar, and commons-logging-1.1.1.jar
to the project.

4.

Click Finish.

Compile iMC-RS client codes

Right-click the project and select New > Class from the shortcut menu.

In the wizard, type the package name (for example, com.mycompany.rest) and class name
(for example, MyClient), select Create Main Class, and click Finish.

Enter the following codes in the new class:

package com.mycompany.rest;

import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

public class MyClient {
public static void main(String[] args) throws Exception {
DefaultHttpClient client = new DefaultHttpClient();
client.getCredentialsProvider().setCredentials(
new AuthScope("imc_host", 8080, "iMC RESTful Web Services"),
new UsernamePasswordCredentials("admin", "admin"));
HttpGet get = new HttpGet("http://imc_host:8080/imcrs/plat/res/device");
get.addHeader("accept", "application/xml");
HttpResponse response = client.execute(get);
System.out.println(response.getStatusLine());
System.out.println(EntityUtils.toString(response.getEntity()));
}
}

In the previous codes, "imc_host" and "8080" represent the iMC server address and port number,
and the two "admin" strings represent the login username and password.

Run codes and view result

Select Run > Run or press Ctrl + F11 to run the codes. The Eclipse console will display the following:

HTTP/1.1 200 OK
<?xml version=”;1.0” encoding=”UTF-8” standalone=”yes”?><list start=”0”>.....

The First iMC-RS Client

23

Advertising