Skip to content

Instantly share code, notes, and snippets.

@dkstar88
Created July 6, 2013 17:45
Show Gist options
  • Save dkstar88/5940643 to your computer and use it in GitHub Desktop.
Save dkstar88/5940643 to your computer and use it in GitHub Desktop.
I wrote these two functions for idhttp for easy usage.
unit httpUtil;
interface
uses Classes, SysUtils, IdHTTP;
type
THTTP_STATUS = Cardinal;
function DownloadToStream(AURL: String; AStream: TStream): THTTP_STATUS;
function HTTPGet(AURL: String): string; overload;
function HTTPGet(AURL: String; const Encoding: TEncoding): string; overload;
implementation
function HTTPGet(AURL: String): string; overload;
begin
Result := HTTPGet(AURL, TEncoding.Default);
end;
function HTTPGet(AURL: String; const Encoding: TEncoding): string; overload;
var
text: TMemoryStream;
strings: TStrings;
begin
with TIdHTTP.Create(nil) do
begin
try
text := TMemoryStream.Create;
try
try
Get(AURL, text);
except
end;
if ResponseCode=200 then
begin
strings := TStringList.Create;
text.Seek(0, 0);
strings.LoadFromStream(text, Encoding);
Result := strings.Text;
strings.Free;
end
else
Result := '';
finally
text.Free;
end;
finally
Free;
end;
end;
end;
function DownloadToStream(AURL: String; AStream: TStream): THTTP_STATUS;
begin
with TIdHTTP.Create(nil) do
begin
try
Get(AURL, AStream);
Result := ResponseCode;
finally
Free;
end;
end;
end;
end.
@dkstar88
Copy link
Author

Hi mmp112, what is your code, what exact error do you get?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment