有如下代码,用来向一个PHP页面提交请求,并根据返回信息得知是否成功。OpenURL没有问题,但是有时候Read到的东西都为空,而且,while循环中的语句会执行多次(长度跟正常是一样的,就是Read的东西都为空),但是将URL放在IE中,确是正确的,请各位分析原因。CInternetSession session("Reaction); CStdioFile* pFile = NULL;
char szBuff[1025] = {0};
ZeroMemory(szBuff, 1025); pFile = session.OpenURL(strURL);
if (pFile)
{
CString strRetContent = ""; while (pFile->Read(szBuff, 1024) > 0)
{
strRetContent += szBuff;
ZeroMemory(szBuff, 1025);
} strRetString = strRetContent; delete pFile;
pFile = NULL;
}
char szBuff[1025] = {0};
ZeroMemory(szBuff, 1025); pFile = session.OpenURL(strURL);
if (pFile)
{
CString strRetContent = ""; while (pFile->Read(szBuff, 1024) > 0)
{
strRetContent += szBuff;
ZeroMemory(szBuff, 1025);
} strRetString = strRetContent; delete pFile;
pFile = NULL;
}
if(NULL == pFileHttp)
{
goto EXIT;
} // get http file length
DWORD lSize = 0;
DWORD dwBufferLength = sizeof(lSize);
if(!pFileHttp->QueryInfo(HTTP_QUERY_FLAG_NUMBER | HTTP_QUERY_CONTENT_LENGTH, &lSize, &dwBufferLength) || 0 == lSize)
{
goto EXIT;
} // get http file mem
pBuf = new char[lSize];
pFileHttp->Read(pBuf, lSize);
试试,不过请问下,这个跟我的有啥不一样吗?我那边大小应该是没错,只是有时候多次read都是空内容。
CHttpFile *file = NULL;
CString strURL = _T("http://www.csdn.net");
CString strHtml = _T(""); //´æ·ÅÍøÒ³Êý¾Ý try
{
file = (CHttpFile*)session.OpenURL(strURL);
}
catch(CInternetException * m_pException)
{ file = NULL;
m_pException->m_dwError;
m_pException->Delete();
session.Close();
AfxMessageBox(_T("CInternetException"));
} CString strLine;
CString strHtmlgb2312;
char sRecived[1024];
if(file != NULL)
{
while(file->ReadString((LPTSTR)sRecived,1024) != NULL)
{
strHtmlgb2312 = ConvertUTF8toGB2312(sRecived,strlen(sRecived));
strHtml += strHtmlgb2312;
}
}
else
{ AfxMessageBox(_T("fail"));
}
CFile MyFile;
MyFile.Open("F:\\×Ô¼ºÐ´µÄ³ÌÐò\\Á·Ï°ÓóÌÐòÉè¼Æ\\NetTest\\Debug\\test.html",CFile::modeWrite | CFile::modeCreate,NULL);
MyFile.Write(strHtml,strHtml.GetLength());
MyFile.Close();
session.Close();
file->Close();
delete file;
file = NULL;
{
strRetContent += szBuff;
ZeroMemory(szBuff, 1025);
}
在VS2005中有问题, 改用ReadString和CString就没问题了。传说这是因为VS2005中CString类的一个bug导致的。如果系统升级会修复该bug。。
if(!pFileHttp->QueryInfo(HTTP_QUERY_FLAG_NUMBER | HTTP_QUERY_CONTENT_LENGTH, &lSize, &dwBufferLength)每次都失败。不知是啥原因。