인터넷의 html 페이지 요청하여 가져오는 방법.
MFC의 CInternetSession 를 사용하여 가져옴.
(winsock2 으로 직접 요청하여 가져오는 방법은 복잡 불편)


인터넷 HTML 페이지 다운로드 코드 (Project는 유니코드 캐릭터 셋 사용)
CString theUrl = _T("http://naver.com");  //가져올 HTML 주소
CInternetSession session;
CHttpFile* file;

try
{
//naver 접속 하여 HTML 획득
file = (CHttpFile*) session.OpenURL(theUrl, 1, INTERNET_FLAG_TRANSFER_BINARY | INTERNET_FLAG_RELOAD); 
CString strHtml = _T("");
TCHAR buf[1024];
while (TRUE)
{
TCHAR* strRet = file->ReadString(buf, 1024);
if (strRet == NULL) break;

strHtml += ConvertUTF8toUnicode((char*)strRet); //웹페이지는 charset=utf-8이므로 unicode로 변경
}

//파일로 저장
HANDLE hFile = CreateFile(_T("c:\\naver.htm"), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
DWORD dwWriteBytes;
int len = strHtml.GetLength();
WORD wd = 0xFEFF;  //유니코드 파일임을 나타냄
WriteFile(hFile, &wd, 2, &dwWriteBytes, NULL);
WriteFile(hFile, strHtml.GetBuffer(len), len * sizeof(TCHAR), &dwWriteBytes, NULL);
CloseHandle(hFile);
}
catch (CInternetException* e)
{
CString errmsg;
errmsg.Format(_T("[ERROR] context: %lu, error : %lu"), e->m_dwContext, e->m_dwError);
AfxMessageBox(errmsg);
e->Delete();
} 

UTF-8을 UNICODE로 변경
CString ConvertUTF8toUnicode(char* ansiStr)
{
int nSize = MultiByteToWideChar(CP_UTF8, 0, ansiStr ,  -1 , 0 , 0);

TCHAR* uniStr = new TCHAR[nSize+1];
MultiByteToWideChar(CP_UTF8, 0, ansiStr ,  -1 , uniStr, nSize);
uniStr[nSize] = '\0';

CString str(uniStr);
delete uniStr;
return str;
} 

인터넷에 설명은 많은데 Visual Studio가 unicode 캐릭터 셋이 기본이 되기 전의 설명이라
한글 깨지는 것 때문에 한참 고민하였습니다. (다 깨졌다가 한글만 깨졌다가...)