본문 바로가기

Programming

Http File Upload Code (C++)

336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

#include "afxinet.h"

void CTEST1Dlg::HttpPutFile(LPCTSTR wszServerURL, LPCTSTR wszRemoteFilePath, LPCTSTR wszLocalFilePath, UINT nPort, LPCTSTR wszLoginUserID, LPCTSTR wszLoginPassword )
{
/*
TCHAR wszServerURL[100]        = _T("192.168.0.2");
TCHAR wszServerURL[100]        = _T("abc.com");
TCHAR wszLocalFilePath[100]    = _T("\\Program Files\\aaa\\abc");
TCHAR wszRemoteFilePath[100]   = _T("/aaaa/abc.bmp");
TCHAR wszLoginUserID[100]      = _T("");
TCHAR wszLoginPassword[100]    = _T("");
TCHAR nPort                    = 80;
*/

HINTERNET hInternet, hURL, hRequest;

// connection
hInternet = InternetOpen( _T("HTTPFILE"), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if ( hInternet == NULL )
{
TRACE(  _T("Not Connected...(internet)"));
return; 
}

// url
hURL = InternetConnect( hInternet, wszServerURL, nPort, wszLoginUserID, wszLoginPassword, 
INTERNET_SERVICE_HTTP, 0, 0);
if ( hURL==NULL )
{
InternetCloseHandle( hInternet );
TRACE( _T("Not Connected... (internet service http)"));
return;
}

INTERNET_BUFFERS        iBuf = {0};
iBuf.dwStructSize = sizeof( INTERNET_BUFFERS );

hRequest = HttpOpenRequest( hURL, _T("PUT"), wszRemoteFilePath, NULL, NULL, NULL, 0, 0 );
if ( !hRequest )
{
InternetCloseHandle( hURL );
InternetCloseHandle( hInternet );
TRACE(_T("Fail to HttpOpenRequest function"));
return;
}

//  Open local file
HANDLE hFile;
hFile = CreateFile( wszLocalFilePath, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
if ( hFile == INVALID_HANDLE_VALUE )
{
TRACE(_T("Fail to Read File"));
InternetCloseHandle( hRequest );
InternetCloseHandle( hInternet );
InternetCloseHandle( hURL );
return;
}


DWORD dwWritten;
DWORD dwFileSize, dwFilePos=0, dwRead; 

dwFileSize = GetFileSize( hFile, NULL );
iBuf.dwBufferTotal = dwFileSize;

if( !HttpSendRequestEx( hRequest, &iBuf, NULL, HSR_INITIATE, 0 ) )
{
TRACE(_T("Fail to HttpSendRequestEx Function"));
CloseHandle( hFile );
InternetCloseHandle( hRequest );
InternetCloseHandle( hInternet );
InternetCloseHandle( hURL );
return;
}

char buf[512];
DWORD cnt=0;
dwFilePos = 0;
do
{
SetFilePointer( hFile, dwFilePos, NULL, FILE_BEGIN );
if( !ReadFile( hFile, buf, sizeof(buf), &dwRead, NULL) )
{
TRACE(_T("Fail to Read Local file"));
break;
};

InternetSetFilePointer( hRequest, dwFilePos, NULL, FILE_BEGIN, 0 );
if( !InternetWriteFile( hRequest, buf, dwRead, &dwWritten ) )
{
TRACE(_T("Fail to Remote File Write"));
break;
};
dwFilePos += dwWritten;

} while ( dwRead == sizeof(buf) );


CloseHandle( hFile );

if( !HttpEndRequest( hRequest, NULL, 0, 0 ) )
{
TRACE(_T("Fail to HttpEndRequest Function"));
}

InternetCloseHandle( hRequest );
InternetCloseHandle( hInternet );
InternetCloseHandle( hURL );

}

================================================================================================


(주의) Web Service Extensions 설정 중 WebDAV 를 Allowed 로 setting 하여야 한다.




(개인 저장용 자료입니다. 위 코드를 작성하기 전 참고 한 소스는 제 코드가 아님을 알려드립니다.)

'Programming' 카테고리의 다른 글

MFC DLL의 resource 찾는 순서  (0) 2009.04.28
WebDEV  (0) 2009.04.23
오픈소스 라이센스 정리 (GPL, LGPL, BSD, MPL, QPL)  (0) 2009.04.16