64,654
社区成员
发帖
与我相关
我的任务
分享
CString GetHtml(CHAR* strFormData){
CString str;
CString info;
try{
CInternetSession session;
session.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, 1000 * 20);
session.SetOption(INTERNET_OPTION_CONNECT_BACKOFF, 1000);
session.SetOption(INTERNET_OPTION_CONNECT_RETRIES, 1);
CHttpConnection* pConnection = session.GetHttpConnection( TEXT("www.test.cn"),//www.test.cn localhost
(INTERNET_PORT)80);
CHttpFile* pFile = pConnection->OpenRequest( CHttpConnection::HTTP_VERB_POST,
TEXT("/data.ashx"),
NULL,
1,
NULL,
TEXT("HTTP/1.1"),
INTERNET_FLAG_RELOAD);
//需要提交的数据
CString szHeaders = L"Content-Type: application/x-www-form-urlencoded;charset=UTF-8";
//下面这段编码,则是可以让服务器正常处理
//CHAR* strFormData = "username=0&password=0";
pFile->SendRequest( szHeaders,
szHeaders.GetLength(),
(LPVOID)strFormData,
strlen(strFormData));
DWORD dwRet;
pFile->QueryInfoStatusCode(dwRet);
try{
char szBuff[1025];
UINT nRead = 0;
while ((nRead = pFile->Read(szBuff,1024))>0)
{ info+=CString(szBuff,nRead);
}
}catch(char* value){}
session.Close();
pFile->Close();
delete pFile;
// AfxMessageBox(info);
return info;
}catch(char* value){}
return info;
}
CFile* pFile = NULL;
// Constructing a CFile object with this override may throw
// a CFile exception and won't throw any other exceptions.
// Calling CString::Format() may throw a CMemoryException,
// so we have a catch block for such exceptions, too. Any
// other exception types this function throws will be
// routed to the calling function.
TRY
{
pFile = new CFile(_T( "C:\\WINDOWS\\SYSTEM.INI"),
CFile::modeRead | CFile::shareDenyNone);
ULONGLONG dwLength = pFile->GetLength();
CString str;
str.Format(_T("Your SYSTEM.INI file is %I64u bytes long.") , dwLength);
AfxMessageBox(str);
}
CATCH(CFileException, pEx)
{
// Simply show an error message to the user.
pEx->ReportError();
}
AND_CATCH(CMemoryException, pEx)
{
// We can't recover from this memory exception, so we'll
// just terminate the app without any cleanup. Normally,
// an application should do everything it possibly can to
// clean up properly and not call AfxAbort().
AfxAbort();
}
END_CATCH
// If an exception occurs in the CFile constructor,
// the language will free the memory allocated by new
// and will not complete the assignment to pFile.
// Thus, our cleanup code needs to test for NULL.
if (pFile != NULL)
{
pFile->Close();
delete pFile;
}
CString GetHtml(CHAR* strFormData)
{
CString str;
CString info;
CInternetSession session;
CHttpConnection* pConnection = NULL;
CHttpFile* pFile = NULL;
try
{
session.SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, 1000 * 20);
session.SetOption(INTERNET_OPTION_CONNECT_BACKOFF, 1000);
session.SetOption(INTERNET_OPTION_CONNECT_RETRIES, 1);
pConnection = session.GetHttpConnection(TEXT("www.test.cn"), (INTERNET_PORT)80);
pFile = pConnection->OpenRequest( CHttpConnection::HTTP_VERB_POST,
TEXT("/data.ashx"),
NULL,
1,
NULL,
TEXT("HTTP/1.1"),
INTERNET_FLAG_RELOAD);
CString szHeaders = L"Content-Type: application/x-www-form-urlencoded;charset=UTF-8";
pFile->SendRequest( szHeaders,
szHeaders.GetLength(),
(LPVOID)strFormData,
strlen(strFormData));
DWORD dwRet;
pFile->QueryInfoStatusCode(dwRet);
char szBuff[1025];
UINT nRead = 0;
while ((nRead = pFile->Read(szBuff,1024))>0)
{
info+=CString(szBuff,nRead);
}
}
catch (CInternetException *pe)
{
pe->Delete();
if (pFile)
{
pFile->Close();
delete pFile;
}
if (pConnection)
{
pConnection->Close();
delete pConnection;
}
session.Close();
}
return info;
}
CInternetSession、CHttpConnection、CHttpFile这组API会抛出CInternetException异常,需要处理CInternetException异常