如何在c中的curl post请求中设置cookie

时间:2015-01-23 09:04:09

标签: c post curl cookies

我已经以这种方式将cookie设置为HTTP头。

     .....
    CURL *curl;
    CURLcode res;

    struct curl_slist *headers = NULL;
    headers = curl_slist_append(headers, "Accept: application/x-www-form-urlencoded");
    headers = curl_slist_append(headers, "Content-Type: application/x-www-form-urlencoded");
    headers = curl_slist_append(headers, "charsets: utf-8");

    curl_global_init(CURL_GLOBAL_ALL);
    curl = curl_easy_init();
    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
    curl_easy_setopt(curl,CURLOPT_HTTPHEADER,headers);
    curl_easy_setopt(curl, CURLOPT_URL,"http://localhost:9763/addApp.jag");
    curl_easy_setopt(curl, CURLOPT_POST, 1);
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS,reqbody);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, body_callback);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &data);
    curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, header_callback);
    curl_easy_setopt(curl, CURLOPT_HEADERDATA,&cookiec);
    curl_easy_setopt(curl, CURLOPT_COOKIE,cookie);
    res=curl_easy_perform(curl);
    ......

在服务器端,当我检索发布数据时,它们似乎为空。但是当我从Wireshark检查时,实际上发送了这些后期数据。但是当我删除

    curl_easy_setopt(curl, CURLOPT_COOKIE,cookie);

我可以访问服务器端的帖子数据。如果我在发布请求中犯了任何错误,请纠正我。

1 个答案:

答案 0 :(得分:0)

实际上找到答案有点复杂。因为当我设置cookie时,应该排除来自服务器端的cookie值中的\ r \ n字符。如果有人想这样做,那么示例代码

   struct response_data *makeCURLRequest(char* reqbody,char* cookie,int  memorySize,char* url){
                    CURL *curl;
                    struct url_data data;
                    data.size = 0;
                    data.data = malloc(memorySize);
                    if(NULL == data.data) {

                                fprintf(stderr, "Failed to allocate memory.\n");
                                return NULL;
                    }
                    struct site_cookie cookiec;
                    cookiec.size=0;
                    cookiec.cookie=malloc(256);
                    if(NULL == cookiec.cookie) {
                                fprintf(stderr, "Failed to allocate memory.\n");
                                return NULL;
                    }
                    struct response_data* res_data;
                    res_data=malloc(sizeof(struct response_data));
                    res_data->res_body=malloc(1000);
                    res_data->res_cookie=malloc(256);
                    if(NULL == res_data->res_body || NULL==res_data->res_cookie) {
                                fprintf(stderr, "Failed to allocate memory.\n");
                                return NULL;
                    }
                     struct curl_slist *headers = NULL;
                     headers = curl_slist_append(headers, "Accept: Subscription/x-www-form-urlencoded");
                     headers = curl_slist_append(headers, "Content-Type: Subscription/x-www-form-urlencoded");
                     headers = curl_slist_append(headers, "charsets: utf-8");
                    data.data[0] = '\0';
                    cookiec.cookie[0]= '\0';
                    res_data->res_body[0]='\0';
                    res_data->res_cookie[0]='\0';
                    buf_t *buf = buf_size(NULL, BUFFER_SIZE);
                    CURLcode res;
                    curl_global_init(CURL_GLOBAL_ALL);
                    curl = curl_easy_init();
                    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
                    curl_easy_setopt(curl, CURLOPT_URL,url);
                    curl_easy_setopt(curl, CURLOPT_POST, 1);
                    curl_easy_setopt(curl, CURLOPT_POSTFIELDS,reqbody);
                    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fetch_data);
                    curl_easy_setopt(curl, CURLOPT_WRITEDATA, buf);
                    curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, header_callback);
                    curl_easy_setopt(curl, CURLOPT_HEADERDATA,&cookiec);
                    curl_easy_setopt(curl, CURLOPT_COOKIE,cookie);
                    res=curl_easy_perform(curl);
                    if(res != CURLE_OK){
                        fprintf(stderr, "curl_easy_perform() failed: %s\n",curl_easy_strerror(res));
                        return NULL;
                    }
                     curl_easy_cleanup(curl);
                     char *jsonstr = buf_tostr(buf);
                     free(buf->data);
                     free(buf);
                     strcpy(res_data->res_body,jsonstr);
                     strcpy(res_data->res_cookie,cookiec.cookie);
            return res_data;
  } 

完整的源代码可以在https://github.com/GPrathap/REST_API_in_C/blob/master/libs/curlreq.c

找到