一、GET 數據,下載網頁,文件等,用于可下載的文件,不能用于服務端運行的程序,比如.aspx文件等,否則會返回500錯誤。
CString strSentence, strWriteName="1.htm";
CString strFileName="http://localhost/InDesign/" + strWriteName;

CInternetSession sess;
CHttpFile* fileGet;
try

{
fileGet=(CHttpFile*)sess.OpenURL(strFileName);
}
catch(CException* e)

{
fileGet = 0;
throw;
}

if(fileGet)

{
DWORD dwStatus;
DWORD dwBuffLen = sizeof(dwStatus);
BOOL bSuccess = fileGet->QueryInfo(HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &dwStatus, &dwBuffLen);

if( bSuccess && dwStatus>= 200&& dwStatus<300 )

{
CStdioFile fileWrite;
if(fileWrite.Open(strWriteName, CFile::modeWrite|CFile::modeCreate))

{
while(fileGet->ReadString(strSentence))

{
fileWrite.WriteString(strSentence+"\n");
}
fileWrite.Close();
AfxMessageBox("下載完畢");
}
else

{
AfxMessageBox("本地文件"+strWriteName+"打開出錯.");
}
}
else

{
strSentence.Format("打開網頁文件出錯,錯誤碼:%d", dwStatus);
AfxMessageBox(strSentence);
}
fileGet->Close();
delete fileGet;
}
else
AfxMessageBox("不能找到網頁文件!");

sess.Close();二、POST 數據,比如用于提交注冊信息等
CString strHttpName="http://localhost/TestReg/RegForm.aspx"; // 需要提交數據的頁面
CString strFormData = "username=abc&password=123"; // 需要提交的數據

CInternetSession sess;
CHttpFile* fileGet;
CString strHeaders = _T("Content-Type: application/x-www-form-urlencoded"); // 請求頭

try

{
fileGet=(CHttpFile*)sess.OpenURL(strHttpName);//打開文件
}
catch(CException* e)

{
fileGet = 0;
throw;
}

CString strSentence, strGetSentence = "";
if(fileGet)

{
DWORD dwStatus;
DWORD dwBuffLen = sizeof(dwStatus);
BOOL bSuccess = fileGet->QueryInfo(HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &dwStatus, &dwBuffLen);
if( bSuccess && dwStatus>= 200 &&dwStatus<300 )

{
BOOL result = fileGet->SendRequest(strHeaders, (LPVOID)(LPCTSTR)strFormData, strFormData.GetLength());
while(fileGet->ReadString(strSentence)) // 讀取提交數據后的返回結果

{
strGetSentence = strGetSentence + strSentence + char(13) + char(10);
}
AfxMessageBox(strGetSentence); // 顯示返回網頁內容
}
else

{
strSentence.Format("POST出錯,錯誤碼:%d", dwStatus);
AfxMessageBox(strSentence);
}
fileGet->Close();
delete fileGet;
}
else
AfxMessageBox("不能找到網頁文件!");

sess.Close();