-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathuTCLQHttpThreadControl.pas
295 lines (222 loc) · 6.08 KB
/
uTCLQHttpThreadControl.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
unit uTCLQHttpThreadControl;
//在线程中访问 http 有结果后在主线程响应的类
interface
uses
Classes,IdBaseComponent, IdComponent, IdTCPConnection, forms, activex,
SysUtils,IdTCPClient, IdHTTP, Dialogs, OleCtrls, Variants;
//,uTWebBrowserHttp1;
type
TCLQHttpThreadEvent = procedure(const out1:string;succeed1:boolean) of object;
TCLQHttpThreadEvent2 = procedure(const out1:string;succeed1:boolean); //没有控件的
type
TCLQHttpThread = class(TThread)
private
{ Private declarations }
protected
out1:string;
ok1:boolean;
IdHTTP1: TIdHTTP;
//应当由外部生成//Webhttp1:TWebBrowserHttp1;
procedure do_ok;
procedure Execute; override;
public
post_url1:string; //接受http请求的网址
post_data1:tstrings; //IdHTTP所需要的post数据
on_ok1:TCLQHttpThreadEvent; //成功后的事件
on_ok2:TCLQHttpThreadEvent2; //成功后的事件
is_get1:boolean;//使用 get 方法
//Webhttp1:TWebBrowserHttp1;
//WebBrowser1:TWebBrowser;
constructor Create(CreateSuspended: Boolean);
destructor Destroy; override;
end;
type
TCLQHttpThreadControl = class (TComponent)
private
procedure set_post_data1(const Value: TStrings);
procedure set_pre_data1(const Value: TStrings);
{ Private declarations }
protected
out1:string;
ok1:boolean;
IdHTTP1: TIdHTTP;
post_url1:string; //接受http请求的网址
post_data1:tstrings; //IdHTTP所需要的post数据
pre_url1:string; //接受http请求的网址
is_get1:boolean; //是否使用 get 方法
pre_data1:tstrings; //IdHTTP所需要的post数据
fon_ok:TCLQHttpThreadEvent; //成功后的事件
//WebBrowser1:TWebBrowser;
http1:TCLQHttpThread;
public
//事件
on_ok2: TCLQHttpThreadEvent2;
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure execute;
published
property post_data: TStrings read post_data1 write set_post_data1;
property post_url: string read post_url1 write post_url1;
property pre_data: TStrings read pre_data1 write set_pre_data1;
property pre_url: string read pre_url1 write pre_url1;
property is_get: boolean read is_get1 write is_get1;//是否使用 get 方法
//事件
property on_ok: TCLQHttpThreadEvent read fon_ok write fon_ok;
//事件
//property on_ok2: TCLQHttpThreadEvent2;
end;
procedure Register;
implementation
{ Important: Methods and properties of objects in visual components can only be
used in a method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure TPostHttpThread1.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }
{ TPostHttpThread1 }
procedure Register;
begin
RegisterComponents('clq', [TCLQHttpThreadControl]);
end;
//--------------------------------------------------
//bcb6 兼容
//function TIdCustomHTTP.Post(AURL: string; const ASource: TStrings): string;
function http_post(http:TIdHTTP; AURL: string; const ASource: TStrings): string;
var
LResponse: TStringStream;
begin
LResponse := TStringStream.Create('');
try
http.Post(AURL, ASource, LResponse);
finally
result := LResponse.DataString;
LResponse.Free;
end;
end;
//--------------------------------------------------
constructor TCLQHttpThreadControl.Create(AOwner: TComponent);
begin
inherited;
post_data1:=tstringlist.Create;
pre_data1:=tstringlist.Create;
is_get1:=false;
//WebBrowser1:=TWebBrowser.Create(application.MainForm);
//WebBrowser1.Visible:=false;
end;
destructor TCLQHttpThreadControl.Destroy;
begin
//http1.free; //?需要这个调用吗?
post_data1.Free;
pre_data1.Free;
inherited;
end;
procedure TCLQHttpThreadControl.execute;
begin
http1:=TCLQHttpThread.create(true);
http1.post_data1:=self.post_data;
http1.post_url1:=self.post_url;
http1.on_ok1:=self.on_ok;
http1.on_ok2:=self.on_ok2;
//http1.WebBrowser1:=self.WebBrowser1;
http1.is_get1:=is_get1;
http1.Resume;//它会自动free
end;
procedure TCLQHttpThread.Execute;
var
b_new1:boolean;//是否已经进行过取得新的URL的动作[不论是否成功]
s1:string;
strData: string;
PostData: OleVariant;
Headers: OleVariant;
i:integer;
procedure do_exit1;
begin
//post_data1.Free;
if IdHTTP1<>nil then
begin
IdHTTP1.Free;
end;
end;
begin
//CoInitialize(nil);
s1:=self.post_data1.Text;
b_new1:=false;
IdHTTP1:= TIdHTTP.Create(nil);
ok1:=true;
out1:='';
//如果没有找到则终止
if trim(self.post_url1)='' then
begin
ok1:=false;
Synchronize(do_ok);
do_exit1;
exit;
end;
//开始访问真正的地址
try
if is_get1
then out1:=IdHTTP1.Get(self.post_url1)
else out1:=http_post(IdHTTP1,self.post_url1,self.post_data1);
except
post_url1:='';//要修改为空,否则GetNewPostUrl不会取得最新url
ok1:=false;
out1:='';
end;
//如果这个URL访问不正常,并且没有取得过新的URL,那么重新取URL,再发送一次
if (b_new1=false)and(ok1=false) then
begin
b_new1:=true;
//如果没有找到则终止
if trim(self.post_url1)='' then
begin
ok1:=false;
Synchronize(do_ok);
do_exit1;
exit;
end;
//开始访问真正的地址
try
self.post_data1.Text:=s1;
// if use_ie1
// then out1:=webHTTP1.Post1(self.post_url1,self.post_data1)
// else
out1:=http_post(IdHTTP1,self.post_url1,self.post_data1);
except
ok1:=false;
out1:='';
end;
end;
Synchronize(do_ok);
do_exit1;
end;
procedure TCLQHttpThread.do_ok;
begin
if assigned(self.on_ok1) then self.on_ok1(out1,ok1);
if assigned(self.on_ok2) then self.on_ok2(out1,ok1);
end;
procedure TCLQHttpThreadControl.set_post_data1(const Value: TStrings);
begin
post_data1 := Value;
end;
procedure TCLQHttpThreadControl.set_pre_data1(const Value: TStrings);
begin
pre_data1 := Value;
end;
constructor TCLQHttpThread.Create(CreateSuspended: Boolean);
begin
inherited;
is_get1:=false;
end;
destructor TCLQHttpThread.Destroy;
begin
inherited;
end;
initialization
{ Initialization section goes here }
//CoInitialize(nil);
finalization
{ Finalization section goes here }
//CoUninitialize();
end.