How to Parse an HTTP User-Agent String; Identify Robots using Java
--
User-agent request headers are designed so that network peers/servers can easily identify their various component parts. If you want to retrieve User-Agent information for your own purposes, we have an API that can help: our Parse HTTP User-Agent String API will return a haul of information about any given user-agent, including — importantly — whether that user-agent is a robot. Below I’ve included a full example response body (JSON) showing the various components returned by this API call:
{
"Successful": true,
"IsBot": true,
"BotName": "string",
"BotURL": "string",
"OperatingSystem": "string",
"OperatingSystemCPUPlatform": "string",
"OperatingSystemVersion": "string",
"DeviceType": "string",
"DeviceBrandName": "string",
"DeviceModel": "string",
"BrowserName": "string",
"BrowserVersion": "string",
"BrowserEngineName": "string",
"BrowserEngineVersion": "string"
}
You can easily take advantage of this API using the ready-to-run Java code examples included below to structure your API call. It’s completely free to use — just register a free account on our website (this account will come with a limit of 800 API calls per month and zero additional commitments; perfect for smaller projects and startups).
To start installing the API client, let’s first add a reference to the pom.xml repository:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
And then let’s add one to the pom.xml dependency:
<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v4.25</version>
</dependency>
</dependencies>
Now let’s shift focus over to the controller and add the following imports:
// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.UserAgentApi;
Lastly, let’s include the final code block to call the function. The comments indicate where to authenticate your Cloudmersive API key:
ApiClient defaultClient = Configuration.getDefaultApiClient();// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");UserAgentApi apiInstance = new UserAgentApi();
UserAgentValidateRequest request = new UserAgentValidateRequest(); // UserAgentValidateRequest | Input parse request
try {
UserAgentValidateResponse result = apiInstance.userAgentParse(request);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling UserAgentApi#userAgentParse");
e.printStackTrace();
}
When we pass our parse request through the function, we should structure it like so:
{
"UserAgentString": "string"
}
With that, you’re all finished — no more code required. Nice & easy!