轉載:http://blog.csdn.net/dgyanyong/article/details/14166217
轉載:http://blog.csdn.net/th_gsb/article/details/42810007
轉載:http://www.cnblogs.com/yangxunpeng/articles/7040697.html
轉載:http://blog.csdn.net/wzx19840423/article/details/6587370
第一步:建立控制台工程,配置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 <iostream> #include <sstream> //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) << data << endl; return size * nmemb; }
方式一:用JsonCpp構建Json串 //POST json int main() { CURL *curl; CURLcode res; char tmp_str[256] = { 0 }; std::stringstream out; //HTTP報文頭 struct curl_slist* headers = NULL; char *url = "http://if.qdocument.net:705/bic/operationNote/upload"; curl = curl_easy_init(); if(curl) { //構建json Json::Value item; item["uid"]=Json::Value("chechenluoyang@163.com"); item["fileName"]=Json::Value("梅西&內馬爾&蘇亞雷斯.txt"); item["time"]=Json::Value("2017.07.31 9:55:22"); item["type"]=Json::Value("Libcurl HTTP POST Json串"); item["authList"]=Json::Value("weidong0925@126.com"); std::string jsonout = item.toStyledString(); jsonout = AsciiToUtf8(jsonout); //設置url curl_easy_setopt(curl, CURLOPT_URL, url); //設置http發送的內容類型為JSON //構建HTTP報文頭 sprintf_s(tmp_str, "Content-Length: %s", jsonout.c_str()); headers=curl_slist_append(headers, "Content-Type:application/json;charset=UTF-8"); //headers=curl_slist_append(headers, tmp_str);//在請求頭中設置長度,請求會失敗,還沒找到原因 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); //curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");//自定義請求方式 curl_easy_setopt(curl, CURLOPT_POST, 1);//設置為非0表示本次操作為POST // 設置要POST的JSON數據 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, jsonout.c_str()); curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, jsonout.size());//設置上傳json串長度,這個設置可以忽略 // 設置接收數據的處理函數和存放變量 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);//設置回調函數 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &out);//設置寫數據 res = curl_easy_perform(curl);//執行
curl_slist_free_all(headers); /* free the list again */
string str_json = out.str();//返回請求值
printf("%s",str_json.c_str());
/* always cleanup */
curl_easy_cleanup(curl);
}
return 0;
}
方式二:手動拼Json串
//POST json int main() { CURL *curl; CURLcode res; char szJsonData[1024]; //HTTP報文頭 struct curl_slist* headers = NULL; char *url = "http://if.qdocument.net:705/bic/operationNote/upload"; curl = curl_easy_init(); if(curl) { //string類型的json串 memset(szJsonData, 0, sizeof(szJsonData)); std::string strJson = "{"; strJson += "\"uid\" : \"chechenluoyang@163.com\","; strJson += "\"fileName\" : \"梅西.txt\","; strJson += "\"time\" : \"2017.07.28 10:55:22\","; strJson += "\"type\" : \"Libcurl HTTP POST JSON \","; strJson += "\"authList\" : \"123\""; strJson += "}"; strcpy(szJsonData, strJson.c_str()); strJson = string AsciiToUtf8(strJson);//如果json串中包含有中文,必須進行轉碼 std::wstring wstrJson = _T("{"); wstrJson += _T("\"uid\" : \"chechenluoyang@163.com\","); wstrJson += _T("\"fileName\" : \"梅西.txt\","); wstrJson += _T("\"time\" : \"2017.07.29 10:55:22\","); wstrJson += _T("\"type\" : \"Libcurl HTTP POST JSON \","); wstrJson += _T("\"authList\" : \"test\""); wstrJson += _T("}"); string testJson = UnicodeToUtf8(wstrJson); std::stringstream out; //設置url curl_easy_setopt(curl, CURLOPT_URL, url); // 設置http發送的內容類型為JSON //構建HTTP報文頭 headers=curl_slist_append(headers, "Content-Type:application/json;charset=UTF-8"); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); //curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST"); curl_easy_setopt(curl, CURLOPT_POST, 1);//設置為非0表示本次操作為POST // 設置要POST的JSON數據
//curl_easy_setopt(curl, CURLOPT_POSTFIELDS, testJson.c_str());//以多字節上傳 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, testJson.c_str());//以Unicode編碼上傳 //curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strJson.size()); // 設置接收數據的處理函數和存放變量 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);//設置回調函數 curl_easy_setopt(curl, CURLOPT_WRITEDATA, &out);//設置寫數據 res = curl_easy_perform(curl);
curl_slist_free_all(headers); /* free the list again */
string str_json = out.str();//返回值 例如:{"status":"ok"}
printf("%s",str_json.c_str());
/* always cleanup */
curl_easy_cleanup(curl);
}
return 0;
}
后記:上傳的Json串,必須以UTF-8的方式上傳,因為服務器端是以UTF-8進行編碼顯示,按說發送過去的Json中中文顯示亂碼,但用libcurl庫直接請求失敗,還有就是我在手動拼Json串是格式錯誤,它們都會返回"客戶端發送的請求在語法上不正確"
在解決這個問題過程中,想到一個思路,讓源碼文件的編碼為UTF-8,然后直接上傳含有中文的json(不進行編碼轉換),發現還是不行。
以string類型保存的json,編碼轉換流程: Ascii--->Unicode--->UTF-8
以wstring類型保存的json,編碼轉換流程:Unicode--->UTF-8