網站首頁 編程語言 正文
如題
很久不搞C++了,最近應工作要求,在VS2010下開發(fā)mfc客戶端程序。因為涉及到客戶端訪問服務端,必須通過賬戶登錄來確保安全性,所以需要客戶端登錄拿到服務端返回的token,然后客戶端后續(xù)所有url請求都要攜帶token去訪問。
這塊功能用的是httpclient 和 curl庫,網上資料很多,這里不做過多敘述,但也給出一個get/post請求函數的寫法:
Get()
// 向服務器發(fā)出get請求
int CHttpClient::Get(const std::string & strUrl, std::string & strResponse)
{
CURLcode res;
CURL* curl = curl_easy_init();
//打印Get命令
//logprint(strUrl.c_str());
if(NULL == curl)
{
return CURLE_FAILED_INIT;
}
if(m_bDebug)
{
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
}
curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Content-type: application/json;charset='utf-8'");
// 轉換CString為const char*,作為httpheader一部分
std::string cstrToken ="Authorization:" + GLOBAL_TOKEN;
headers = curl_slist_append(headers, cstrToken.c_str()); 請求頭添加token校驗身份
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
/**
* 當多個線程都使用超時處理的時候,同時主線程中有sleep或是wait等操作。
* 如果不設置這個選項,libcurl將會發(fā)信號打斷這個wait從而導致程序退出。
*/
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 8);//連接超時,這個數值如果設置太短可能導致數據請求不到就斷開了
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10);//接收數據時超時設置,如果10秒內數據未接收完,直接退出
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
//打印輸出結果
// logprint("httpclient raw std string data=",strResponse.c_str());
return res;
}
Post()
// 向服務器發(fā)出post請求
int CHttpClient::Post(const std::string & strUrl, const std::string & strPost, std::string & strResponse/*,bool bStructHttppost,struct curl_httppost* httppost*/)
{
CURLcode res;
CURL* curl = curl_easy_init();
if(NULL == curl)
{
return CURLE_FAILED_INIT;
}
if(m_bDebug)
{
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
}
curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
curl_easy_setopt(curl, CURLOPT_POST, 1);
// 以下是請求頭部分
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Content-type: application/json;charset='utf-8'");
// 轉換CString為const char*,作為httpheader一部分
std::string cstrToken ="Authorization:" + GLOBAL_TOKEN;
headers = curl_slist_append(headers, cstrToken.c_str()); 請求頭添加token校驗身份
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, strPost.c_str());
curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 8);//連接超時,這個數值如果設置太短可能導致數據請求不到就斷開了
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 30);//接收數據時超時設置,如果10秒內數據未接收完,直接退出
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
return res;
}
全局引用變量
一開始,我定義了一個extern GLOBAL_TOKEN變量放在一個公共頭文件中,然后在需要引用的地方引用,但是報錯:無法解析的外部符號。經過一番查找資料,偶然看到一位老哥的解決方法:
打開類視圖管理器,找到 theApp這個變量,因為任何一個mfc程序都會有它,然后照貓畫虎,在該文件.cpp中定義好如int var1;,然后在該文件的頭文件末尾聲明extern int var1; 最后即可在任意地方引用該變量了。
原文鏈接:https://blog.csdn.net/qq_36256590/article/details/122987948
相關推薦
- 2022-12-09 shell腳本實現(xiàn)Hbase服務的監(jiān)控報警和自動拉起問題_linux shell
- 2022-11-22 Linux實現(xiàn)自動掛載autofs的方法詳解_Linux
- 2022-05-05 Entity?Framework管理一對一實體關系_實用技巧
- 2023-10-15 webrtc 測試video_loopback
- 2022-07-25 Python?APScheduler?定時任務詳解_python
- 2022-11-10 Android傳感器的簡單使用方法_Android
- 2022-10-19 報錯No?module?named?numpy問題的解決辦法_python
- 2022-04-04 npm run ...自動打開瀏覽器
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支