这里我使用django搭建了简单的http web服务器,具体步骤百度一大堆!去libcurl官网下载最新的开源库就行。
1. LibcurlHttpResquest.hpp
#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);
string httpGetReturnString(string url);
/*****'POST'方式请求*****/
void httpPostDefault(string url,string data);
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
2. LibcurlHttpResquest.cpp
#include "LibcurlHttpResquest.hpp"
LibcurlHttpResquest::LibcurlHttpResquest()
{
}
LibcurlHttpResquest::~LibcurlHttpResquest()
{
}
LibcurlHttpResquest *LibcurlHttpResquest::getInstance()
{
LibcurlHttpResquest *http = new LibcurlHttpResquest();
if (http)
{
return http;
}
return nullptr;
}
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;
}
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;
}
3. main.cpp
#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);
}