2015-01-23 2 views
0

쿠키를 HTTP 헤더로 설정했습니다.C에서 컬링 요청에 쿠키를 설정하는 방법

 ..... 
    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); 
    ...... 

서버 측에서는 게시 된 데이터를 검색 할 때 null 인 것으로 보입니다. 그러나 실제로 Wireshark에서 확인하면 해당 게시물 데이터가 전송됩니다. 하지만 제거 할 때

curl_easy_setopt(curl, CURLOPT_COOKIE,cookie); 

나는 서버 측의 게시물 데이터에 액세스 할 수 있습니다. 내 게시물 요청에 실수를하면 나를 바로 잡으십시오.

답변

0

실제로이 문제에 대한 답변을 찾기 란 다소 복잡합니다. 쿠키를 설정할 때 서버 측에서 오는 쿠키 값의 \ r \ n 문자를 제외시켜야하기 때문입니다. 누군가가 여기에이 작업을 수행하려면 전체 소스 코드 https://github.com/GPrathap/REST_API_in_C/blob/master/libs/curlreq.c

여기에서 찾을 수 있습니다

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; 
    } 

샘플 코드를입니다

관련 문제