If you want to retrieve content from a web server that supports gzip compression, you have a few ways to get data from this web server, of which, here are a couple:
- Use TIdHttp and assign IdHttp1.Request.AcceptEncoding to blank. If you do this, a Get call to the web server will return the full text and you will not see the benefits of the gzip compression that can occur on the web server. Not exactly what we want.
- Use TIdHttp and assign IdHttp1.Request.AcceptEncoding to 'gzip'. If you do this, the web server will return gzipped content when doing an IdHttp1.Get(URL) call.
If you use #2 above, you will need to manually decode the returned data. I found the LVK components from the LVK web site to be most helpful in this task. I couldn't easily get the standard zlib or Indy10 zlib implementation to work with gzip encoding, so I ended up using LVK. Using the code below, you get the data back in compressed format and decode it. As an aside, it looks like LVK also supports the deflate option in addition to the gzip option, and has TONS of components and utility code.
// add lvkZLibUtils to the uses clause
procedure TForm3.Button2Click(Sender: TObject);
var
inStream, outStream: TMemoryStream;
begin
inStream := TMemoryStream.Create;
try
IdHttp1.Get('http://www.pgatour.com', inStream);
outStream := TMemoryStream.Create;
try
gZipDecompress(inStream, outStream);
outStream.Position := 0;
Memo1.Lines.LoadFromStream(outStream);
finally
outStream.Free;
end;
finally
inStream.Free;
end;
end;