unix网络编程之开源库libcurl的简单封装使用

前端之家收集整理的这篇文章主要介绍了unix网络编程之开源库libcurl的简单封装使用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这里我使用django搭建了简单的http web服务器,具体步骤百度一大堆!去libcurl官网下载最新的开源库就行。

1. LibcurlHttpResquest.hpp

//
// LibcurlHttpResquest.hpp
// HttpClient
//
// Created by LiYong on 2018/1/25.
//

#ifndef LibcurlHttpResquest_hpp
#define LibcurlHttpResquest_hpp

#include <stdio.h>
#include <curl/curl.h>
#include <iostream>
using namespace std;
struct WriteThis{
    const char *readptr;
    long sizeleft;
};
class LibcurlHttpResquest
{
public:
    LibcurlHttpResquest();
    ~LibcurlHttpResquest();
    static LibcurlHttpResquest *getInstance();
public:
    /******'GET'方式请求******/
    //1.将接收到得数据存到本地
    void httpGetSaveLocation(string url);
    //2.将接收到的数据作为string返回
    string httpGetReturnString(string url);

    /*****'POST'方式请求*****/
    //1.默认read function方式
    void httpPostDefault(string url,string data);
    //2.使用定制read function
    void httpPostCustom(string url,string data);

    /***custom function***/
    static size_t readData(void *buffer,size_t size,size_t nmemb,void *usedata);
    static size_t OnWriteData(void *buffer,void *usedata);

};
#endif /* LibcurlHttpResquest_hpp */

2. LibcurlHttpResquest.cpp

//
// LibcurlHttpResquest.cpp
// HttpClient
//
// Created by LiYong on 2018/1/25.
//

#include "LibcurlHttpResquest.hpp"
LibcurlHttpResquest::LibcurlHttpResquest()
{

}
LibcurlHttpResquest::~LibcurlHttpResquest()
{

}
LibcurlHttpResquest *LibcurlHttpResquest::getInstance()
{
    LibcurlHttpResquest *http = new LibcurlHttpResquest();
    if (http)
    {
        return http;
    }
    return nullptr;
}
//'GET'方式
void LibcurlHttpResquest::httpGetSaveLocation(string url)
{
    string body;
    string heady;
    CURL *curl;
    CURLcode res;
    FILE *file = fopen("./resp.html","wb");

    struct curl_slist *headers = NULL;
    headers = curl_slist_append(headers,"Accept:Agent-000");
    curl = curl_easy_init();
    if (curl)
    {
        curl_easy_setopt(curl,CURLOPT_HTTPHEADER,headers);
        curl_easy_setopt(curl,CURLOPT_URL,url.c_str());
        curl_easy_setopt(curl,CURLOPT_READFUNCTION,LibcurlHttpResquest::readData);
        curl_easy_setopt(curl,CURLOPT_HEADERDATA,file);
        curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,LibcurlHttpResquest::OnWriteData);
        curl_easy_setopt(curl,CURLOPT_WRITEDATA,CURLOPT_ACCEPT_ENCODING,"gzip");
        res = curl_easy_perform(curl);
        if (res != 0)
        {
            curl_slist_free_all(headers);
        }
    }
    fclose(file);
    curl_easy_cleanup(curl);
}
string LibcurlHttpResquest::httpGetReturnString(string url)
{
    string body;
    string heady;
    CURL *curl;
    CURLcode res;

    struct curl_slist *headers = NULL;
    headers = curl_slist_append(headers,(void *)&body);
        curl_easy_setopt(curl,"gzip");
        res = curl_easy_perform(curl);
        if (res != 0)
        {
            curl_slist_free_all(headers);
        }
    }
    curl_easy_cleanup(curl);
    return body;
}
//'POST'方式
void LibcurlHttpResquest::httpPostDefault(string url,string data)
{
    string resp_data;

    CURL *curl;
    CURLcode res;
    curl = curl_easy_init();
    if (curl)
    {
        curl_easy_setopt(curl,CURLOPT_POST,1);
        curl_easy_setopt(curl,(void *)&resp_data);
        curl_easy_setopt(curl,CURLOPT_READDATA,CURLOPT_POSTFIELDS,data.c_str());
        curl_easy_setopt(curl,CURLOPT_POSTFIELDSIZE,strlen(data.c_str()));
        curl_easy_setopt(curl,CURLOPT_TIMEOUT,1);
        res = curl_easy_perform(curl);
    }
    if (res == 0)
    {
        cout << "resp empty" << endl;
    }
    curl_easy_cleanup(curl);
}
void LibcurlHttpResquest::httpPostCustom(string url,string data)
{
    string resp_data;
    struct WriteThis pooh;
    pooh.readptr = data.c_str();
    pooh.sizeleft = (long)strlen(data.c_str());
    CURL *curl;
    CURLcode res;
    curl = curl_easy_init();
    if (curl)
    {
        curl_easy_setopt(curl,OnWriteData);
        curl_easy_setopt(curl,readData);

        curl_easy_setopt(curl,&pooh);
        curl_easy_setopt(curl,pooh.sizeleft);

        res = curl_easy_perform(curl);
    }
    curl_easy_cleanup(curl);
}
size_t LibcurlHttpResquest::OnWriteData(void *buffer,void *usedata)
{
    string *str = dynamic_cast<string *>((string *)usedata);
    if (NULL == str || NULL == buffer)
    {
        return -1;
    }
    char *pData = (char *)buffer;
    str->append(pData,size * nmemb);
    return nmemb;
}
size_t LibcurlHttpResquest::readData(void *buffer,void *usedata)
{
    struct WriteThis *data = (struct WriteThis *)usedata;
    if (data == NULL)
    {
        return -1;
    }
    if (size * nmemb < 1)
    {
        return 0;
    }
    if (data->sizeleft)
    {
        *(char *)buffer = data->readptr[0];
        data->readptr++;
        data->sizeleft--;
        return 1;
    }
    return 0;
}
//coustom function

3. main.cpp

//
// main.cpp
// HttpClient
//
// Created by LiYong on 2018/1/23.
//

#include <iostream>
#include "LibcurlHttpResquest.hpp"
int main()
{
    string url = "192.168.0.83:8000";
    string data = "Hello http server !";
    LibcurlHttpResquest *http = LibcurlHttpResquest::getInstance();

    string body = http->httpGetReturnString(url);
    if (body != "")
    {
        cout << body <<endl;
    }
    http->httpPostCustom(url,data);
}
原文链接:https://www.f2er.com/bash/388898.html

猜你在找的Bash相关文章