C中的curl_easy_perform程序

时间:2016-03-02 06:20:27

标签: c curl

现在,在对代码进行更改之后,我可以将POST resquest发送到SERVER,甚至服务器也在发送响应。但现在我的程序挂起curl_easy_perform函数。你有什么想法吗?

curl -H“授权:基本dGVzdENsaWVudDp0ZXN0U2VjcmV0”-X POST“http://x.x.x.x:8080/authservice/token?grant_type=client_credential

我的计划:

struct url_data {
    size_t size;
    char* data;
};
static size_t write_data(char *buffer, size_t size, size_t nitems, void *userp)
{
    struct url_data *data = (struct url_data *)userp;
    size_t index = data->size;
    size_t n = (size * nitems);
    data->size += (size * nitems);
    char* tmp = realloc(data->data, data->size + 1);
    if(tmp) {
        data->data = tmp;
    } else {
        if(data->data) free(data->data);
        fprintf(stderr, "Failed to allocate memory.\n");
        return EXIT_FAILURE;
    }
    memcpy(data->data + index, buffer, n);
    data->data[data->size] = '\0';
    return size * nitems;
}

int main(void)
{
    int i = 0;
    long http_code = 0;
    CURL *curl;
    CURLcode res;
    struct curl_slist *slist=NULL;

    struct url_data data;
    data.size = 0;
    data.data = (char*) malloc(4096); /* reasonable size initial buffer */
    bzero(data.data, 4096);
    //curl_global_init(CURL_GLOBAL_ALL);

    curl = curl_easy_init();
    if(curl) {
        curl_easy_setopt(curl, CURLOPT_URL, "http://x.x.x.x:8080/authservice/token?grant_type=client_credential");
        curl_easy_setopt(curl, CURLOPT_POST, 1L);
        curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");
        curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &data);
        curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10);


        slist = curl_slist_append(slist, "Authorization: Basic dGVzdENsaWVudDp0ZXN0U2VjcmV0");
        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist);
        slist = curl_slist_append(slist, "Content-Type:application/json");
        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist);
        slist = curl_slist_append(slist, "Content-Length: 0");
        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist);
        slist = curl_slist_append(slist, "Expect:");
        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist);

        curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);

        res = curl_easy_perform(curl);
        /* Check for errors */
        if(res != CURLE_OK) {
            fprintf(stderr, "curl_easy_perform() failed: %s\n",     curl_easy_strerror(res));
        } else {
            fprintf(stderr, "Data:[%s] Ret:[%s]\n", data.data, curl_easy_strerror(res));
        }
        res = curl_easy_getinfo (curl, CURLINFO_HTTP_CODE, &http_code);
        printf("http_code:%ld\n", http_code);
        /* always cleanup */
        curl_easy_cleanup(curl);
        if(data.data) free(data.data);

        curl_slist_free_all(slist);
    }
    //curl_global_cleanup();
    return 0;
}

输出:

* About to connect() to x.x.x.x port 8080 (#0)
*   Trying x.x.x.x...
* connected
* Connected to x.x.x.x (x.x.x.x) port 8080 (#0)
> POST /authservice/token?grant_type=client_credential HTTP/1.1
User-Agent: libcurl-agent/1.0
Host: x.x.x.x:8080
Accept: */*
Authorization: Basic dGVzdENsaWVudDp0ZXN0U2VjcmV0
Content-Type:application/json
Content-Length: 0

0 个答案:

没有答案