Download Using HTTPSendRequest

I need to send files to a PHP script using delphi. I finally decided to use Wininet features because I need to go through the NTLM Authentication proxy.

When I submit the file, I have empty characters (00) between each character 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 TES 

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 on what's going on?

+4
source share
2 answers

You do not take into account that Delphi strings have switched to UTF-16 encoded Unicode since Delphi 2009. When you pass PChar('TESTR') , you are actually passing PWideChar , not PAnsiChar , as you would expect. The character length of the string is 5 characters, but the byte length is 10 bytes. HTTPSendRequest() works with bytes, not characters. So you really send 5 bytes where every second byte is null, because that is how ASCII characters are encoded in UTF-16.

Instead, change the last line:

 var S: AnsiString; ... S := 'TESTR'; HTTPSendRequest(pRequest, nil, 0, PAnsiChar(S), Length(S)); 

You do not need to do this with other functions that you call, because they accept Unicode characters as input, so there is no mismatch.

+7
source

Are you using Delphi 2009 or higher? If so, your string will be in unicode. Try using the ANSIstring variable for the string TESTR

+2
source

Source: https://habr.com/ru/post/1432427/


All Articles