怎样使用CHttpFile读取Unicode编码的网页

本文介绍了一种使用CHttpFile的Read方法而非ReadString方法来正确读取Unicode编码网页的方法。通过循环读取并存储字节,再将字节转换为TCHAR字符串,确保了Unicode内容能够被正确解析。

 如果使用CHttpFile的ReadString方法读取Unicode编码的网页,结果是读出的数据不正确,感觉是ReadString方法在Unicode下实现错误,因此只能使用Read方法。

  const int size = 1024;
  byte pByte[size];
  SecureZeroMemory(pByte,size);
  int count = 0;
  vector<byte> vecByte;
  CString strUpdateInfo;
  while( ( count = pFile->Read(pByte,size) ) > 0 )
  {
   for(int i = 0; i < count; ++i)
   {
    vecByte.push_back(pByte[i]);
   }
   SecureZeroMemory(pByte,size);
   if( count < size )
    break;
  }

  if( vecByte.size() > 0 )
  {
   byte * pB = new byte[vecByte.size()];
   copy(vecByte.begin(),vecByte.end(),pB);

   TCHAR * pChr = (TCHAR*)pB;

   strUpdateInfo = pChr;

   delete [] pB;
  }
  else
  {
   strUpdateInfo = _T("");
  }

auto deleter = [](CHttpFile* p) { if (p) delete p; }; std::unique_ptr<CHttpFile, decltype(deleter)> pFile( m_pConnection->OpenRequest(CHttpConnection::HTTP_VERB_POST, m_GFunction.String2CString(apiPath)), deleter); // 设置请求头 //Post请求 /* Content - Type: application / json Authorization : Basic Auth 用户名 : master 密码 : 88Jjlh$&efwObdDQ*/ CString strHeaders = _T("Content-Type: application/json\r\n"); strHeaders += _T("Authorization: Basic Auth") + EncodeBasicAuth(m_GFunction.String2CString(m_username), m_GFunction.String2CString(m_password)) + _T("\r\n"); // 转换请求数据 CString strData = m_GFunction.String2CString(info); addInfo( "请求头:" + m_GFunction.CString2String(strHeaders) + "请求体:" + info); pFile->SendRequest ( strHeaders, (DWORD)strHeaders.GetLength(), (LPVOID)(LPCTSTR)strData, (DWORD)strData.GetLength() ); DWORD dwStatusCode; pFile->QueryInfoStatusCode(dwStatusCode); CString strResponse; if (dwStatusCode == HTTP_STATUS_OK) { TCHAR szBuffer[1024]; while (pFile->Read(szBuffer, 1023) > 0) { strResponse += szBuffer; } } if (dwStatusCode != HTTP_STATUS_OK) { resp.Code = HTTP_ERROR; resp.Msg = QString::number(dwStatusCode); return resp; } resp.Msg = QString::fromLocal8Bit(m_GFunction.CString2String(strResponse).c_str()); pFile->Close(); } catch (CInternetException* pEx) { TCHAR szError[1024]; pEx->GetErrorMessage(szError, 1024); resp.Msg = QString::fromLocal8Bit(m_GFunction.CString2String(szError).c_str());// 异常信息写入resultMSG pEx->Delete(); resp.Code = NETWORK_ERROR; return resp; } resp.Code = SUCCESS; return resp;
09-29
``` #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; }```该使用boost
04-03
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值