LPBYTE PostWebPage( LPCTSTR pszURL , LPBYTE pbPostData )
{
int result;
DWORD dwLength;
int nContentLength;
BYTE baBuffer[ 64 ];
DWORD dwServiceType;
INTERNET_PORT nPort;
HINTERNET hInternet = NULL;
HINTERNET hConnect = NULL;
HINTERNET hRequest = NULL;
CString sServer, sObject;
CString strHeaders = _T( "Content-Type: application/x-www-form-urlencoded" );
LPBYTE pbContent = NULL;
CString sLicenseXML;
LPSTR pszFirst = NULL, pszLast = NULL;
CString sLog ;
DWORD dwFlags = 0 ;
CString ss;
//
// URL 호출
//
hInternet = InternetOpen( _T( "Mozilla" ), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0 );
if( hInternet == NULL )
{
result = GetLastError();
goto finish;
}
result = AfxParseURL( pszURL, dwServiceType, sServer, sObject, nPort );
if( result == FALSE )
{
result = GetLastError();
goto finish;
}
hConnect = InternetConnect( hInternet, sServer, nPort, NULL, NULL, INTERNET_SERVICE_HTTP, 0, NULL );
if( hConnect == NULL )
{
result = GetLastError();
goto finish;
}
//
// 반드시 포스트 방식으로
//
hRequest = HttpOpenRequest( hConnect, _T( "POST" ), sObject, NULL, NULL, NULL,INTERNET_FLAG_NO_CACHE_WRITE , NULL );
if( hRequest == NULL )
{
result = GetLastError();
goto finish;
}
//
// 웹서버에 데이타를 전송
//
result = HttpSendRequest( hRequest, strHeaders, strHeaders.GetLength(), (LPVOID)pbPostData, strlen( (LPSTR)pbPostData ) );
if( result == FALSE )
{
result = GetLastError();
goto finish;
}
//
// HTTP STATUS 코드
//
dwLength = sizeof( baBuffer );
result = HttpQueryInfo( hRequest, HTTP_QUERY_STATUS_CODE, (LPVOID)baBuffer, &dwLength, NULL );
if( result == FALSE )
{
result = GetLastError();
goto finish;
}
if( strcmp( (LPCSTR)baBuffer, "200" ) != 0 )
{
//
// HTTP Request 가 실패했다. 왜???
//
//CString s = (LPCSTR)baBuffer;
result = ERROR_HTTP_INVALID_QUERY_REQUEST;
goto finish;
}
dwLength = sizeof( baBuffer );
result = HttpQueryInfo( hRequest, HTTP_QUERY_CONTENT_LENGTH , (LPVOID)baBuffer, &dwLength, NULL );
if( result == FALSE )
{
result = GetLastError();
goto finish;
}
nContentLength = atoi( (LPSTR)baBuffer );
pbContent = new BYTE[ nContentLength + 1 ];
if( pbContent == NULL )
{
result = ERROR_NOT_ENOUGH_MEMORY;
goto finish;
}
result = InternetReadFile( hRequest, pbContent, nContentLength, &dwLength );
if( result == FALSE )
{
result = GetLastError();
goto finish;
}
pbContent[ nContentLength ] = '\0';
result = 0;
finish:
if( hInternet != NULL )
InternetCloseHandle( hInternet );
if( hConnect != NULL )
InternetCloseHandle( hConnect );
if( hRequest != NULL )
InternetCloseHandle( hRequest );
return pbContent;
}
사용방법은 아래와 같이...
LPBTYEpbPostData = new BYTE[ 512 ];
CString strRes;
memset( pbPostData, 0, sizeof( BYTE ) * 512 );
sprintf( (char*)pbPostData, "id=%s&pw=%s", loginDlg.GetID(), strPass64 );
strRes = PostWebPage( 웹주소, pbPostData );
strRes에 Post방식으로 날린 웹페이지의 응답값이 들어온다. LPBYTE형을 리턴하지만, 자동형변환 되어 들어간다.
댓글 없음:
댓글 쓰기