无法使用Delphi通过Post打开Microsoft语音识别API

问题描述 投票:-1回答:1

我想使用Delphi中的Indy的TIdHTTP通过HTTPS向Microsoft语音识别API发送Post请求。

关于微软语音识别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;
delphi ssl https speech indy10
1个回答
3
投票

您的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;
© www.soinside.com 2019 - 2024. All rights reserved.