Wireshark中C#/Python/CURL调用Azure API的HTTP可见性不一致问题
Azure TTS API 部分词汇返回空音频及抓包差异问题
我使用Azure TTS API生成文字对应的音频,C#代码原本运行正常,但发现部分词汇会返回空音频。尝试用Python和CURL调用同一API,却能正常获取这些词汇的音频。
为排查请求差异,我配置Wireshark通过SSLKEYLOGFILE解密TLS流量,结果运行Python代码时能看到完整HTTP请求,但运行C#和CURL时仅能看到TCP和TLS流量,无法查看HTTP请求内容,想了解不同语言/工具抓包结果存在差异的原因。
C# 代码片段
string subscriptionKey = Environment.GetEnvironmentVariable("STT_API_KEY"); Authentication auth = new Authentication("https://eastus.api.cognitive.microsoft.com/sts/v1.0/issueToken", subscriptionKey); try { accessToken = await auth.FetchTokenAsync().ConfigureAwait(false); //Console.WriteLine("Successfully obtained an access token. \n"); } catch (Exception ex) { //Console.WriteLine("Failed to obtain an access token."); //Console.WriteLine(ex.ToString()); //Console.WriteLine(ex.Message); return null; } string host = "https://eastus.tts.speech.microsoft.com/cognitiveservices/v1"; // Create SSML document. XDocument body = new XDocument( new XElement("speak", new XAttribute("version", "1.0"), new XAttribute(XNamespace.Xml + "lang", "en-US"), new XElement("voice", new XAttribute(XNamespace.Xml + "lang", "en-US"), new XAttribute(XNamespace.Xml + "gender", "Female"), new XAttribute("name", "en-US-AvaNeural"), // Short name for "Microsoft Server Speech Text to Speech Voice (en-US, Jessa24KRUS)" text))); using (HttpClient client = new HttpClient()) { using (HttpRequestMessage request = new HttpRequestMessage()) { // Set the HTTP method request.Method = HttpMethod.Post; // Construct the URI request.RequestUri = new Uri(host); // Set the content type header request.Content = new StringContent(body.ToString(), Encoding.UTF8, "application/ssml+xml"); // Set additional header, such as Authorization and User-Agent request.Headers.Add("Authorization", "Bearer " + accessToken); request.Headers.Add("Connection", "Keep-Alive"); // Update your resource name request.Headers.Add("User-Agent", "YOUR_RESOURCE_NAME"); // Audio output format. See API reference for full list. request.Headers.Add("X-Microsoft-OutputFormat", "riff-24khz-16bit-mono-pcm"); // Create a request //Console.WriteLine("Calling the TTS service. Please wait... \n"); using (HttpResponseMessage response = await client.SendAsync(request).ConfigureAwait(false)) }
Python 代码片段
endpoint = 'https://eastus.tts.speech.microsoft.com/cognitiveservices/v1' ssml = """ <speak version='1.0' xml:lang='en-US'> <voice xml:lang='en-US' xml:gender='Female' name='en-US-AvaNeural'> black </voice> </speak> """ headers = { 'Ocp-Apim-Subscription-Key': subscription_key, 'Content-Type': 'application/ssml+xml', 'X-Microsoft-OutputFormat': 'audio-16khz-32kbitrate-mono-mp3' } response = requests.post(endpoint, headers=headers, data=ssml, verify=False)
内容的提问来源于stack exchange,提问作者tzviya
相关产品推荐
相关产品推荐

