2013-08-22 35 views
0

我有一個小程序,將文件上傳到服務器上。爲了測試目的,我使用了由WebAmp軟件創建的我自己的本地服務器。我的程序將通過使用PHP腳本來上傳文件,該腳本將接受程序中的文件並將它們存儲到指定位置的服務器上。現在我對此感到困惑,那就是我必須將這個PHP文件放在我的系統中,以便我的程序將與此腳本交互併成功發送文件。這裏是我的代碼HttpSendRequest 12005錯誤

int main() 
{ 

    static TCHAR frmdata[] = "-----------------------------7d82751e2bc0858\r\nContent-Disposition: form-data; name=\"uploadedfile\"; filename=\"D:\\er.txt\"\r\nContent-Type: text/plain\r\n\r\nfile contents here\r\n-----------------------------7d82751e2bc0858--\r\n"; 
    static TCHAR hdrs[] = "Content-Type: multipart/form-data; boundary=---------------------------7d82751e2bc0858"; 

    HINTERNET hSession = InternetOpen("MyBrowser",INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0); 
    if(!hSession) 
    { 
    cout<<"Error: InternetOpen"; 
    } 


    HINTERNET hConnect = InternetConnect(hSession, _T("http://localhost"),INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1); 
    if(!hConnect) 
    { 
    cout<<"Error: InternetConnect"; 
    } 

    //HINTERNET hRequest = HttpOpenRequest(hConnect, (const char*)"POST",_T("upload.php"), NULL, NULL, (const char**)"*/*\0", 0, 1); 
    LPCTSTR rgpszAcceptTypes[] = {_T("*/*"), NULL}; 
    HINTERNET hRequest = HttpOpenRequest(hConnect, (const char*)"POST", 
            _T("upload.php"), NULL, NULL, 
            rgpszAcceptTypes, 0, 1); 
    if(hRequest==NULL) 
    { 
    cout<<"Error: HttpOpenRequest"; 
    } 

    BOOL sent= HttpSendRequest(hRequest, hdrs, strlen(hdrs), frmdata, strlen(frmdata)); 
    if(!sent) 
    { 
    cout<<"Error: HttpSendRequest "<<GetLastError(); 
    } 


    //close any valid internet-handles 
    InternetCloseHandle(hSession); 
    InternetCloseHandle(hConnect); 
    InternetCloseHandle(hRequest); 
    getchar(); 
    return 0; 
} 

PHP腳本

<?php 
$uploaddir = './'; // Relative Upload Location of data file 

if (is_uploaded_file($_FILES['uploadedfile']['tmp_name'])) { 
$uploadfile = $uploaddir . basename($_FILES['uploadedfile']['name']); 
echo "File ". $_FILES['uploadedfile']['name'] ." uploaded successfully. "; 
if (move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $uploadfile)) { 
echo "File is valid, and was successfully moved. "; 
} 

else 
print_r($_FILES); 
} 

else { 
echo "Upload Failed!!!"; 
print_r($_FILES); 
} 
?> 

回答

0

的lpszServerName中的參數應該是一個主機名,而不是URL。

http://localhost改爲localhost

將這個代碼下面HTTPSendRequest打印

//Print the first 2k of the response headers and content 
char buffer[2048] = {}; 
DWORD bufferSize = sizeof(buffer); 
BOOL success = HttpQueryInfo(hRequest, HTTP_QUERY_RAW_HEADERS_CRLF, buffer, &bufferSize, NULL); 
if(!success) 
{ 
    std::cout<<"Error: HttpQueryInfo "<< GetLastError(); 
    return 0; 
} 
std::cout << buffer << std::endl; 

ZeroMemory(buffer, sizeof(buffer)); 
success = InternetReadFile(hRequest, buffer, sizeof(buffer), &bufferSize); 
if(!success) 
{ 
    std::cout << "Error: InternetReadFile " << GetLastError(); 
    return 0; 
} 
std::cout << buffer << std::endl; 
+0

當我這樣做,沒有輸出顯示的響應頭和內容..而當我按下回車鍵,在控制檯窗口會消失,你會想到什麼 – user2682006

+0

輸出?除非有錯誤,否則沒有。如果您想要檢索任何數據,您可能希望使用「InternetReadFile」來執行此操作,或者使用「HttpQueryInfo」來獲取狀態碼或響應頭。 –

+0

是一個正確的程序,或者當它沒有任何錯誤地正常工作時,它必須將該文件保存在我的電腦中的某個位置。但是當我搜索這個文件時,它只顯示原始文件而不是上傳的文件......這意味着我的程序仍然有一些錯誤 – user2682006