Code Library/VC++ | Posted by 서비 2007/01/19 10:59

HttpGet

const INT BUF_SIZE = 2048576;
TCHAR g_pBuffer[BUF_SIZE] = {0};

// 지정된 URL(strURL) 에서 소스코드를 얻는다(strSourceCode)

BOOL HttpGet(const CString& strURL, CString& strSourceCode)
{
    HINTERNET hInternet;
    hInternet = InternetOpen(_T("GBHConnect"), INTERNET_OPEN_TYPE_DIRECT, 0, 0, 0);

    if( hInternet == NULL )
    {
        ASSERT(0);
        return FALSE;
    }

    CString strHeader = _T("Accept: */*\'\r\n\'Accept-Charset: ks_c_5601-1987\'\r\n\'");
    strHeader += _T("Accept-Language: ko\'\r\n\'Content-Encoding: ks_c_5601-1987");

    HINTERNET hInternetRequest;
    hInternetRequest = InternetOpenUrl( hInternet, strURL, strHeader, 0, INTERNET_FLAG_RELOAD, 0 );
    if( NULL == hInternetRequest )
    {
        ASSERT(0);
        return FALSE;
    }

    strSourceCode = _T("");
    BOOL bOK;

    const INT READ_SIZE = 10240;

    DWORD dwTotalReadByte = 0;
    while( TRUE )
    {
        DWORD dwNumOfBytesRead;
        bOK = InternetReadFile( hInternetRequest, &g_pBuffer[dwTotalReadByte], READ_SIZE, &dwNumOfBytesRead);
        if( FALSE == bOK ||  0 == dwNumOfBytesRead )
        {
            break;
        }
        dwTotalReadByte += dwNumOfBytesRead;


        if( bOK == FALSE )
        {
            ASSERT(0);
            InternetCloseHandle( hInternetRequest );
            InternetCloseHandle( hInternet );
            return FALSE;
        }
    }
    InternetCloseHandle( hInternetRequest );
    InternetCloseHandle( hInternet );

    g_pBuffer[dwTotalReadByte+1] = '\0';
    strSourceCode = g_pBuffer;

    return TRUE;
}
TAG