linux下发送http协议请求

来源:互联网 发布:中国知名的php培训机构 编辑:程序博客网 时间:2024/05/21 10:41

一、使用准备条件,添加文件头#include <curl/curl.h> ,在makefile文件中添加库lcurl


二、写代码

size_t writedata(char *ptr, size_t size, size_t nmemb, string * page){    if(page == NULL)    {        return 0;    }    page->append(ptr, size*nmemb);    return size*nmemb;}//发送post的http消息int sendpostrequest(){   CURL* curl;   CURLcode code;   curl    = curl_easy_init();   string response;   if(curl)   {       curl_easy_setopt(curl, CURLOPT_URL, "http:xxxxxxxxxxxxxxxx");       curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10);        //设置超时时间        curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L);       //屏蔽其它信号       code = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);       if (code != CURLE_OK)       {           return -1;       }       code = curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);       code = curl_easy_perform(curl);       curl_easy_cleanup(curl);       if (code != CURLE_OK)       {           return -1;       }   } else {        return -1;   }}



0 0
原创粉丝点击