The reason TIdHTTP
is failing is because of this key piece of information that wget is reporting:
No headers, assuming HTTP/0.9
In an HTTP 0.9 response, the HTTP status line and headers are not present at all, only the raw file data by itself, terminated by a disconnect. wget supports that, but TIdHTTP
does not (even though the official HTTP 1.0 and HTTP 1.1 specs require support for recognizing HTTP 0.9 responses) . TIdHTTP
supports only HTTP 1.0 and 1.1 formatted messages, which require the use of an HTTP status line and headers. For whatever reason, this particular server is choosing to send an HTTP 0.9 response for Indy's UserAgent
, but is choosing to send an HTTP 1.0 response for Internet Explorer UserAgents instead. Odd.
The short-term solution is to do what @TLama said. Setting the TIdHTTP.Request.UserAgent
property to mimic Internet Explorer allows TIdHTTP.Get()
to work properly:
procedure TForm1.Button1Click(Sender: TObject);
var
icon: TMemoryStream;
begin
icon := TMemoryStream.Create;
try
try
IdHTTP1.Request.UserAgent := 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1';
IdHTTP1.Get('http://perforce.eigenbase.org:8080/favicon.ico', icon);
except
on E: Exception do
begin
{$IFDEF DEBUG}ShowMessage('get error:'+E.Message){$ENDIF};
end;
end;
ShowMessage(IntToStr(icon.Size));
finally
icon.Free;
end;
end;
The long-term solution would be to update TIdHTTP
to support HTTP 0.9 responses, even though they are very rare to encounter nowadays. I have opened tickets in Indy's issue trackers for that.