【问题标题】:Sending HTTP post request in C# to Microsoft Bing speech API将 C# 中的 HTTP 发布请求发送到 Microsoft Bing 语音 API
【发布时间】:2016-12-17 13:48:33
【问题描述】:

我正在尝试向 Microsoft Bing 语音 API 发送 HTTP 发布请求或转录音频文件。首先,我们需要发送一个发布请求以获取“访问令牌”作为响应,然后在另一个发布请求中使用此令牌(作为授权)来上传实际文件并在响应中获取转录。我可以发送第一个发布请求并成功获取访问令牌,但我无法为我的第二个发布请求获得合理的响应。我关注此页面:https://www.microsoft.com/cognitive-services/en-us/speech-api/documentation/api-reference-rest/bingvoicerecognition

这是第二次发帖请求:

        Guid requestId = Guid.NewGuid();
        var Uri = @"https://speech.platform.bing.com/recognize?version=3.0&requestid=" + requestId.ToString() + @"&appID=D4D52672-91D7-4C74-8AD8-42B1D981415A&format=json&locale=en-US&device.os=Windows%20OS&scenarios=ulm&instanceid=f1efbd27-25fd-4212-9332-77cd63176112";
        HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, Uri);

        request.Headers.Add("Authorization", String.Format("Bearer {0}", accessToken));
        request.Headers.TryAddWithoutValidation("Content-Type", @"audio/wav; samplerate=16000");
        MemoryStream ms = new MemoryStream();
        using (var fs = System.IO.File.OpenRead("audio.wav"))
        {
            byte[] buffer = new byte[1024 * 8];
            while (fs.Read(buffer, 0, buffer.Length) > 0)
            {
                ms.Write(buffer, 0, buffer.Length);
            }
            fs.Close();
        }
        ms.Seek(0, SeekOrigin.Begin);

        HttpContent _Body = new StreamContent(ms);
        request.Content = _Body;
        var client2 = new HttpClient();
        var response2 = client2.SendAsync(request);

我想问题出在我为标题设置“Content-Type”的位置。原因是当我调试时,我没有看到在请求的 Header 中设置了这个属性。实际上,header 中没有 Content-Type。任何帮助,将不胜感激。此页面讨论了等效的 curl 命令,也可以提供帮助:https://social.msdn.microsoft.com/Forums/en-US/ad73e4f1-e576-4080-9fe7-060cc2f583ca/microsoft-bing-voice-recognition-api-authorization-404resource-not-found?forum=SpeechService

【问题讨论】:

    标签: c# http-post httpclient content-type bing-api


    【解决方案1】:

    Content-Type 是与内容相关的标头。以下代码适用于我:

    public async Task<string> SendRequestAsync(string url, string bearerToken, string contentType, string fileName)
    {
        var content = new StreamContent(File.OpenRead(fileName));
        content.Headers.TryAddWithoutValidation("Content-Type", contentType);
    
        using (var httpClient = new HttpClient())
        {           
            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", bearerToken);
            var response = await httpClient.PostAsync(url, content);
    
            return await response.Content.ReadAsStringAsync();
        }
    }
    

    您的案例中的调用(如果您在同步上下文中工作):

    var result = SendRequestAsync(Uri, accessToken, "audio/wav; samplerate=16000", "audio.wav").Result;
    

    【讨论】:

      【解决方案2】:

      您可以改为发送以下标头,因为令牌而不必执行 2 个请求。

      如果您不想每次都登录而不是使用'Authorization': 'Bearer {TOKEN}' 标头,您可以使用'Ocp-Apim-Subscription-Key': '{YOUR AZURE TOKEN}' 以便不必向应用程序发出授权工厂或更多请求并使其更快

      注意:{TOKEN} 是 JWT 令牌,如

      eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzY29wZSI6Imh0dHBzOi8vc3BlZWNoLnBsYXRmb3JtLmJpbmcuY29tIiwic3Vic2NyaXB0aW9uLWlkIjoiZmFhZTNlYTkxNmI1NGMxZWEyODY4MDlhYTg3ZWE1MmUiLCJwcm9kdWN0LWlkIjoiQmluZy5TcGVlY2guUHJldmlldyIsImNvZ25pdGl2ZS1zZXJ2aWNlcy1lbmRwb2ludCI6Imh0dHBzOi8vYXBpLmNvZ25pdGl2ZS5taWNyb3NvZnQuY29tL2ludGVybmFsL3YxLjAvIiwiYXp1cmUtcmVzb3VyY2UtaWQiOiIiLCJpc3MiOiJ1cm46bXMuY29nbml0aXZlc2VydmljZXMiLCJhdWQiOiJ1cm46bXMuc3BlZWNoIiwiZXhwIjoxNTAwODgxNjIzfQ.KdlCrIJ_H0jxs1yyeyYxYR7ucbLuFKT__ep7lGJmGbU
      

      注意 2:{YOUR AZURE TOKEN} 类似于 d5kals90935b40809dc6k38533c21e85,您会发现它是 here

      请求如下所示:

      curl -v -X POST "https://speech.platform.bing.com/speech/recognition/interactive/cognitiveservices/v1?language=es-ES&locale=es-ES&format=simple&requestid=req_id" -H "Ocp-Apim-Subscription-Key: d5kals90935b40809dc6k38533c21e85" -H 'Transfer-Encoding: chunked'  -H 'Content-type: audio/wav; codec="audio/pcm"; samplerate=8000' --data-binary @"{BINAYFILE}.wav"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-10-29
        • 1970-01-01
        • 2012-12-11
        • 1970-01-01
        • 2017-12-11
        • 1970-01-01
        • 2019-07-30
        • 1970-01-01
        相关资源
        最近更新 更多