Skip to content Skip to sidebar Skip to footer

How To Use HTTP POST With "application/octet-stream" In Android? (Microsoft Cognitive Video)

I want to use the Video Cognitive Service in Android. The sample that Microsoft provided is used in C#. The video function is sending an URL to the server, So I think it is possibl

Solution 1:

You may try something like this to send image files for cognitive-services face detect. Using org.apache.httpcomponents::httpclient :

    @Test
    public void testSendBinary() throws MalformedURLException {
        File picfile = new File("app/sampledata/my_file.jpeg");
        if (!picfile.exists()) throw new AssertionError();


        HttpClient httpclient = HttpClients.createDefault();

        try {
            URIBuilder builder = new URIBuilder("https://westcentralus.api.cognitive.microsoft.com/face/v1.0/detect");

            builder.setParameter("returnFaceId", "true");
            builder.setParameter("returnFaceLandmarks", "false");

            URI uri = builder.build();
            HttpPost request = new HttpPost(uri);
            request.setHeader("Content-Type", "application/octet-stream");
            request.setHeader("Ocp-Apim-Subscription-Key", "***");

            // Request body
            request.setEntity(new FileEntity(picfile));

            HttpResponse response = httpclient.execute(request);
            HttpEntity entity = response.getEntity();

            if (entity != null) {
                System.out.println(EntityUtils.toString(entity));
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

Solution 2:

HTTP POST refers to the HTTP method 'POST', application/octet-stream refers to the media type - in this case a stream of application specific octets or bytes.

This is, unfortunately, very subjective as the mechanism for uploading content via HTTP action may be preferred one way or another. Suffice it to say, you will create an InputStream of your content, format a POST request using the mechanism of your choosing:

Making sure to set the content-type of the POST to application/octet-stream.

After performing the post, consult your API documentation for expected return types.


Post a Comment for "How To Use HTTP POST With "application/octet-stream" In Android? (Microsoft Cognitive Video)"