msdn upload image with winhttp c++

一曲冷凌霜 提交于 2020-01-14 03:21:08

问题


I have been trying this the whole day but no luck i want to upload an image file to a php file which i have created but when ever i try to do that winhttpsendrequest throws 183 error that means cannot send file that is already sent please can someone point out where i am wrong

c++ code:

int _tmain(int argc, _TCHAR* argv[]) {

HINTERNET hSession = NULL, 
    hConnect = NULL,
    hRequest = NULL;
BOOL bResults = FALSE;

FILE *pFile;
long lSize;
char *buffer;
size_t result;

pFile = fopen("blog.jpg", "rb");

fseek(pFile, 0, SEEK_END);
lSize = ftell(pFile);
rewind(pFile);

buffer = (char *) malloc(sizeof(char) * lSize);

result = fread(buffer, 1, lSize, pFile);

fclose(pFile);

hSession = WinHttpOpen( L"WinHTTP Example/1.0",  
    WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
    WINHTTP_NO_PROXY_NAME, 
    WINHTTP_NO_PROXY_BYPASS, 0);

if (hSession)
    hConnect = WinHttpConnect(hSession, L"localhost",
    INTERNET_DEFAULT_HTTP_PORT, 0);

if (hConnect)
    hRequest = WinHttpOpenRequest(hConnect, L"POST", L"locker/upload.php",
    NULL, WINHTTP_NO_REFERER, 
    WINHTTP_DEFAULT_ACCEPT_TYPES, 
    WINHTTP_FLAG_REFRESH);

static WCHAR frmdata[2048] = L"Connection: keep-alive\r\nContent-Type: multipart/form-data; -----------------------------7d82751e2bc0858\r\nContent-Disposition: form-data; name=\"file\"; filename=\"blog.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n";

bResults = WinHttpSendRequest(hRequest,
    frmdata, wcslen(frmdata), buffer, 
    lSize, wcslen(frmdata)+lSize, 0);

if (bResults) {
    /*
    DWORD dwBytesWritten = 0;
    bResults = WinHttpWriteData(hRequest, buffer, 
        lSize, 
        &dwBytesWritten);
    if (bResults) {
        printf_s("Data: %d", dwBytesWritten);
    }
    */

} else {
    printf_s("SendReq: %d", GetLastError());
}


free(buffer);

if (hRequest) { WinHttpCloseHandle(hRequest); }
if (hConnect) { WinHttpCloseHandle(hConnect); }
if (hSession) { WinHttpCloseHandle(hSession); }

getchar();

return 0;
    }

php code:

if (isset($_FILES["file"])) {
$target_path = "uploads/";

$target_path = $target_path . basename( $_FILES['file']['name']); 

if(move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) {
echo "The file ".  basename( $_FILES['file']['name']). " has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
}

回答1:


Keshav Nair.

try to this code:

CHAR postData[1024];
CHAR postData2[1024];
ZeroMemory(&postData,1024);
ZeroMemory(&postData2,1024);
WinHttpAddRequestHeaders(hRequest,L"Content-Type: multipart/form-data; boundary=----OiRBxC0fjdSEpqhd",-1L,WINHTTP_ADDREQ_FLAG_ADD);
wsprintfA(postData,"%s","----OiRBxC0fjdSEpqhd\r\nContent-Disposition: form-data; name=\"file\"; filename=\"blog.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n");
wsprintfA(postData2,"%s","\r\n----OiRBxC0fjdSEpqhd\r\nContent-Disposition: form-data; name=\"submit\"\r\n\r\nSubmit\r\n----OiRBxC0fjdSEpqhd--\r\n");
bResults = WinHttpSendRequest( hRequest,
                                   WINHTTP_NO_ADDITIONAL_HEADERS,
                                   0, WINHTTP_NO_REQUEST_DATA, 0, 
                                   lstrlenA(postData)+lstrlenA(postData2)+lSize, NULL);
if (bResults)
{
    bResults=WinHttpWriteData(hRequest,LPCVOID)postData,lstrlenA(postData),NULL);
    bResults = WinHttpWriteData(hRequest, buffer, lSize, &dwBytesWritten);
    bResults=WinHttpWriteData(hRequest,(LPCVOID)postData2,lstrlenA(postData2),NULL);
}

WinHttpWriteData: POST data - postData, postData2 must be 8 bit encoding.



来源:https://stackoverflow.com/questions/18193363/msdn-upload-image-with-winhttp-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!