I have to send files to a php script with delphi.
I finaly chose to use Wininet functions because I have to pass through a NTLM Authentication proxy.
When i send the file, I have empty chars (00) between each chars of my content request :
POST /upload.php HTTP/1.1
User-Agent: MYUSERAGENT
Host: 127.0.0.1
Cookie: ELSSESSID=k6ood2su2fbgrh805vs74fvmk5
Pragma: no-cache
Content-Length: 5
T.E.S
Here is my Delphi code :
pSession := InternetOpen('MYUSERAGENT', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
pConnection := InternetConnect(pSession, PChar('127.0.0.1'), INTERNET_DEFAULT_HTTP_PORT, nil, nil, INTERNET_SERVICE_HTTP, 0, 0);
pRequest := HTTPOpenRequest(pConnection, PChar('POST'), PChar('/upload.php'), 'HTTP/1.0', nil, nil, INTERNET_SERVICE_HTTP, 0);
HTTPSendRequest(pRequest, nil, 0, Pchar('TESTR'), Length('TESTR'));
Any ideas of what’s happening?
You are not taking into account that Delphi strings switched to UTF-16 encoded Unicode starting in Delphi 2009. When you pass
PChar('TESTR'), you are actually passing aPWideChar, not aPAnsiCharlike you are expecting. The character length of the string is 5 characters, but the byte length is 10 bytes.HTTPSendRequest()operates on bytes, not characters. So you really are sending 5 bytes, where each second byte is a null, because that is how ASCII characters are encoded in UTF-16.Change the last line to this instead:
You don’t need to do that with the other functions you are calling because they do take Unicode characters as input, so there is no mismatch.