curl post请求

libcurl发送post请求,包括httpheader参数

static size_t getCharCode(void *ptr, size_t size, size_t nmemb, void *userdata)
{
    string *version = (string*)userdata;
    version->append((char*)ptr, size * nmemb);
    return (size * nmemb);
}

void sendpost()
{
  string result = "";
    CURL *handle = curl_easy_init();
    if (! handle)
    {
        CCLOG("can not init curl");
        return;
    }
    curl_easy_setopt(handle, CURLOPT_URL, "you_url_path");
    curl_easy_setopt(handle, CURLOPT_HEADER, 0);
    curl_easy_setopt(handle, CURLOPT_POST, 1);
    curl_easy_setopt(handle, CURLOPT_POSTFIELDS,token.c_str());
    curl_easy_setopt(handle, CURLOPT_NOSIGNAL, 1L);
    
    struct curl_slist *slist  =  NULL;
    slist  =  curl_slist_append(slist, "param:1");
    slist  =  curl_slist_append(slist, "param:2");

    curl_easy_setopt(handle, CURLOPT_HTTPHEADER, slist);
    curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, getCharCode);
    curl_easy_setopt(handle, CURLOPT_WRITEDATA, &result);
    CURLcode res = curl_easy_perform(handle);
    if (res == CURLE_OK)
    {

    }
    curl_easy_cleanup(handle);
}

 

posted @ 2014-06-10 17:20  haroel  阅读(478)  评论(0编辑  收藏  举报