Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将C#中的HTTP post请求发送到Microsoft Bing语音API_C#_Http Post_Httpclient_Content Type_Bing Api - Fatal编程技术网

将C#中的HTTP post请求发送到Microsoft Bing语音API

将C#中的HTTP post请求发送到Microsoft Bing语音API,c#,http-post,httpclient,content-type,bing-api,C#,Http Post,Httpclient,Content Type,Bing Api,我正在尝试向microsoft Bing语音API发送HTTP post请求,以便转录音频文件。首先,我们需要发送post请求以获取“访问令牌”作为响应,然后使用该令牌(作为授权)“在另一个post请求中上载实际文件并在响应中获取转录。我可以发送第一个post请求并成功获取访问令牌,但我无法获得第二个post请求的合理响应。我遵循此页面: 这是第二个post请求: Guid requestId = Guid.NewGuid(); var Uri = @"https

我正在尝试向microsoft Bing语音API发送HTTP post请求,以便转录音频文件。首先,我们需要发送post请求以获取“访问令牌”作为响应,然后使用该令牌(作为授权)“在另一个post请求中上载实际文件并在响应中获取转录。我可以发送第一个post请求并成功获取访问令牌,但我无法获得第二个post请求的合理响应。我遵循此页面:

这是第二个post请求:

        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);

我想问题在于我在哪里设置“内容类型”对于标题。原因是当我调试时,我没有看到在请求的标题中设置此属性。事实上,标题中没有内容类型。任何帮助都将不胜感激。此页面讨论等效的
curl
命令,也可能有帮助:

内容类型
是一个与内容相关的标题以下代码适用于我:

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();
    }
}

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

如果您不想每次都登录而不是使用
'Authorization':'Bearer{TOKEN}
头,您可以使用
'Ocp-Apim-Subscription-Key':“{YOUR AZURE TOKEN}”
,这样就不必向应用程序发出授权工厂或超出必要的请求,从而使其速度更快

注意:{TOKEN}是一个类似JWT的令牌

eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzY29wZSI6Imh0dHBzOi8vc3BlZWNoLnBsYXRmb3JtLmJpbmcuY29tIiwic3Vic2NyaXB0aW9uLWlkIjoiZmFhZTNlYTkxNmI1NGMxZWEyODY4MDlhYTg3ZWE1MmUiLCJwcm9kdWN0LWlkIjoiQmluZy5TcGVlY2guUHJldmlldyIsImNvZ25pdGl2ZS1zZXJ2aWNlcy1lbmRwb2ludCI6Imh0dHBzOi8vYXBpLmNvZ25pdGl2ZS5taWNyb3NvZnQuY29tL2ludGVybmFsL3YxLjAvIiwiYXp1cmUtcmVzb3VyY2UtaWQiOiIiLCJpc3MiOiJ1cm46bXMuY29nbml0aXZlc2VydmljZXMiLCJhdWQiOiJ1cm46bXMuc3BlZWNoIiwiZXhwIjoxNTAwODgxNjIzfQ.KdlCrIJ_H0jxs1yyeyYxYR7ucbLuFKT__ep7lGJmGbU
注2:{您的AZURE令牌}类似于
d5kals90935b40809dc6k38533c21e85
,您可以找到它

请求如下所示:

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"
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"