-
Notifications
You must be signed in to change notification settings - Fork 63
/
DN.HttpClient.pas
141 lines (118 loc) · 3.45 KB
/
DN.HttpClient.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
unit DN.HttpClient;
interface
uses
Classes,
SysUtils,
DN.HttpClient.Intf;
type
TDNHttpClient = class(TInterfacedObject, IDNHttpClient)
private
FAuthentication: string;
FAccept: string;
FOnProgress: TProgressEvent;
FIgnoreCacheExpiration: Boolean;
function GetAuthentication: string;
procedure SetAuthentication(const Value: string);
function GetOnProgress: TProgressEvent;
procedure SetOnProgress(const Value: TProgressEvent);
function GetAccept: string;
procedure SetAccept(const Value: string);
function GetLastResponseSoure: TResponseSource;
function GetIgnoreCacheExpiration: Boolean;
procedure SetIgnoreCacheExpiration(const Value: Boolean);
protected
FLastResponseSource: TResponseSource;
procedure DoProgress(AProgress, AMax: Int64);
function GetResponseHeader(const AName: string): string; virtual;
public
function Get(const AUrl: string; AResponse: TStream): Integer; virtual; abstract;
function GetText(const AUrl: string; out AResponse: string): Integer; virtual;
function Download(const AUrl, ATargetFile: string): Integer; virtual;
procedure BeginWork; virtual;
procedure EndWork; virtual;
property Authentication: string read GetAuthentication write SetAuthentication;
property Accept: string read GetAccept write SetAccept;
property LastResponseSource: TResponseSource read GetLastResponseSoure;
property IgnoreCacheExpiration: Boolean read GetIgnoreCacheExpiration write SetIgnoreCacheExpiration;
property ResponseHeader[const AName: string]: string read GetResponseHeader;
property OnProgress: TProgressEvent read GetOnProgress write SetOnProgress;
end;
implementation
{ TDNHttpClient }
procedure TDNHttpClient.BeginWork;
begin
end;
procedure TDNHttpClient.DoProgress(AProgress, AMax: Int64);
begin
if Assigned(FOnProgress) then
FOnProgress(AProgress, AMax);
end;
function TDNHttpClient.Download(const AUrl, ATargetFile: string): Integer;
var
LFile: TFileStream;
begin
LFile := TFileStream.Create(ATargetFile, fmCreate or fmOpenReadWrite);
try
Result := Get(AUrl, LFile);
finally
LFile.Free;
end;
end;
procedure TDNHttpClient.EndWork;
begin
end;
function TDNHttpClient.GetAccept: string;
begin
Result := FAccept;
end;
function TDNHttpClient.GetAuthentication: string;
begin
Result := FAuthentication;
end;
function TDNHttpClient.GetIgnoreCacheExpiration: Boolean;
begin
Result := FIgnoreCacheExpiration;
end;
function TDNHttpClient.GetLastResponseSoure: TResponseSource;
begin
Result := FLastResponseSource;
end;
function TDNHttpClient.GetOnProgress: TProgressEvent;
begin
Result := FOnProgress;
end;
function TDNHttpClient.GetResponseHeader(const AName: string): string;
begin
Result := '';
end;
function TDNHttpClient.GetText(const AUrl: string;
out AResponse: string): Integer;
var
LText: TStringStream;
begin
LText := TStringStream.Create('', TEncoding.UTF8);
try
Result := Get(AUrl, LText);
if Result = HTTPErrorOk then
AResponse := LText.DataString;
finally
LText.Free;
end;
end;
procedure TDNHttpClient.SetAccept(const Value: string);
begin
FAccept := Value;
end;
procedure TDNHttpClient.SetAuthentication(const Value: string);
begin
FAuthentication := Value;
end;
procedure TDNHttpClient.SetIgnoreCacheExpiration(const Value: Boolean);
begin
FIgnoreCacheExpiration := Value;
end;
procedure TDNHttpClient.SetOnProgress(const Value: TProgressEvent);
begin
FOnProgress := Value;
end;
end.