locked
Getting 408 error in reply when i try to use Bing speech to text API RRS feed

  • Question

  • package MicrosoftSpeech;
    
    import java.awt.List;
    import java.io.BufferedInputStream;
    import java.io.BufferedReader;
    import java.io.ByteArrayOutputStream;
    import java.io.DataOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Map.Entry;
    import java.util.UUID;
    
    import javax.net.ssl.HttpsURLConnection;
    
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.entity.UrlEncodedFormEntity;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.entity.ContentType;
    import org.apache.http.entity.StringEntity;
    import org.apache.http.entity.mime.HttpMultipartMode;
    import org.apache.http.entity.mime.MultipartEntityBuilder;
    import org.apache.http.entity.mime.content.FileBody;
    import org.apache.http.impl.client.HttpClientBuilder;
    import org.apache.http.message.BasicNameValuePair;
    import org.json.JSONException;
    import org.json.JSONObject;
    
    
    public class MicrosoftSpeechTOText {
    
                    /**
                    * @param args
                    * @throws IOException 
                     * @throws JSONException 
                     */
                    public static void main(String[] args) throws IOException, JSONException {
                                    URL url = new URL("https://api.cognitive.microsoft.com/sts/v1.0/issueToken");
            HttpsURLConnection webRequest = (HttpsURLConnection) url.openConnection();
            
            webRequest.setDoInput(true);
            webRequest.setDoOutput(true);
            webRequest.setConnectTimeout(5000);
            webRequest.setReadTimeout(5000);
            webRequest.setRequestMethod("POST");
    
            byte[] bytes = new byte[0];
            webRequest.setRequestProperty("content-length", String.valueOf(bytes.length));
            webRequest.setRequestProperty("Ocp-Apim-Subscription-Key", "712992460790453f98ddc4d546abe8e9");
            webRequest.connect();
            System.out.println(webRequest);
            DataOutputStream dop = new DataOutputStream(webRequest.getOutputStream());
            dop.write(bytes);
            dop.flush();
            dop.close();
    
          InputStream  inSt = webRequest.getInputStream();
            InputStreamReader in = new InputStreamReader(inSt);
            BufferedReader bufferedReader = new BufferedReader(in);
            StringBuffer strBuffer = new StringBuffer();
            String line = null;
            while ((line = bufferedReader.readLine()) != null) {
                strBuffer.append(line);
            }
    
            bufferedReader.close();
            in.close();
            inSt.close();
            webRequest.disconnect();
    
            // parse the access token
            String token = strBuffer.toString();
            System.out.println(token);
            
            
            
            
            String fileFullPath = "C:\\Users\\arjun.jasmatiya\\workspace_new\\SpeechTOText\\src\\main\\resources\\audio.wav";
                                    String sttServiceUri = "https://speech.platform.bing.com/speech/recognition/conversation/cognitiveservices/v1";
                                    
            
            try{
                // Note: Sign up at http://www.projectoxford.ai for the client credentials.
                    
                    
                    //language=pt-BR&locale=your_locale&format=your_format&requestid=your_guid
                sttServiceUri += "?language=en-US";                                  // websearch is the other main option.
                //sttServiceUri += "&appid=D4D52672-91D7-4C74-8AD8-42B1D98141A5";     // You must use this ID.
                sttServiceUri += "&locale=en-US";                                   // We support several other languages.  Refer to README file.
                //sttServiceUri += "&device.os=wp7";
                //sttServiceUri += "&version=3.0";
                sttServiceUri += "&format=json";
               // sttServiceUri += "&instanceid=565D69FF-E928-4B7E-87DA-9A750B96D9E3";
                UUID uuid = UUID.randomUUID();
                sttServiceUri += "&requestid=" + uuid.toString();
                
                URL url1 = new URL(sttServiceUri);
                
                HttpsURLConnection webRequest1 = (HttpsURLConnection) url1.openConnection();
                System.out.println("url connected");
                webRequest1.setDoInput(true);
                webRequest1.setDoOutput(true);
                webRequest1.setConnectTimeout(5000);
                webRequest1.setReadTimeout(15000);
                webRequest1.setRequestMethod("POST");
                webRequest1.setRequestProperty("Content-Type", "audio/wav; samplerate=16000");
                webRequest1.setRequestProperty("Authorization", "Bearer " + token);
    
                webRequest1.setRequestProperty("Transfer-Encoding", "chunked");
                byte[] bytes1 = ToByteArray(fileFullPath);
                webRequest1.setRequestProperty("content-length", String.valueOf(bytes1.length));
                
                System.out.println("websocket connected");
                DataOutputStream dop1 = new DataOutputStream(webRequest1.getOutputStream());
                dop1.write(bytes);
                dop1.flush();
                dop1.close();
                System.out.println("Output stream set");
                // Get the response
                System.out.println(webRequest1);
                final BufferedReader rd = new BufferedReader(new InputStreamReader(webRequest1.getInputStream()));
                System.out.println("buffer reader created");
                final StringBuffer stringBuffer = new StringBuffer();
                String line2;
                while ((line2 = rd.readLine()) != null) {
                    stringBuffer.append(line2);
                }
                rd.close();
                webRequest1.disconnect();
                System.out.println(stringBuffer.toString());
            }catch(Exception e){
                    e.printStackTrace();
            }
            
               
                    }
                    
                    
                    
                    public static byte[] ToByteArray(String filename) throws IOException {  
                                      
                        File f = new File(filename);  
                        if (!f.exists()) {  
                            throw new FileNotFoundException(filename);  
                        }  
    
                        ByteArrayOutputStream bos = new ByteArrayOutputStream((int) f.length());  
                        BufferedInputStream in = null;  
                        try {  
                            in = new BufferedInputStream(new FileInputStream(f));  
                            int buf_size = 1024;  
                            byte[] buffer = new byte[buf_size];  
                            int len = 0;  
                            while (-1 != (len = in.read(buffer, 0, buf_size))) {  
                                bos.write(buffer, 0, len);  
                            }  
                            return bos.toByteArray();  
                        } catch (IOException e) {  
                            e.printStackTrace();  
                            throw e;  
                        } finally {  
                            try {  
                                in.close();  
                            } catch (IOException e) {  
                                e.printStackTrace();  
                            }  
                            bos.close();  
                        }  
    
                    }
    }
    

    Above is my code 

    and i am getting result:


    java.io.IOException: Server returned HTTP response code: 408 for URL: https://speech.platform.bing.com/speech/recognition/interactive/cognitiveservices/v1?language=en-US&locale=en-US&format=json&requestid=204dae8e-8810-47fb-90ce-4f66042628bb

                    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)

                    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)

                    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)

                    at MicrosoftSpeech.MicrosoftSpeechTOText.main(MicrosoftSpeechTOText.java:131)

    I am trying to call Bing Speech Rest API in java . Even after adding Transfer-Encoded Header i am receiving the above mentioned error. I request you to  please provide a solution for my issue ASAP.

    Tuesday, June 6, 2017 6:23 AM