我想编写一个java http请求来发布用于语音到文本转换的音频文件(wav)。 我对http请求很新,并且找不到任何有用的提示如何实现。 在https://www.microsoft.com/cognitive-services/en-us/Speech-api/documentation/GetStarted/GetStarted-cURL中,我能够获得令牌(网址中的第1步),但现在我正在使用第2步。 有人可以帮助甚至提供第2步的java代码(将wav文件发布到认知服务)。
CURL请求:
curl -v -X POST "https://speech.platform.bing.com/recognize? scenarios=smd&appid=D4D52672-91D7-4C74-8AD8- 42B1D98141A5&locale=your_locale&device.os=your_device_os&version=3.0&format=json&instanceid=your_instance_id&requestid=your_request_id" -H 'Authorization: Bearer your_access_token' -H 'Content-type: audio/wav; codec="audio/pcm"; samplerate=16000' --data-binary @your_wave_file
到目前为止我的Java HTTP代码(使用Apache):
HttpClient client = HttpClientBuilder.create().build();
String url = "https://speech.platform.bing.com/recognize";
String appId = "D4D52672-91D7-4C74-8AD8-42B1D98141A5"; //Always use this. See Docu
String token = "12345"; // received from step 1 (see MS documentation)
String locale = "de-DE";
String deviceOS = "Windows";
String version = "3.0";
String format = "json";
String instanceid = UUID.randomUUID().toString();
String scenarios = "smd";
// setting up post parameters
Map<String, String> postParameters = new HashMap<>();
postParameters.put("scenarios", scenarios);
postParameters.put("appid", appId);
postParameters.put("locale", locale);
postParameters.put("device.os", deviceOS);
postParameters.put("format", format);
postParameters.put("instanceid", instanceid);
postParameters.put("requestid", instanceid);
postParameters.put("version", version);
// setting up HttpPost
HttpPost httpPost = new HttpPost(url);
// PARAMETERS
List<NameValuePair> qparams = new ArrayList<>();
for (Map.Entry<String, String> s : postParameters.entrySet()) {
qparams.add(new BasicNameValuePair(s.getKey(), s.getValue()));
}
httpPost.setEntity(new UrlEncodedFormEntity(qparams));
// HEADERS
String wavFile = "C:\\Folder\\AudioData.wav";
Map<String, String> postHeaders = new HashMap<>();
postHeaders.put("Authorization", "Bearer " + token);
postHeaders.put("Content-Type", "audio/wav; codec=\"audio/pcm\"; samplerate=16000");
for (Map.Entry<String, String> entry : postHeaders.entrySet()) {
httpPost.setHeader(entry.getKey(), entry.getValue());
}
// WAV FILE
File file = new File(wavFile);
FileBody bin = new FileBody(file, ContentType.DEFAULT_BINARY);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addPart("bin", bin);
HttpEntity reqEntity = builder.build();
httpPost.setEntity(reqEntity);
// RESPONSE
HttpResponse response = client.execute(httpPost);
int responseCode = response.getStatusLine().getStatusCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
BufferedReader rd = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));
StringBuilder result = new StringBuilder();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
System.out.println(result.toString());
我的响应代码是200,但我的response.getEntity()。getContent()为空。我希望有一个JSON。
btw:如果我用curl(当然设置了参数)来解决这个问题,那么它可以正常工作,并且我将JSON恢复为可识别的语音文本。
你能帮帮我吗?
耐抗