微软服务器使用HTTP 实现TTS 依赖libcurl

在此期间研究了好多天Http,结果后面使用了一个postman工具,可以方便的调参数和请求头相关的东西。伤感啊~~~

微软文档:https://www.microsoft.com/cognitive-services/en-us/documentation

1.请求token

curl = curl_easy_init();
struct curl_slist *headers = NULL;  
headers = curl_slist_append(headers, "Content-type:application/x-www-form-urlencoded");  
headers = curl_slist_append(headers, "Content-Length:0");  
headers = curl_slist_append(headers, "Ocp-Apim-Subscription-Key:1df7903956d34f5991a65c645e464033");   
code = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);


char *url;
code = curl_easy_setopt(curl, CURLOPT_URL, "https://api.cognitive.microsoft.com/sts/v1.0/issueToken");
code = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, downloadCallback);
code = curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
code = curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
code = curl_easy_setopt(curl, CURLOPT_POST, 1); 

2.使用标准的规定的请求方式设置header,body

code2 = curl_global_init(CURL_GLOBAL_DEFAULT);
curl2 = curl_easy_init();
struct curl_slist *headers2 = NULL;
headers2 = curl_slist_append(headers2, tokenStr); 
headers2 = curl_slist_append(headers2, "Content-Type:application/x-www-form-urlencoded"); 
headers2 = curl_slist_append(headers2, "X-Microsoft-OutputFormat:riff-8khz-8bit-mono-mulaw");
headers2 = curl_slist_append(headers2, mydata);
headers2 = curl_slist_append(headers2, mydata_2); 
headers2 = curl_slist_append(headers2, "user-agent: bingspeech-api-client");
headers2 = curl_slist_append(headers2, "Cache-Control: no-cache");
headers2 = curl_slist_append(headers2, "Postman-Token: c6251495-a532-3b22-6fe0-1034f544f581"); 


code2 = curl_easy_setopt(curl2, CURLOPT_CUSTOMREQUEST, "POST");
code2 = curl_easy_setopt(curl2, CURLOPT_URL, Posturl);

hunk.memory = (char *)malloc(1);  /* will be grown as needed by the realloc above */ 
chunk.size = 0;    /* no data at this point */ 


code2 = curl_easy_setopt(curl2, CURLOPT_HTTPHEADER, headers2);
code2 = curl_easy_setopt(curl2, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
code2=curl_easy_setopt(curl2, CURLOPT_WRITEDATA, (void *)&chunk);
code2 = curl_easy_setopt(curl2, CURLOPT_POSTFIELDS,audio_data);
code2 = curl_easy_setopt(curl2, CURLOPT_SSL_VERIFYPEER, 0L);

CURLcode retcCode2 = curl_easy_perform(curl2);


其中有几个注意的地方是回调函数.详情参考:https://curl.haxx.se/libcurl/c/CURLOPT_WRITEFUNCTION.html



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值