azure-cognitive-servicesbing-speech

Bing Speech API giving error with JAVA code


I am trying to use Bind Speech Rest API from Java service but every time I am getting 408 response code.

java.io.IOException: Server returned HTTP response code: 408 for URL: https://speech.platform.bing.com/speech/recognition/conversation/cognitiveservices/v1?language=en-US

I have tried calling REST API using diffrent methods but all the time, error is 408 Request time out.

Whereas I created a .net sample with similar code of calling Speech REST API, it is working.

Is there any way to diagnose what I am missing in my Java code ?

Here is my JAVA code

DataOutputStream dos = null;
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1024; // 1024*1024 = 1MB.  212144 is a quarter MB.
FileInputStream fileInputStream = null;

fileInputStream = new FileInputStream(new File("D://hello.wav"));


               URL url = new URL("https://speech.platform.bing.com/speech/recognition/conversation/cognitiveservices/v1?language=en-US");
   HttpURLConnection conn = (HttpURLConnection) url.openConnection();
   //method
   conn.setRequestMethod("POST");
   conn.setRequestProperty("Transfer-Encoding", "chunked");
   conn.setRequestProperty("Accept","application/json;text/xml");
   conn.setRequestProperty("Host","speech.platform.bing.com");
   //header
   conn.setRequestProperty("content-length", String.valueOf(fileInputStream.available()));
   conn.setRequestProperty("Content-Type", "audio/wav; codec=\"audio/pcm\"; samplerate=16000;");
   conn.setRequestProperty("Authorization", "Bearer "+ token );
   conn.setReadTimeout(22222230);
   conn.setDoOutput(true);

   conn.connect();

   dos = new DataOutputStream(conn.getOutputStream());

   bytesAvailable = fileInputStream.available();
   bufferSize = Math.min(bytesAvailable, maxBufferSize);
   buffer = new byte[bufferSize];

   bytesRead = fileInputStream.read(buffer, 0, bufferSize);
   while (bytesRead > 0)
   {
     try {
       dos.write(buffer, 0, bufferSize);          
     } catch (OutOfMemoryError oome) {

       oome.printStackTrace();
       fileInputStream.close();
       throw new Exception("Out Of Memory!");
     }
     bytesAvailable = fileInputStream.available();
     bufferSize = Math.min(bytesAvailable, maxBufferSize);
     bytesRead = fileInputStream.read(buffer, 0, bufferSize);
   }

   fileInputStream.close();
   dos.flush();
   dos.close();


   BufferedReader in = new BufferedReader(
           new InputStreamReader(
           conn.getInputStream()));
            String decodedString;
            while ((decodedString = in.readLine()) != null) {
            System.out.println(decodedString);
            }

Solution

  • The Bing Speech API wants chunked transfer of the audio data. For the HttpURLConnection class, though, setting the Transfer-Encoding header does not magically cause the transfer type to switch -- you need to set the streaming mode explicitly through the setChunkedStreamingMode or one of its variants. You'll also want to omit the Content-Length header.

    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    //method
    conn.setRequestMethod("POST");
    conn.setChunkedStreamingMode(0);
    //header
    conn.setRequestProperty("Accept","application/json;text/xml");
    conn.setRequestProperty("Content-Type", "audio/wav; codec=\"audio/pcm\"; samplerate=16000;");
    conn.setRequestProperty("Authorization", "Bearer "+ token );