Chinaunix首页 | 论坛 | 博客
  • 博客访问: 844059
  • 博文数量: 643
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 4092
  • 用 户 组: 普通用户
  • 注册时间: 2016-11-11 09:12
  • 认证徽章:
个人简介

To be a better coder

文章分类

全部博文(643)

文章存档

2019年(198)

2018年(217)

2017年(147)

2016年(82)

分类: LINUX

2019-10-18 14:31:48

https://www.cnblogs.com/smallleiit/p/11646622.html

首先shell命令:
[root@controller /]# curl http://192.168.27.92:8080/stats/packets/arp -X POST -H "Content-Type:application/json" -d '{"dpid": 60447901079364,"count": 5,"packets": [{"port": 2,"vlan ": 24,"mac": "6c:92:bf:e0:36:e7","ip": "1.1.1.1"},]}'
[root@controller /]#

int license_url_request(char *url, char *data, int size)
{
    int ret = -1;
    CURLcode res;
    CURL *curl_h = NULL;
    long retcode = 0;
    struct mem_curl_data msg = { NULL, 0};
    struct curl_slist *list = NULL;

    if(url == NULL || data == NULL){
        return -1;
    }
    curl_global_init(CURL_GLOBAL_ALL);
        
    /* init the curl session */
    curl_h = curl_easy_init();
    
    if(strncmp(url, "https", 5) == 0){
        curl_easy_setopt(curl_h, CURLOPT_SSL_VERIFYPEER, 0);
        curl_easy_setopt(curl_h, CURLOPT_SSL_VERIFYHOST, 0);
    }
    
    /* specify URL to get */
    curl_easy_setopt(curl_h, CURLOPT_URL, url);
    /* set curl head */
    list = curl_slist_append(list, "Content-Type: application/json");
    list = curl_slist_append(list, "Accept: */*");
    curl_easy_setopt(curl_h, CURLOPT_HTTPHEADER, list);
    /* send all data to this function  */
    curl_easy_setopt(curl_h, CURLOPT_WRITEFUNCTION, agent_curl_read_data);
    /* we pass our 'chunk' struct to the callback function */
    curl_easy_setopt(curl_h, CURLOPT_WRITEDATA, (void *)&msg);
    /* get it! */
    res = curl_easy_perform(curl_h);
    curl_easy_getinfo(curl_h, CURLINFO_RESPONSE_CODE, &retcode);
    
    /* free cache */
    curl_slist_free_all(list);
    
    /* check for errors */
    if(res == CURLE_OK && retcode == 200) {
        ret = 0;
    } else {
        ret = -1;
    }
    
    if(msg.size <= size && msg.memory != NULL) {
        memset(data, 0x00, size);
        memcpy(data, msg.memory, msg.size);
        free(msg.memory);
    }
    
    curl_easy_cleanup(curl_h);
    
    curl_global_cleanup();
    
    return ret;
}

int license_reply(char *url, char *data, int size)
{
    struct curl_slist* headers = NULL;

    curl_global_init(CURL_GLOBAL_ALL);
    CURL *easy_handle = curl_easy_init();

/*
    if(strncmp(url, "https", 5) == 0){
        curl_easy_setopt(curl_h, CURLOPT_SSL_VERIFYPEER, 0);
        curl_easy_setopt(curl_h, CURLOPT_SSL_VERIFYHOST, 0);
    }
*/

    curl_easy_setopt(easy_handle, CURLOPT_URL, url);
    // 单个域post
    curl_easy_setopt(easy_handle, CURLOPT_POSTFIELDS, data);

    headers = curl_slist_append(headers, "Content-Type:application/json;charset=UTF-8");
    curl_easy_setopt(easy_handle, CURLOPT_HTTPHEADER, headers);
    curl_easy_setopt(easy_handle, CURLOPT_POST, 1);

    curl_easy_perform(easy_handle);

    curl_easy_cleanup(easy_handle);
    curl_global_cleanup();

    return 0;
}

阅读(2918) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议
请登录后评论。

登录 注册