I’m using WinInet to connect and retrieve information from one of our server. I’m using the following:
indexdata: array of byte[1..5000] of byte;
infoBuffer: array [0..5000] of char;
BufferSize: DWORD;
reserved: DWORD;
text: string;
BufferSize := Length(infoBuffer);
res := HttpQueryInfo(hHttpRequest, HTTP_QUERY_RAW_HEADERS_CRLF, @infoBuffer, BufferSize, Reserved);
Reserved := 0;
InternetReadFile(hHttpRequest, @indexdata, sizeof(indexdata), Reserved);
SetLength(text, Reserved);
CopyMemory(@text[1], @indexdata[1], Reserved);
The two array of bytes were enough up until now. Things changed. The server can return now information that can be bigger or smaller than 5000; worst yet, in InternetReadFile can return a variable size in the infoBuffer.
So i tried declaring the indexdata and infobuffer as array of byte and then using SetLength to set its length, but 2 things happened.
1) I still don’t know the size of indexdata that the server will return so I cannot properly set it to, say, 100000.
2) I cannot use (as it is now) CopyMemory passing Low(indexdata) to copy indexdata to a simple string variable so I can use the data.
How do I handle this in Delphi? I can do it in C but I can’t seem to be able to do it properly in Delphi.
Code is appreciated
thanks!
You do know that
charis a Unicode character since Delphi 2009, but an ANSI character prior to Delphi 2009. In the same way, in Delphi 2009 astringis aUnicodeString, not anAnsiString.So, when you write
SetLength(text, Reserved)you do set the number of characters intexttoReserved. But the number of bytes will be2*Reserved.In other words, in Delphi 2009+, one
charis not onebyte, but two bytes.You can get back the old behaviour by replacing all
charwithAnsiCharand allstringwithAnsiString.Update
Since you did not post your entire code, I cannot really say what the problem is. Nevertheless, you might find it interesting to read my example usage of InternetReadFile in Delphi. See my answer to this question. It is a fully-working example of how to read a text file from the Internet using Delphi and
InternetReadFile.For your convinience, I paste my code below as well:
To read data from the Internet, use
InternetReadFilefunction. I use the following code to read a small (one-line) text file from the Internet:Sample usage: