2

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!";
}
}
Keshav Nair
  • 423
  • 1
  • 14
  • 24
  • I'm having a similar problem... the send fails sporadically with error 183 and only recreating the connection helps, until it sporadically fails again. – ActiveTrayPrntrTagDataStrDrvr Nov 15 '13 at 14:25
  • use winsock and remember to check the headers and user \r\n properly and you have to give extra gap when setting border – Keshav Nair Nov 16 '13 at 18:03
  • It's all checked. It seems there is a sporadic bug that occurs, when having more than one connection to the same host. The HTTP tracing reports ERROR_ALREADY_EXISTS on WinHttpSendRequest async completion. Doesn't seem happen on connections to different hosts. – ActiveTrayPrntrTagDataStrDrvr Nov 18 '13 at 14:58
  • @ActiveTrayPrntrTagDataStrDrvr Please look at the code i have marked as correct and please look carefully at the headers sent and please rename the files sent as the file name that is being uploaded might exist – Keshav Nair Nov 25 '13 at 06:54

1 Answers1

2

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.

DJK
  • 36
  • 3
  • Yes, i did realized later on that after boundary and the file data to be send should have extra spaces but this could help ActiveTrayPrntrTagDataStrDrvr and it correct (i have not tested it but i can read :p) – Keshav Nair Nov 25 '13 at 06:51