Skip to content
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
Cannot retrieve contributors at this time
// <dependencies>
// This sample uses Apache HttpComponents:
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;
// </dependencies>
// <environment>
* To compile and run, enter the following at a command prompt:
* javac -cp .;lib\*
* java -cp .;lib\* Detect
public class Detect {
private static final String subscriptionKey = "PASTE_YOUR_FACE_SUBSCRIPTION_KEY_HERE";
private static final String endpoint = "PASTE_YOUR_FACE_ENDPOINT_HERE";
private static final String imageWithFaces =
// </environment>
// <main>
public static void main(String[] args) {
HttpClient httpclient = HttpClientBuilder.create().build();
URIBuilder builder = new URIBuilder(endpoint + "/face/v1.0/detect");
// Request parameters. All of them are optional.
builder.setParameter("detectionModel", "detection_03");
builder.setParameter("returnFaceId", "true");
// Prepare the URI for the REST API call.
URI uri =;
HttpPost request = new HttpPost(uri);
// Request headers.
request.setHeader("Content-Type", "application/json");
request.setHeader("Ocp-Apim-Subscription-Key", subscriptionKey);
// Request body.
StringEntity reqEntity = new StringEntity(imageWithFaces);
// Execute the REST API call and get the response entity.
HttpResponse response = httpclient.execute(request);
HttpEntity entity = response.getEntity();
// </main>
// <print>
if (entity != null)
// Format and display the JSON response.
System.out.println("REST Response:\n");
String jsonString = EntityUtils.toString(entity).trim();
if (jsonString.charAt(0) == '[') {
JSONArray jsonArray = new JSONArray(jsonString);
else if (jsonString.charAt(0) == '{') {
JSONObject jsonObject = new JSONObject(jsonString);
} else {
catch (Exception e)
// Display error message.
// </print>