Created
July 6, 2013 17:45
-
-
Save dkstar88/5940643 to your computer and use it in GitHub Desktop.
I wrote these two functions for idhttp for easy usage.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi mmp112, what is your code, what exact error do you get?