在此期间研究了好多天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