Bing 语音 API 使用 JAVA 代码给出错误
Bing Speech API giving error with JAVA code
我正在尝试使用 Java 服务中的 Bind Speech Rest API,但每次我都收到 408 响应代码。
java.io.IOException:服务器返回 HTTP 响应代码:408 URL:https://speech.platform.bing.com/speech/recognition/conversation/cognitiveservices/v1?language=en-US
我曾尝试使用不同的方法调用 REST API,但始终出现 408 请求超时错误。
虽然我使用类似调用 Speech REST 的代码创建了一个 .net 示例 API,但它正在运行。
有什么方法可以诊断我的 Java 代码中缺少什么吗?
这是我的 JAVA 代码
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);
}
Bing 语音 API 想要分块传输音频数据。但是,对于 HttpURLConnection
class,设置 Transfer-Encoding
header 不会神奇地导致传输类型切换——您需要通过 setChunkedStreamingMode
或其变体之一。您还需要省略 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 );
我正在尝试使用 Java 服务中的 Bind Speech Rest API,但每次我都收到 408 响应代码。
java.io.IOException:服务器返回 HTTP 响应代码:408 URL:https://speech.platform.bing.com/speech/recognition/conversation/cognitiveservices/v1?language=en-US
我曾尝试使用不同的方法调用 REST API,但始终出现 408 请求超时错误。
虽然我使用类似调用 Speech REST 的代码创建了一个 .net 示例 API,但它正在运行。
有什么方法可以诊断我的 Java 代码中缺少什么吗?
这是我的 JAVA 代码
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);
}
Bing 语音 API 想要分块传输音频数据。但是,对于 HttpURLConnection
class,设置 Transfer-Encoding
header 不会神奇地导致传输类型切换——您需要通过 setChunkedStreamingMode
或其变体之一。您还需要省略 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 );