您当前的位置: 首页 >  服务器

鱼儿-1226

暂无认证

  • 1浏览

    0关注

    1100博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

libcurl HTTP POST请求向服务器发送json数据

鱼儿-1226 发布时间:2020-07-25 11:02:18 ,浏览量:1

第一步:建立控制台工程,配置libcurl

           在stdafx.h中导入引用的libcurl库,在用的是静态链接

 

.......

#define CURL_STATICLIB

#include "curl\curl.h"

#ifdef _DEBUG
#pragma comment(lib,"libcurld.lib")
#else
#pragma comment(lib,"libcurl.lib")
#endif

#pragma comment ( lib, "ws2_32.lib" )
#pragma comment ( lib, "winmm.lib" )
#pragma comment ( lib, "wldap32.lib" )
#pragma comment(lib, "Advapi32.lib")

........

库文件的位置

第二步:配置JsonCpp库,如果想在工程中直接引用源码,请参考我之前的博客

第三步:上传json串

#include "stdafx.h"
#include 
#include 
//json
#include "json\json.h"
using namespace std;


//http://blog.csdn.net/wyansai/article/details/50764315
wstring AsciiToUnicode(const string& str) 
{
    // 预算-缓冲区中宽字节的长度  
    int unicodeLen = MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, nullptr, 0);
    // 给指向缓冲区的指针变量分配内存  
    wchar_t *pUnicode = (wchar_t*)malloc(sizeof(wchar_t)*unicodeLen);
    // 开始向缓冲区转换字节  
    MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, pUnicode, unicodeLen);
    wstring ret_str = pUnicode;
    free(pUnicode);
    return ret_str;
}

string UnicodeToUtf8(const wstring& wstr) 
{
    // 预算-缓冲区中多字节的长度  
    int ansiiLen = WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, nullptr, 0, nullptr, nullptr);
    // 给指向缓冲区的指针变量分配内存  
    char *pAssii = (char*)malloc(sizeof(char)*ansiiLen);
    // 开始向缓冲区转换字节  
    WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), -1, pAssii, ansiiLen, nullptr, nullptr);
    string ret_str = pAssii;
    free(pAssii);
    return ret_str;
}


string AsciiToUtf8(const string& str) 
{
    return UnicodeToUtf8(AsciiToUnicode(str));
}

//回调函数
size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream) 
{
    string data((const char*) ptr, (size_t) size * nmemb);

    *((stringstream*) stream) UTF-8

以wstring类型保存的json,编码转换流程:Unicode--->UTF-8

关注
打赏
1604459285
查看更多评论
立即登录/注册

微信扫码登录

0.0471s