记录使用CInternetSession 的一个error

1>g:\microsoft visual studio 10.0\vc\atlmfc\include\afxinet.h(166): error C2248: “CObject::operator =”: 无法访问 private 成员(在“CObject”类中声明)
1>          g:\microsoft visual studio 10.0\vc\atlmfc\include\afx.h(535) : 参见“CObject::operator =”的声明
1>          g:\microsoft visual studio 10.0\vc\atlmfc\include\afx.h(509) : 参见“CObject”的声明
1>          此诊断出现在编译器生成的函数“CInternetSession &CInternetSession::operator =(const CInternetSession &)”中

 

原因在于我在代码里使用了

CInternetSession m_pSession;

m_pSession =NULL;//就是这一句,不能这么使用

 

微软将Object类的操作运算符设置为private,非要使用到话,重载操作符。

参考https://blog.youkuaiyun.com/bao_bei/article/details/46966067

``` #include "pch.h" #include "CSocketClientJNMES.h" #include "Loger.h" extern CGlobalFunction m_GFunction; // 全局函数 extern Loger m_Loger; // 全局Log类 enum ResultCode { SUCCESS = 0, NETWORK_ERROR = -1, HTTP_ERROR = -2 }; CSocketClientJNMES::CSocketClientJNMES() { } CSocketClientJNMES::~CSocketClientJNMES() { } int CSocketClientJNMES::SendMTFData(const string & info, string & resultMSG, const string & hostname, const int & port, const string & apiPath) { CInternetSession session(_T("MyPostAgent")); try { // 使用智能指针自动管理资源 auto deleter = [](auto p) { if (p) delete p; }; std::unique_ptr<CHttpConnection, decltype(deleter)> pServer(session.GetHttpConnection( m_GFunction.String2CString(hostname), port, _T(""), _T("")), deleter); std::unique_ptr<CHttpFile, decltype(deleter)> pFile(pServer->OpenRequest( CHttpConnection::HTTP_VERB_POST, m_GFunction.String2CString(apiPath)), deleter); CHttpConnection* pServer = session.GetHttpConnection ( m_GFunction.String2CString(hostname), port, _T(""), _T("") ); m_Loger.RecordLogTcpClientJNURL("hostname:" + hostname + "port:" + to_string(port) + "CHttpConnection* pServer"); m_Loger.RecordLogTcpClientJNURL(apiPath + "CHttpFile* pFile = pServer->OpenRequest"); CString strHeaders = _T("Content-Type: application/json"); CString strData = m_GFunction.String2CString(info); m_Loger.RecordLogTcpClientJNURL("info:" + info); pFile->SendRequest ( strHeaders, (DWORD)strHeaders.GetLength(), (LPVOID)(LPCTSTR)strData, (DWORD)strData.GetLength() ); m_Loger.RecordLogTcpClientJNURL("pFile->SendRequest"); DWORD dwStatusCode; pFile->QueryInfoStatusCode(dwStatusCode); CString strResponse; if (dwStatusCode == HTTP_STATUS_OK) { TCHAR szBuffer[1024]; while (pFile->Read(szBuffer, 1023) > 0) { strResponse += szBuffer; } AfxMessageBox(strResponse); } if (dwStatusCode != HTTP_STATUS_OK) { resultMSG = "HTTP Error: " + std::to_string(dwStatusCode); return HTTP_ERROR; } resultMSG = m_GFunction.CString2String(strResponse); m_Loger.RecordLogTcpClientJNURL("resultMSG:" + resultMSG); pFile->Close(); } catch (CInternetException* pEx) { TCHAR szError[1024]; pEx->GetErrorMessage(szError, 1024); resultMSG = m_GFunction.CString2String(szError); // 异常信息写入resultMSG m_Loger.RecordLogTcpClientJNURL("EXCEPTION: " + resultMSG); pEx->Delete(); return NETWORK_ERROR; } return 0; }```保持使用MFC
04-03
``` #include "pch.h" #include "CSocketClientJNMES.h" #include "Loger.h" extern CGlobalFunction m_GFunction; // 全局函数 extern Loger m_Loger; // 全局Log类 enum ResultCode { SUCCESS = 0, NETWORK_ERROR = -1, HTTP_ERROR = -2 }; CSocketClientJNMES::CSocketClientJNMES() { } CSocketClientJNMES::~CSocketClientJNMES() { } int CSocketClientJNMES::SendMTFData(const string & info, string & resultMSG, const string & hostname, const int & port, const string & apiPath) { CInternetSession session(_T("MyPostAgent")); try { // 使用智能指针自动管理资源 auto deleter = [](auto p) { if (p) delete p; }; std::unique_ptr<CHttpConnection, decltype(deleter)> pServer(session.GetHttpConnection( m_GFunction.String2CString(hostname), port, _T(""), _T("")), deleter); std::unique_ptr<CHttpFile, decltype(deleter)> pFile(pServer->OpenRequest( CHttpConnection::HTTP_VERB_POST, m_GFunction.String2CString(apiPath)), deleter); CHttpConnection* pServer = session.GetHttpConnection ( m_GFunction.String2CString(hostname), port, _T(""), _T("") ); m_Loger.RecordLogTcpClientJNURL("hostname:" + hostname + "port:" + to_string(port) + "CHttpConnection* pServer"); m_Loger.RecordLogTcpClientJNURL(apiPath + "CHttpFile* pFile = pServer->OpenRequest"); CString strHeaders = _T("Content-Type: application/json"); CString strData = m_GFunction.String2CString(info); m_Loger.RecordLogTcpClientJNURL("info:" + info); pFile->SendRequest ( strHeaders, (DWORD)strHeaders.GetLength(), (LPVOID)(LPCTSTR)strData, (DWORD)strData.GetLength() ); m_Loger.RecordLogTcpClientJNURL("pFile->SendRequest"); DWORD dwStatusCode; pFile->QueryInfoStatusCode(dwStatusCode); CString strResponse; if (dwStatusCode == HTTP_STATUS_OK) { TCHAR szBuffer[1024]; while (pFile->Read(szBuffer, 1023) > 0) { strResponse += szBuffer; } AfxMessageBox(strResponse); } if (dwStatusCode != HTTP_STATUS_OK) { resultMSG = "HTTP Error: " + std::to_string(dwStatusCode); return HTTP_ERROR; } resultMSG = m_GFunction.CString2String(strResponse); m_Loger.RecordLogTcpClientJNURL("resultMSG:" + resultMSG); pFile->Close(); } catch (CInternetException* pEx) { TCHAR szError[1024]; pEx->GetErrorMessage(szError, 1024); resultMSG = m_GFunction.CString2String(szError); // 异常信息写入resultMSG m_Loger.RecordLogTcpClientJNURL("EXCEPTION: " + resultMSG); pEx->Delete(); return NETWORK_ERROR; } return 0; }```帮我重写这个类,这个类通过URL通信,post请求将info这个json格式的字符串发送出去并拿到返回值给resultMSG
04-03
``` int CSocketClientJNMES::SendMTFData(const string & info, string & resultMSG, const string & hostname, const int & port, const string & apiPath) { CInternetSession session(_T("MyPostAgent")); try { CHttpConnection* pServer = session.GetHttpConnection ( m_GFunction.String2CString(hostname), port, _T(""), _T("") ); m_Loger.RecordLogTcpClientJNURL("hostname:" + hostname + "port:" + to_string(port) + "CHttpConnection* pServer"); CHttpFile* pFile = pServer->OpenRequest ( CHttpConnection::HTTP_VERB_POST, m_GFunction.String2CString(apiPath) ); m_Loger.RecordLogTcpClientJNURL(apiPath + "CHttpFile* pFile = pServer->OpenRequest"); CString strHeaders = _T("Content-Type: application/json"); CString strData = m_GFunction.String2CString(info); m_Loger.RecordLogTcpClientJNURL("info:" + info); pFile->SendRequest ( strHeaders, (DWORD)strHeaders.GetLength(), (LPVOID)(LPCTSTR)strData, (DWORD)strData.GetLength() ); m_Loger.RecordLogTcpClientJNURL("pFile->SendRequest"); DWORD dwStatusCode; pFile->QueryInfoStatusCode(dwStatusCode); CString strResponse; if (dwStatusCode == HTTP_STATUS_OK) { TCHAR szBuffer[1024]; while (pFile->Read(szBuffer, 1023) > 0) { strResponse += szBuffer; } AfxMessageBox(strResponse); } resultMSG = m_GFunction.CString2String(strResponse); m_Loger.RecordLogTcpClientJNURL("resultMSG:" + resultMSG); pFile->Close(); delete pFile; delete pServer; } catch (CInternetException* pEx) { TCHAR szError[1024]; pEx->GetErrorMessage(szError, 1024); AfxMessageBox(szError); pEx->Delete(); } return 0; }```分析代码并优化
最新发布
04-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值