newtype
::: newtype의 지식창고 :::
newtype
전체 방문자
오늘
어제
  • 분류 전체보기 (392)
    • Dev (214)
      • C, C++ (43)
      • Go (5)
      • Web (49)
      • DBMS (21)
      • DevOps (8)
      • Java (2)
      • Windows, Win32 (4)
      • Visual Basic (5)
      • C# (2)
      • Mobile (25)
      • SQL CE (7)
      • Google Map (6)
      • Python (2)
      • cygwin (2)
      • 기타 (32)
      • Ruby (1)
    • 명언 (10)
    • 모임 (18)
      • 붕주회 (3)
      • 신흥컴정 (14)
      • 웹20기 (1)
    • 사진 (8)
    • 불펌 (29)
    • 막글 (98)
    • 게임 (6)
    • 여행 (8)

블로그 메뉴

  • 홈
  • 태그
  • 방명록
  • 관리

공지사항

  • whoami
05-20 03:16
hELLO · Designed By 정상우.
newtype

::: newtype의 지식창고 :::

Dev/C, C++

Http프로토콜을 이용한 파일 업로드

2003. 1. 18. 14:23

http서버에 올리는 소스이기 때문에
당연히 서버에 업로드 권한이 있어야 합니다.


void HttpPutFile(LPCTSTR wszServerURL, LPCTSTR wszRemoteFilePath,  
                      LPCTSTR wszLocalFilePath, TCHAR nPort,  
                      LPCTSTR wszLoginUserID, LPCTSTR wszLoginPassword )  
{  
/*  
       TCHAR wszServerURL[100]         = L"solergy.com";  
       TCHAR wszLocalFilePath[100]     = L"\\Program Files\\hanaro\\Sign\_AS\\7250.bmp";  
       TCHAR wszRemoteFilePath[100]  = L"/hanaro/sign/as/7250.bmp";  
       TCHAR wszLoginUserID[100]       = L"";  
       TCHAR wszLoginPassword[100]  = L"";  
       TCHAR nPort                              = 80;  
*/  
       HINTERNET hInternet, hURL, hRequest;  

       // 연결  
       hInternet = InternetOpen( L"HTTPFILE", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);  
       if ( hInternet == NULL )  
       {  
               MessageBox( NULL, L"인터넷이 연결되어 있지 않습니다.", L"Warning", MB_OK);  
               return;  
       }  

       // url  
       hURL = InternetConnect( hInternet, wszServerURL, nPort,  
                                                       wszLoginUserID, wszLoginPassword,  
                                                       INTERNET_SERVICE_HTTP, 0, 0);  
       if ( hURL==NULL )  
       {  
               InternetCloseHandle( hInternet );  
               MessageBox( NULL, L"서버오류로 연결할 수 없습니다.", L"Warning", MB_OK);  
               return;  
       }  

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

       hRequest = HttpOpenRequest( hURL, L"PUT", wszRemoteFilePath, NULL, NULL, NULL, 0, 0 );  
       if ( !hRequest )  
       {  
               InternetCloseHandle( hURL );  
               InternetCloseHandle( hInternet );  
               MessageBox( NULL, L"Request open Failed", L"Warning", MB_OK);  
               return;  
       }  

       //  local file 오픈  
       HANDLE hFile;  
       hFile = CreateFile( wszLocalFilePath, GENERIC_READ, FILE_SHARE_READ, NULL,  
                                               OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );  
       if ( hFile == INVALID_HANDLE_VALUE )  
       {  
               MessageBox( NULL, L"Invalid read File", L"warning", MB_OK);  
               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 ) )  
       {  
               MessageBox( NULL, L"SendRequest Failed", L"warning", MB_OK);  
               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) )  
               {  
                       MessageBox( NULL, L"Local File read error", L"Warning", MB_OK);  
                       break;  
               };  

               InternetSetFilePointer( hRequest, dwFilePos, NULL, FILE_BEGIN, 0 );  
               if( !InternetWriteFile( hRequest, buf, dwRead, &dwWritten ) )  
               {  
                       MessageBox( NULL, L"Remote File write error", L"Warning", MB_OK);  
                       break;  
               };  
               dwFilePos += dwWritten;  

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


       CloseHandle( hFile );  

       if( !HttpEndRequest( hRequest, NULL, 0, 0 ) )  
       {  
               MessageBox( NULL, L"EndRequest Failed", L"warning", MB_OK);  
       }  

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

}  
반응형

'Dev > C, C++' 카테고리의 다른 글

MFC용 ADO클래스  (0) 2003.02.06
Http프로토콜을 이용한 파일 다운로드  (0) 2003.01.23
EVC에서 다이얼로그에서 메인프레임의 타이틀 바꾸기..  (2) 2002.12.30
컴파일시 꼬이는 헤더파일 방지..  (2) 2002.12.21
레지스트리, ini파일 간단히 사용하기.  (0) 2002.12.21
    'Dev/C, C++' 카테고리의 다른 글
    • MFC용 ADO클래스
    • Http프로토콜을 이용한 파일 다운로드
    • EVC에서 다이얼로그에서 메인프레임의 타이틀 바꾸기..
    • 컴파일시 꼬이는 헤더파일 방지..
    newtype
    newtype
    지극히 개인적인 지식의 창고

    티스토리툴바