msdn使用winhttp c++上传图像

msdn upload image with winhttp c++

本文关键字:图像 c++ 使用 winhttp msdn      更新时间:2023-10-16

我已经尝试了一整天,但没有运气,我想上传一个图像文件到我创建的php文件,但当我尝试这样做时,winhttpsendrequest抛出183错误,这意味着无法发送已经发送的文件,请有人指出我的错在哪里

c++代码:

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-alivernContent-Type: multipart/form-data; -----------------------------7d82751e2bc0858rnContent-Disposition: form-data; name="file"; filename="blog.jpg"rnContent-Type: image/jpegrnrn";
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代码:

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!";
}
}

Keshav Nair。

试试这个代码:

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","----OiRBxC0fjdSEpqhdrnContent-Disposition: form-data; name="file"; filename="blog.jpg"rnContent-Type: image/jpegrnrn");
wsprintfA(postData2,"%s","rn----OiRBxC0fjdSEpqhdrnContent-Disposition: form-data; name="submit"rnrnSubmitrn----OiRBxC0fjdSEpqhd--rn");
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数据-postData,postData2必须是8位编码。