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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无法使用Delphi通过Post打开Microsoft语音识别API相关的知识,希望对你有一定的参考价值。

我想使用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;
答案

您的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通过Post打开Microsoft语音识别API的主要内容,如果未能解决你的问题,请参考以下文章

yarn : 无法加载文件 C:UsersliuanAppDataRoaming pmyarn.ps1,因为在此系统上禁止运行脚本。有关详细信息,请参阅 http://go.microso

Delphi:无法打开 .bmp 文件

delphi ClientSocket的属性ClientType设置成ctNonBlocking无法捕获异常,该怎么处理

delphi获取窗体的句柄

WSUS 4.0 由于连接数限制导致管理控制台无法打开。

delphi中使用HTTP控件,怎么使用POST的异步方式