原先写过的代码,现在转移到自己博客上面以备参考。毕竟当初也是查了资料总结出来的方法。 先介绍字节字符集的实现。
UNICODE工程的
CString CServerSession::httpGetRequest(CString url)
{
CInternetSession m_InetSession;
CString strHtml;
try{
CHttpFile* pFile =(CHttpFile*)m_InetSession.OpenURL(url);
if (pFile!=NULL)
{
DWORD statusCode;
pFile->QueryInfoStatusCode(statusCode);
if (statusCode==HTTP_STATUS_OK)
{
CString strLine;
while (pFile->ReadString(strLine)>0)
strHtml += strLine;
}
}
m_InetSession.Close();
pFile->Close();
delete pFile;
}
catch (CInternetException* e){
TCHAR info[1024] = {0};
e->GetErrorMessage(info,1024);
AfxMessageBox(info);
}
catch (CMemoryException* e)
{
TCHAR info[1024] = {0};
e->GetErrorMessage(info,1024);
AfxMessageBox(info);
}
catch (CFileException* e)
{
TCHAR info[1024] = {0};
e->GetErrorMessage(info,1024);
AfxMessageBox(info);
}
catch (CException* e)
{
TCHAR info[1024] = {0};
e->GetErrorMessage(info,1024);
AfxMessageBox(info);
}
return strHtml;
}
CString CServerSession::httpPostRequest(CString requestData,CString pageUrl)
{
CInternetSession m_InetSession; //设置不缓冲
CHttpConnection* pServer = NULL;
CHttpFile* pFile = NULL;
CString strHtml;
//POST过去的数据requestData
try{
INTERNET_PORT nPort; //端口
nPort=80;
pServer = m_InetSession.GetHttpConnection("127.0.0.1", nPort);
pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_POST,pageUrl);
pFile->AddRequestHeaders("Content-Type: application/x-www-form-urlencoded");
pFile->AddRequestHeaders("Accept: */*");
pFile->SendRequestEx(requestData.GetLength());
pFile->WriteString(requestData);
pFile->EndRequest();
DWORD dwRet;
pFile->QueryInfoStatusCode(dwRet);
if (dwRet == HTTP_STATUS_OK){
CString strLine;
while (pFile->ReadString(strLine)>0)
strHtml += strLine;
}
m_InetSession.Close();
pFile->Close();
pServer->Close();
delete pFile;
delete pServer;
}
catch (CInternetException* e){
TCHAR info[1024] = {0};
e->GetErrorMessage(info,1024);
AfxMessageBox(info);
}
catch (CMemoryException* e)
{
TCHAR info[1024] = {0};
e->GetErrorMessage(info,1024);
AfxMessageBox(info);
}
catch (CFileException* e)
{
TCHAR info[1024] = {0};
e->GetErrorMessage(info,1024);
AfxMessageBox(info);
}
catch (CException* e)
{
TCHAR info[1024] = {0};
e->GetErrorMessage(info,1024);
AfxMessageBox(info);
}
return strHtml;
}
UNICODE工程的
CString CFDemoDlg::httpGetRequest(CString szUrl)
{
CInternetSession m_InetSession;
CString strHtml;
try{
CHttpFile* pFile =(CHttpFile*)m_InetSession.OpenURL(szUrl);
if (pFile!=NULL)
{
DWORD statusCode;
pFile->QueryInfoStatusCode(statusCode);
if (statusCode==HTTP_STATUS_OK)
{
TCHAR buf[1024]= {0};
char tmp[2048] = {0};
while(pFile->ReadString(buf, 1024))
{
for(int i=0, j=0; i<lstrlen(buf); i++, j+=2)
{
tmp[j] = LOBYTE(buf[i]);//取字的低字节
tmp[j+1] = HIBYTE(buf[i]);//取字的高字节
}
CString ss(tmp);
strHtml+=ss;
}
}
}
m_InetSession.Close();
pFile->Close();
if(pFile)delete pFile;
}
catch (CInternetException* e){
TCHAR info[1024] = {0};
e->GetErrorMessage(info,1024);
MessageBox(info);
}
catch (CException* e)
{
TCHAR info[1024] = {0};
e->GetErrorMessage(info,1024);
MessageBox(info);
}
return strHtml;
}
CString CFDemoDlg::httpPostRequest(CString szUrl,CString szPage, char* pWriteData)
{
CInternetSession m_InetSession; //设置不缓冲
CHttpConnection* pServer = NULL;
CHttpFile* pFile = NULL;
CString strHtml;
//POST过去的数据requestData
try{
INTERNET_PORT nPort; //端口
nPort=3000;
pServer = m_InetSession.GetHttpConnection(szUrl, nPort);
pFile = pServer->OpenRequest(CHttpConnection::HTTP_VERB_POST,szPage);
pFile -> AddRequestHeaders(_T("Content-Type: application/x-www-form-urlencoded"));
pFile -> AddRequestHeaders(_T("Accept: */*"));
pFile->SendRequest(NULL,NULL,pWriteData,(DWORD)strlen(pWriteData));
DWORD dwRet;
pFile->QueryInfoStatusCode(dwRet);
if (dwRet == HTTP_STATUS_OK){
TCHAR buf[1024]= {0};
char tmp[2048] = {0};
while(pFile->ReadString(buf, 1024))
{
for(int i=0, j=0; i<lstrlen(buf); i++, j+=2)
{
tmp[j] = LOBYTE(buf[i]);//取字的低字节
tmp[j+1] = HIBYTE(buf[i]);//取字的高字节
}
DWORD dwUnicodeLen; //转换后Unicode的长度
//获得转换后的长度,并分配内存
dwUnicodeLen = MultiByteToWideChar(CP_UTF8,0,tmp,-1,NULL,0);
WCHAR* pWChar = new WCHAR[dwUnicodeLen];
MultiByteToWideChar(CP_UTF8,0,tmp,-1,pWChar,dwUnicodeLen);
strHtml += pWChar;
//清除内存
delete pWChar;
}
}
m_InetSession.Close();
if(pServer){pServer->Close();delete pServer;}
if(pFile){pFile->Close();delete pFile;}
}
catch (CException* e)
{
TCHAR info[1024] = {0};
e->GetErrorMessage(info,1024);
MessageBox(info);
}
return strHtml;
}

收藏的用户(0) X
正在加载信息~
推荐阅读
最新回复 (0)
站点信息
- 文章2314
- 用户1336
- 访客11812443
每日一句
Let's seek joy in the simple, quiet moments.
让我们在简单宁静的时刻中寻找快乐。
让我们在简单宁静的时刻中寻找快乐。