您的位置:首页 > 编程语言 > C语言/C++

C++ 使用post/get进行发送数据

2017-09-18 19:25 405 查看
使用开源的libcur实现post发送数据。libcur的使用请自行百度。下面是我的代码,用post发送过个类型的数据

注意:代码不能直接使用,因为我里面依赖了其他文件和类,这里这是做一个示例

//httpcurl.h
#include <string>
#include "SqlOperation.h"

class  CFaceRecognice;

class CHttpClient
{
public:
CHttpClient(CSqlOperation* sqloperate);
~CHttpClient(void);

public:
int Post(const std::string strUrl, int found_int, string guid_str, BYTE* feat_byte, BYTE* photo_byte, long picsize);

private:
bool m_bDebug;
CSqlOperation* m_sqloperate;
};
//httpcurl.cpp
#include "stdafx.h"
#include "httpcurl.h"
#include "curl.h"
#include "json/json.h"

CHttpClient::CHttpClient(CSqlOperation* sqloperate)   //CSqlOperation 是我其他类中自己定义的一个类,可以不传入
:m_bDebug(false)
,m_sqloperate(sqloperate)
{

}

CHttpClient::~CHttpClient(void)
{

}

string Utf82Ansi(const char* utf8)
{

int WLength = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, NULL);
LPWSTR pszW = (LPWSTR)_alloca((WLength + 1) *sizeof(WCHAR));
MultiByteToWideChar(CP_UTF8, 0, utf8, -1, pszW, WLength);
pszW[WLength] = 0;

int ALength = WideCharToMultiByte(CP_ACP, 0, pszW, -1, NULL, 0, NULL, NULL);
LPSTR pszA = (LPSTR)_alloca(ALength + 1);
WideCharToMultiByte(CP_ACP, 0, pszW, -1, pszA, ALength, NULL, NULL);
pszA[ALength] = 0;
return pszA;
}

//post返回调用的函数,如果没定义此函数则会有一个默认的函数
//如果需要给OnWriteData传递指针,则传递给lpVoid
void OnWriteData(void* buffer, size_t size, size_t nmemb, void* lpVoid)
{

if (NULL == buffer)
{
return;
}

Json::Reader reader;
Json::Value json_value;
if (reader.parse((char*)buffer, json_value))  // reader将Json字符串解析到root,root将包含Json里所有子元素
{
if (!json_value["found_c"].isNull())
{
int face_found = json_value["found_c"].asInt();
if (!face_found) //客户端数据库中没有找到face信息,认为是新人
{
if (!json_value["guid_c"].isNull() && !json_value["guid_s"].isNull())
{
string guid_c = Utf82Ansi(json_value["guid_c"].asString().c_str());
string guid_s = Utf82Ansi(json_value["guid_s"].asString().c_str());
if (strcmp(guid_c.c_str(), guid_s.c_str()) != 0)
{
CSqlOperation* sql = (CSqlOperation*)lpVoid;
sql->UpdateGUID(guid_c, guid_s);    //此处更新数据库
}
}
}
}
}
return;
}

char* Unicode2Utf8(const TCHAR* unicode)
{
int len;
len = WideCharToMultiByte(CP_UTF8, 0, (const wchar_t*)unicode, -1, NULL, 0, NULL, NULL);
char *szUtf8 = (char*)malloc(len + 1);
memset(szUtf8, 0, len + 1);
WideCharToMultiByte(CP_UTF8, 0, (const wchar_t*)unicode, -1, szUtf8, len, NULL, NULL);
return (char*)szUtf8;
}

BOOL StringToWString0(const std::string &str, std::wstring &wstr)
{
int nLen = (int)str.length();
wstr.resize(nLen, L' ');

int nResult = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)str.c_str(), nLen, (LPWSTR)wstr.c_str(), nLen);

if (nResult == 0)
{
return FALSE;
}

return TRUE;
}
/*
post发送了三种类型的数据

found:整形数据
guid:字符型数据
feat:二进制字节流数据
photo:二进制字节流数据,其实是一个图片
*/

int CHttpClient::Post(const std::string strUrl, int found_int, string guid_str, BYTE* feat_byte, BYTE* photo_byte,long picsize)
{
CURLcode res;

struct curl_httppost *formpost = NULL;
struct curl_httppost *lastptr = NULL;

CURL* curl = curl_easy_init();
if (NULL == curl)
{
return CURLE_FAILED_INIT;
}

if (found_int)
{
curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, Unicode2Utf8(_T("found")), CURLFORM_COPYCONTENTS, "1", CURLFORM_END);
}
else
{
curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, Unicode2Utf8(_T("found")), CURLFORM_COPYCONTENTS, "0", CURLFORM_END);
}

wstring wguid;

StringToWString0(guid_str, wguid);

curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, Unicode2Utf8(_T("guid")), CURLFORM_COPYCONTENTS, Unicode2Utf8(wguid.c_str()), CURLFORM_END);

curl_formadd(&formpost, &lastptr,
CURLFORM_COPYNAME, Unicode2Utf8(_T("feat")),
CURLFORM_BUFFER, "data",
CURLFORM_BUFFERPTR, feat_byte,
CURLFORM_BUFFERLENGTH, sizeof(float)*2048,
CURLFORM_END);

curl_formadd(&formpost, &lastptr,
CURLFORM_COPYNAME, Unicode2Utf8(_T("photo")),
CURLFORM_BUFFER, "data",
CURLFORM_BUFFERPTR, photo_byte,
CURLFORM_BUFFERLENGTH, sizeof(float)* 2048,
CURLFORM_END);
string url = "http://127.0.0.1:8080/apiReport.do"; //这是我的url地址,根据自己的需求做更改

struct curl_slist *headers = NULL;
//设置post的一些相关信息
headers = curl_slist_append(headers, "POST");
headers = curl_slist_append(headers, "Connection: Keep-Alive");
headers = curl_slist_append(headers, "Accept: */*");
headers = curl_slist_append(headers, "Content-Type: multipart/form-data;");

curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());

curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);

curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
curl_easy_setopt(curl, CURLOPT_WRITE
a8a2
FUNCTION, OnWriteData);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, m_sqloperate); //给OnWriteData传递的指针参数
res = curl_easy_perform(curl);
curl_formfree(formpost);
curl_easy_cleanup(curl);
return res;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐