gpt4 book ai didi

delphi - 无法使用 Delphi 通过 Post 打开 Microsoft Speech Recognition API

转载 作者:太空宇宙 更新时间:2023-11-03 15:09:17 25 4
gpt4 key购买 nike

我想在 Delphi 中使用 Indy 的 TIdHTTP 通过 HTTPS 向 Microsoft Speech Recognition API 发送 Post 请求。

在 Microsoft 的语音识别 API 页面上:Microsoft Speech Recognition API Get started with speech recognition by using the REST API

他们写道你应该像这样发送一个 HTTP POST 请求:

POST     https://speech.platform.bing.com/speech/recognition/interactive/cognitiveservices/v1?language=en-US&format=detailed HTTP/1.1
Accept: application/json;text/xml
Content-Type: audio/wav; codec=audio/pcm; samplerate=16000
Ocp-Apim-Subscription-Key: YOUR_SUBSCRIPTION_KEY
Host: speech.platform.bing.com
Transfer-Encoding: chunked
Expect: 100-continue

我在 Delphi XE 10 Indy 上进行了尝试。

但我收到错误 400 - 错误请求作为答案!

我在下面的代码中做错了什么?

procedure TForm1.Button1Click(Sender: TObject);
var
Response, csrf, url: String;
PostStream: TIdMultipartFormDataStream;

HTTPClient: TIdHTTP;
SSL: TIdSSLIOHandlerSocketOpenSSL;


begin
url := 'https://speech.platform.bing.com/speech/recognition/interactive/cognitiveservices/v1?language=en-US&format=detailed HTTP/1.1';

HTTPClient := TIdHTTP.Create;
try
HTTPClient.Disconnect;
HTTPClient.AllowCookies := True;
SSL := TIdSSLIOHandlerSocketOpenSSL.Create(HTTPClient);
SSL.SSLOptions.SSLVersions := [sslvTLSv1, sslvTLSv1_1, sslvTLSv1_2];
HTTPClient.IOHandler := SSL;
HTTPClient.HandleRedirects := true;

HTTPClient.Request.Accept := 'application/json;text/xml';
HTTPClient.Request.Method := 'POST';
HTTPClient.Request.ContentType := 'audio/wav; codec=audio/pcm; samplerate=16000';

//-----------------------------------------------------------------------


PostStream := TIdMultiPartFormDataStream.Create;
try
PostStream.AddFormField('Ocp-Apim-Subscription-Key','YOUR_SUBSCRIPTION_KEY');

PostStream.AddFile('file', 'test.wav');


Response := HTTPClient.Post(url, PostStream);

PostStream.Clear;


finally
PostStream.Free;
end;

finally
HTTPClient.Free;
end;
end;

最佳答案

您的POST 请求未按照Microsoft 文档所述方式设置。最重要的是,您根本不应该使用 TIdMultipartFormDataStream,因为 REST 服务器不需要 multipart/form-data 格式的请求。请求的正文应该只是实际的 WAV 文件,除此之外别无其他。 TIdHTTP 甚至有一个重载的 Post() 专门用于上传一个文件。

试试这个:

procedure TForm1.Button1Click(Sender: TObject);
var
Response, url: String;
HTTPClient: TIdHTTP;
SSL: TIdSSLIOHandlerSocketOpenSSL;
begin
url := 'https://speech.platform.bing.com/speech/recognition/interactive/cognitiveservices/v1?language=en-US&format=detailed';

HTTPClient := TIdHTTP.Create;
try
SSL := TIdSSLIOHandlerSocketOpenSSL.Create(HTTPClient);
SSL.SSLOptions.SSLVersions := [sslvTLSv1, sslvTLSv1_1, sslvTLSv1_2];
HTTPClient.IOHandler := SSL;

HTTPClient.AllowCookies := True;
HTTPClient.HandleRedirects := true;

HTTPClient.Request.Accept := 'application/json;text/xml';
HTTPClient.Request.ContentType := 'audio/wav; codec=audio/pcm; samplerate=16000';
HTTPClient.Request.CustomHeaders.Values['Ocp-Apim-Subscription-Key'] := 'YOUR_SUBSCRIPTION_KEY';

Response := HTTPClient.Post(url, 'test.wav');
finally
HTTPClient.Free;
end;
end;

关于delphi - 无法使用 Delphi 通过 Post 打开 Microsoft Speech Recognition API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49088591/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com