无法通过 Post 使用 Delphi 打开 Microsoft 语音识别 API
Can't open Microsoft Speech Recognition API via Post with Delphi
我想在 Delphi 中使用 Indy 的 TIdHTTP 通过 HTTPS 向 Microsoft 语音识别 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 中使用 Indy 的 TIdHTTP 通过 HTTPS 向 Microsoft 语音识别 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;