使用curl发送post请求,示例代码,返回结果正确:
#include <stdio.h>
#include <curl/curl.h>
int main(void) {
CURL *curl; CURLcode res;
// 初始化winsock的内容
curl_global_init(CURL_GLOBAL_DEFAULT);
//*获得一个curl的手柄(handle)
curl = curl_easy_init();
if(curl) {
/* 设置post请求地址,最好使用https,第三个参数是url,即网址 */
curl_easy_setopt(curl, CURLOPT_URL, “https://www.wangsansan.com/mydir/test/HttpsPostTest.php”);
//设置post传输的信息,第三个参数即传输的字符串
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, “{“A”:“111”, “B”:“222”}”);
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, “curl_easy_perform() failed: %s\n”, curl_easy_strerror(res));
//结束curl_easy_cleanup 结束一个libcurl简易句柄 curl_easy_cleanup(curl); }
cur