4

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?

2 Answers 2

7

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 a PWideChar, not a PAnsiChar like 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:

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

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.

Sign up to request clarification or add additional context in comments.

Comments

2

Are you using Delphi 2009 or above? If so then your string would be in unicode. Try using an ANSIstring variable for your TESTR string

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.