c调用libcurl库发送GET 和 POST请求

版权声明:本文为博主原创文章,转载请提提我 https://blog.csdn.net/hushiganghu/article/details/86636493

libcrul请求的基本套路流程

  • 1、调用curl_global_init()初始化libcurl
  • 2、调用curl_easy_init()函数得到 easy interface型指针
  • 3、调用curl_easy_setopt()设置传输选项
  • 4、根据curl_easy_setopt()设置的传输选项,实现回调函数以完成用户特定任务
  • 5、调用curl_easy_perform()函数完成传输任务
  • 6、调用curl_easy_cleanup()释放内存

过程中关键的是用curl_easy_setopt()设置参数

示例代码

GET

/*
参考POST代码
当不调用CURLOPT_POST,和CURLOPT_POSTFIELDS,默认使用GET方式
某些服务器只支持其中一种方式
*/

POST

struct MemoryStruct	
{		
    char *memory;		
    size_t size; 
};
static size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb,	void *userp) 
{		
    size_t	realsize =	size * nmemb;		
    struct MemoryStruct *mem = (struct MemoryStruct *)userp;
	mem->memory	= realloc(mem->memory, mem->size + realsize	+ 1);		
    if(mem->memory	==	NULL)	
    {				
        /*	out	of	memory!	*/				
        printf("not enough memory (realloc returned NULL)\n");				
        return 0;
	}
	memcpy(&(mem->memory[mem->size]), contents,	realsize);		
    mem->size += realsize;		
    mem->memory[mem->size] = 0;
    
    int i=0;
    printf("\ncontents:");
    for(i=0;i<realsize;i++)
    {
        printf("%c",mem->memory[i]);
    }printf("\n");

	return realsize; 
}

/*
curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' 'http://rdtest.myhiott.com:8490/xyStoryDeviceServer/signRecord?deviceid=1'
Request URL:
    http://rdtest.myhiott.com:8490/xyStoryDeviceServer/signRecord?deviceid=1
*/
int set_sign_record(char * device_id,char *msg)
{
    int ret = 0;
    int info = 0;
    CURL *curl;
    CURLcode res;
    struct curl_slist *headers=NULL; 
    char *url = "http://rdtest.myhiott.com:8490/xyStoryDeviceServer/signRecord?deviceid=";

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

    char *Request_URL = (char *) malloc(strlen(url) + strlen(device_id) + 1);
    strcpy(Request_URL, url);
    strcat(Request_URL, device_id);

     curl = curl_easy_init();
     if (curl)
     {
        headers = curl_slist_append(headers, "Content-Type: application/json");
        headers = curl_slist_append(headers, "Accept: application/json");
        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);//设置HTTP头
        curl_easy_setopt(curl, CURLOPT_URL, Request_URL);//设置post请求的url地址
        //curl_easy_setopt(curl, CURLOPT_TIMEOUT, 1);//设置发送超时时间
        curl_easy_setopt(curl,	CURLOPT_WRITEFUNCTION,	WriteMemoryCallback);
        curl_easy_setopt(curl,	CURLOPT_WRITEDATA,	(void	*)&chunk);
        curl_easy_setopt(curl,CURLOPT_POSTFIELDS,"");
        curl_easy_setopt(curl, CURLOPT_POST, 1); 
        curl_easy_setopt(curl,CURLOPT_VERBOSE,1);//打印调试信息

        res = curl_easy_perform(curl);
        curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE  , &info);
        if(res != CURLE_OK || info != 200)
        {
            ret = 1;
            printf( "!crul failed to perform url='%s' res=[%d] rcode=%d\n",Request_URL, res,info);
        }else{
            printf("%lu	bytes retrieved\n", (long)chunk.size);
        }
       // _get_post_msg(chunk.memory,msg);

        curl_slist_free_all(headers);
        curl_easy_cleanup(curl);
     }else{
         ret = 2;
     }
    free(chunk.memory);
    free(Request_URL);
    return ret;
}

参考

https://www.cnblogs.com/moodlxs/archive/2012/10/15/2724318.html

猜你喜欢

转载自blog.csdn.net/hushiganghu/article/details/86636493