Using Java
The following Java examples use the Apache HttpClient and the Javax json classes.
Sample code doing a GET request
/* Need Apache HttpClient 4.5 */
/* https://hc.apache.org/downloads.cgi */
/* Need javax.json-1.0.jar */
/* http://www.java2s.com/Code/Jar/j/Downloadjavaxjson10jar.htm */
//
// Get the list of monitors
//
public static void DoGETMonitors (String serverName, String domain, String userName, String password)
{
try {
// NTLM Authentication
CredentialsProvider cProvider = new BasicCredentialsProvider();
cProvider.setCredentials(AuthScope.ANY,
new NTCredentials(userName,password,"",domain) );
RequestConfig config = RequestConfig.custom().
setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.NTLM)).build();
// Create the HTTP Request
HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(cProvider);
HttpHost target = new HttpHost(serverName,80,"http");
CloseableHttpClient client = HttpClients.custom().
setDefaultRequestConfig(config).build();
HttpGet request = new HttpGet("/reveille/api/Monitors/AllMonitors");
request.addHeader("content-type","application/json");
request.addHeader("Accept","*/*");
// Get the response
HttpResponse response = client.execute(target,request,context);
InputStream is = response.getEntity().getContent();
JsonReader rdr = Json.createReader(is);
JsonArray appArray = rdr.readArray();
rdr.close();
// Output the list of monitors
for (Object monitor: appArray){
System.out.println(monitor.toString());
}
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
Sample code doing a POST request
try {
// NTLM Authentication
CredentialsProvider cProvider = new BasicCredentialsProvider();
cProvider.setCredentials(AuthScope.ANY,
new NTCredentials(userName,password,"",domain) );
RequestConfig config = RequestConfig.custom()
.setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.NTLM))
.build();
// Create the HTTP Request
HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(cProvider);
HttpHost target = new HttpHost(serverName,80,"http");
CloseableHttpClient client = HttpClients.custom()
.setDefaultRequestConfig(config).build();
HttpPost post =
new HttpPost("/ReveilleUserAnalytics/API/Application/3/ApplicationDetails");
post.addHeader("content-type","application/json");
post.addHeader("Accept","*/*");
long HOUR = 3600*1000;
DateFormat ISOFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US);
ISOFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date toDate = new Date();
Date fromDate = new Date(toDate.getTime() - 24 * HOUR);
JsonObject filter = Json.createObjectBuilder()
.add("Type", "L10")
.add("Transactions", "")
.add("CustomField1Value", "")
.add("ClientIps", "")
.add("ServerIps", "")
.add("Errors",true)
.add("NonErrors",true)
.add("StartDateTime", ISOFormat.format(fromDate))
.add("EndDateTime", ISOFormat.format(toDate))
.add("SortExpression","")
.build();
StringEntity input = new StringEntity(filter.toString());
post.setEntity(input);
// Get the response
HttpResponse response = client.execute(target,post,context);
BufferedReader rd =
new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line = "";
while ((line = rd.readLine()) != null) {
System.out.println(line);
}
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
Was this helpful?
Please tell us why:
Thank you.