-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtiswinhttp.pas
More file actions
539 lines (482 loc) · 20.1 KB
/
tiswinhttp.pas
File metadata and controls
539 lines (482 loc) · 20.1 KB
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
unit tiswinhttp;
{ -----------------------------------------------------------------------
# This file is part of WAPT
# Copyright (C) 2013 Tranquil IT Systems http://www.tranquil.it
# WAPT aims to help Windows systems administrators to deploy
# setup and update applications on users PC.
#
# Part of this file is based on JEDI JCL library
#
# WAPT is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# WAPT is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with WAPT. If not, see <http://www.gnu.org/licenses/>.
#
# -----------------------------------------------------------------------
}
{functions to get files and string using Winet windows http facilities}
{$mode objfpc}{$H+}
{$ifndef windows}
interface
implementation
end.
{$else}
interface
uses
Classes, SysUtils, wininet;
type
TProgressCallback=function(Receiver:TObject;current,total:Integer):Boolean of object;
TLoginCallback = function(realm:String;var user,password:String):Boolean of object;
EHTTPException=Class(Exception)
HTTPStatus: Integer;
constructor Create(const msg: string;AHTTPStatus:Integer);
end;
function wget(const fileURL, DestFileName: Utf8String; CBReceiver:TObject=Nil;progressCallback:TProgressCallback=Nil;enableProxy:Boolean=False;forceReload:Boolean=True;const UserAgent: ansistring=''): boolean;
function httpGetHeaders(url: ansistring; enableProxy:Boolean= False;
ConnectTimeout:integer=4000;SendTimeOut:integer=60000;ReceiveTimeOut:integer=60000;user:AnsiString='';password:AnsiString='';VerifyCert:Boolean=False):RawByteString;
function httpGetString(url: ansistring; enableProxy:Boolean= False;
ConnectTimeout:integer=4000;SendTimeOut:integer=60000;ReceiveTimeOut:integer=60000;user:AnsiString='';password:AnsiString='';VerifyCert:Boolean=False):RawByteString;
function httpPostData(const UserAgent: ansistring; const url: Ansistring; const Data: RawByteString; enableProxy:Boolean= False;
ConnectTimeout:integer=4000;SendTimeOut:integer=60000;ReceiveTimeOut:integer=60000;user:AnsiString='';password:AnsiString='';VerifyCert:Boolean=False):RawByteString;
function ignoreCerticateErrors(oRequestHandle:HINTERNET; var aErrorMsg: ansistring): Boolean;
function GetWinInetError(ErrorCode:Cardinal): ansistring;
implementation
uses URIParser,LazFileUtils,JwaWinType,JwaWinBase;
Function GetHttpAgent:ansistring;
var
sAppName: AnsiString;
begin
sAppName := ExtractFileName(ParamStr(0));
Result := sAppName+' '+GetFileDescription(ParamStr(0));
end;
Function wget(const fileURL, DestFileName: Utf8String; CBReceiver:TObject=Nil;progressCallback:TProgressCallback=Nil;enableProxy:Boolean=False;forceReload:Boolean=True;const UserAgent: ansistring=''): boolean;
const
BufferSize = 1024*512;
var
hInet, hURL: HInternet;
Buffer: array[1..BufferSize] of Byte;
BufferLen: DWORD;
f: File;
sAppName: Utf8string;
Size: Integer;
total:DWORD;
totalLen:DWORD;
dwindex: cardinal;
dwcode : array[1..20] of Ansichar;
dwCodeLen : DWORD;
res : PAnsiChar;
begin
result := false;
if UserAgent = '' then
sAppName := ExtractFileName(ParamStr(0))
else
sAppName:= UserAgent;
if enableProxy then
hInet := InternetOpenW(PWideChar(UTF8Decode(sAppName)), INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0)
else
hInet := InternetOpenW(PWideChar(UTF8Decode(sAppName)), INTERNET_OPEN_TYPE_DIRECT, nil, nil, 0) ;
try
if not forceReload then
hURL := InternetOpenUrlW(hInet, PWideChar(UTF8Decode(fileURL)), nil, 0, INTERNET_FLAG_DONT_CACHE+INTERNET_FLAG_KEEP_CONNECTION+INTERNET_FLAG_NO_AUTH , 0)
else
hURL := InternetOpenUrlW(hInet, PWideChar(UTF8Decode(fileURL)), nil, 0, INTERNET_FLAG_DONT_CACHE+INTERNET_FLAG_RELOAD+INTERNET_FLAG_PRAGMA_NOCACHE+INTERNET_FLAG_KEEP_CONNECTION+INTERNET_FLAG_NO_AUTH, 0) ;
if assigned(hURL) then
try
dwIndex := 0;
dwCodeLen := SizeOf(dwcode);
totalLen := SizeOf(totalLen);
HttpQueryInfo(hURL, HTTP_QUERY_STATUS_CODE, @dwcode, dwcodeLen, dwIndex);
HttpQueryInfo(hURL, HTTP_QUERY_CONTENT_LENGTH or HTTP_QUERY_FLAG_NUMBER, @total,totalLen, dwIndex);
res := pansichar(@dwcode);
if (res ='200') or (res ='302') then
begin
Size:=0;
try
AssignFile(f, UTF8Decode(DestFileName)) ;
try
Rewrite(f,1) ;
repeat
BufferLen:= 0;
if InternetReadFile(hURL, @Buffer, SizeOf(Buffer), BufferLen) then
begin
inc(Size,BufferLen);
BlockWrite(f, Buffer, BufferLen);
if Assigned(progressCallback) then
if not progressCallback(CBReceiver,size,total) then
begin
BufferLen:=0;
raise EHTTPException.Create('Download stopped by user',0);
end;
end;
until BufferLen = 0;
finally
CloseFile(f);
end;
except
If FileExists(DestFileName) then
LazFileUtils.DeleteFileUTF8(DestFileName);
raise;
end;
result := (Size>0);
end
else
raise EHTTPException.Create('Unable to download: "'+fileURL+'", HTTP Status:'+res, StrToInt(res));
finally
InternetCloseHandle(hURL)
end
else
raise EHTTPException.Create('Unable to download: "'+fileURL+'", wininet error:'+IntToStr(GetLastError()), 400);
finally
InternetCloseHandle(hInet)
end
end;
function httpGetHeaders(url: ansistring; enableProxy:Boolean= False;
ConnectTimeout:integer=4000;SendTimeOut:integer=60000;ReceiveTimeOut:integer=60000;user:AnsiString='';password:AnsiString='';VerifyCert:Boolean=False):RawByteString;
var
hInet,hUrl,hConnect: HINTERNET;
OutBufferLen:Cardinal;
OutBuffer:PAnsiChar;
flags : DWORD;
doc,error: AnsiString;
uri :TURI;
puser,ppassword:PAnsiChar;
begin
result := '';
hInet:=Nil;
hConnect := Nil;
hUrl:=Nil;
if enableProxy then
hInet := InternetOpen('wapt',INTERNET_OPEN_TYPE_PRECONFIG,nil,nil,0)
else
hInet := InternetOpen('wapt',INTERNET_OPEN_TYPE_DIRECT,nil,nil,0);
try
InternetSetOption(hInet,INTERNET_OPTION_CONNECT_TIMEOUT,@ConnectTimeout,sizeof(integer));
InternetSetOption(hInet,INTERNET_OPTION_SEND_TIMEOUT,@SendTimeOut,sizeof(integer));
InternetSetOption(hInet,INTERNET_OPTION_RECEIVE_TIMEOUT,@ReceiveTimeOut,sizeof(integer));
uri := ParseURI(url,'http',80);
if (uri.Protocol = 'https') and (uri.port=80) then
uri.port := 443;
if uri.Username<>'' then
puser:= PAnsiChar(uri.Username)
else
puser := Nil;
if uri.Password<>'' then
ppassword:=PAnsiChar(uri.Password)
else
ppassword:=Nil;
if user<>'' then
puser:=PAnsiChar(user);
if password<>'' then
ppassword:=PAnsiChar(password);
hConnect := InternetConnect(hInet, PAnsiChar(uri.Host), uri.port, puser, ppassword, INTERNET_SERVICE_HTTP, 0, 0);
if not Assigned(hConnect) then
Raise EHTTPException.Create('Unable to connect to '+url+' code : '+IntToStr(GetLastError)+' ('+GetWinInetError(GetlastError)+')',0);
flags := INTERNET_FLAG_DONT_CACHE or INTERNET_FLAG_NO_CACHE_WRITE or INTERNET_FLAG_PRAGMA_NOCACHE or INTERNET_FLAG_RELOAD or INTERNET_FLAG_KEEP_CONNECTION;
if uri.Protocol='https' then
flags := flags or INTERNET_FLAG_SECURE;
doc := uri.Path+uri.document;
if uri.params<>'' then
doc:= doc+'?'+uri.Params;
hUrl := HttpOpenRequest(hConnect, 'HEAD', PAnsiChar(doc), HTTP_VERSION, nil, nil,flags , 0);
if not Assigned(hUrl) then
Raise EHTTPException.Create('Unable to get doc '+url+' code : '+IntToStr(GetLastError)+' ('+GetWinInetError(GetlastError)+')',0);
if not HttpSendRequest(hUrl, nil, 0, nil, 0) then
begin
ErrorCode:=GetLastError;
if (ErrorCode = ERROR_INTERNET_INVALID_CA) then
begin
if not VerifyCert then ignoreCerticateErrors(hUrl, error);
if not HttpSendRequest(hUrl, nil, 0, nil, 0) then
Raise EHTTPException.Create('Unable to send request to '+url+' code : '+IntToStr(GetLastError)+' ('+GetWinInetError(GetlastError)+')',0);
end;
end;
OutBuffer:=Nil;
OutBufferLen:=0;
if Assigned(hUrl) then
try
HttpQueryInfo(hUrl, HTTP_QUERY_RAW_HEADERS_CRLF, OutBuffer,@OutBufferLen, Nil);
if OutBufferLen>0 then
begin
OutBuffer := Getmem(OutBufferLen);
if HttpQueryInfo(hUrl, HTTP_QUERY_RAW_HEADERS_CRLF, OutBuffer,@OutBufferLen, Nil) then
begin
Result := PAnsiChar(OutBuffer);
//SetLength(Result,OutBufferLen);
//Move(OutBuffer,Result[1],OutBufferLen);
end;
end;
finally
if Assigned(OutBuffer) then
Freemem(OutBuffer);
InternetCloseHandle(hUrl);
if Assigned(hUrl) then
InternetCloseHandle(hUrl);
end
finally
if Assigned(hConnect) then
InternetCloseHandle(hConnect);
if Assigned(hInet) then
InternetCloseHandle(hInet);
end;
end;
// récupère une chaine de caractères en http en utilisant l'API windows
function httpGetString(url: ansistring; enableProxy:Boolean= False;
ConnectTimeout:integer=4000;SendTimeOut:integer=60000;ReceiveTimeOut:integer=60000;user:AnsiString='';password:AnsiString='';VerifyCert:Boolean=False):RawByteString;
var
hInet,hUrl,hConnect: HINTERNET;
buffer: array[1..1024] of byte;
flags,bytesRead,dwError,port : DWORD;
pos:integer;
dwindex,dwcodelen,dwread,dwNumber: cardinal;
dwcode : array[1..20] of ansichar;
res : PAnsiChar;
doc,error: AnsiString;
uri :TURI;
puser,ppassword:PAnsiChar;
begin
result := '';
hInet:=Nil;
hConnect := Nil;
hUrl:=Nil;
if enableProxy then
hInet := InternetOpen('wapt',INTERNET_OPEN_TYPE_PRECONFIG,nil,nil,0)
else
hInet := InternetOpen('wapt',INTERNET_OPEN_TYPE_DIRECT,nil,nil,0);
try
InternetSetOption(hInet,INTERNET_OPTION_CONNECT_TIMEOUT,@ConnectTimeout,sizeof(integer));
InternetSetOption(hInet,INTERNET_OPTION_SEND_TIMEOUT,@SendTimeOut,sizeof(integer));
InternetSetOption(hInet,INTERNET_OPTION_RECEIVE_TIMEOUT,@ReceiveTimeOut,sizeof(integer));
uri := ParseURI(url,'http',80);
if (uri.Protocol = 'https') and (uri.port=80) then
uri.port := 443;
if uri.Username<>'' then
puser:= PAnsiChar(uri.Username)
else
puser := Nil;
if uri.Password<>'' then
ppassword:=PAnsiChar(uri.Password)
else
ppassword:=Nil;
if user<>'' then
puser:=PAnsiChar(user);
if password<>'' then
ppassword:=PAnsiChar(password);
hConnect := InternetConnect(hInet, PAnsiChar(uri.Host), uri.port, puser, ppassword, INTERNET_SERVICE_HTTP, 0, 0);
if not Assigned(hConnect) then
Raise EHTTPException.Create('Unable to connect to '+url+' code : '+IntToStr(GetLastError)+' ('+GetWinInetError(GetlastError)+')',0);
flags := INTERNET_FLAG_DONT_CACHE or INTERNET_FLAG_NO_CACHE_WRITE or INTERNET_FLAG_PRAGMA_NOCACHE or INTERNET_FLAG_RELOAD or INTERNET_FLAG_KEEP_CONNECTION;
if uri.Protocol='https' then
flags := flags or INTERNET_FLAG_SECURE;
doc := uri.Path+uri.document;
if uri.params<>'' then
doc:= doc+'?'+uri.Params;
hUrl := HttpOpenRequest(hConnect, 'GET', PAnsiChar(doc), HTTP_VERSION, nil, nil,flags , 0);
if not Assigned(hUrl) then
Raise EHTTPException.Create('Unable to get doc '+url+' code : '+IntToStr(GetLastError)+' ('+GetWinInetError(GetlastError)+')',0);
if not HttpSendRequest(hUrl, nil, 0, nil, 0) then
begin
ErrorCode:=GetLastError;
if (ErrorCode = ERROR_INTERNET_INVALID_CA) then
begin
if not VerifyCert then ignoreCerticateErrors(hUrl, error);
if not HttpSendRequest(hUrl, nil, 0, nil, 0) then
Raise EHTTPException.Create('Unable to send request to '+url+' code : '+IntToStr(GetLastError)+' ('+GetWinInetError(GetlastError)+')',0);
end;
end;
if Assigned(hUrl) then
try
dwIndex := 0;
dwCodeLen := 10;
if HttpQueryInfo(hUrl, HTTP_QUERY_STATUS_CODE, @dwcode, dwcodeLen, dwIndex) then
begin
res := pansichar(@dwcode);
dwNumber := sizeof(Buffer)-1;
if (res ='200') or (res ='302') then
begin
Result:='';
pos:=1;
repeat
FillChar(buffer,SizeOf(buffer),0);
InternetReadFile(hUrl,@buffer,SizeOf(buffer),bytesRead);
SetLength(Result,Length(result)+bytesRead);
Move(Buffer,Result[pos],bytesRead);
inc(pos,bytesRead);
until bytesRead = 0;
end
else
if res='401' then
raise EHTTPException.Create('Not authorized: '+URL+' HTTP Status: '+res,StrToInt(res))
else
raise EHTTPException.Create('Unable to download: '+URL+' HTTP Status: '+res,StrToInt(res));
end
else
raise EHTTPException.Create('Unable to download: '+URL+' code : '+IntToStr(GetLastError)+' ('+GetWinInetError(GetlastError)+')',0);
finally
if Assigned(hUrl) then
InternetCloseHandle(hUrl);
end
else
raise EHTTPException.Create('Unable to download: "'+URL+' code : '+IntToStr(GetLastError)+' ('+GetWinInetError(GetlastError)+')',0);
finally
if Assigned(hConnect) then
InternetCloseHandle(hConnect);
if Assigned(hInet) then
InternetCloseHandle(hInet);
end;
end;
function GetWinInetError(ErrorCode:Cardinal): ansistring;
const
winetdll = 'wininet.dll';
var
Len: Integer;
Buffer: PAnsiChar;
begin
Len := FormatMessage(
FORMAT_MESSAGE_FROM_HMODULE or FORMAT_MESSAGE_FROM_SYSTEM or
FORMAT_MESSAGE_ALLOCATE_BUFFER or FORMAT_MESSAGE_IGNORE_INSERTS or FORMAT_MESSAGE_ARGUMENT_ARRAY,
Pointer(GetModuleHandle(winetdll)), ErrorCode, 0, @Buffer, SizeOf(Buffer), nil);
try
while (Len > 0) and {$IFDEF UNICODE}(CharInSet(Buffer[Len - 1], [#0..#32, '.'])) {$ELSE}(Buffer[Len - 1] in [#0..#32, '.']) {$ENDIF} do Dec(Len);
SetString(Result, Buffer, Len);
finally
LocalFree(HLOCAL(Buffer));
end;
end;
function ignoreCerticateErrors(oRequestHandle:HINTERNET; var aErrorMsg: ansistring): Boolean;
var
vDWFlags: DWord;
vDWFlagsLen: DWord;
begin
Result := False;
try
vDWFlagsLen := SizeOf(vDWFlags);
if not InternetQueryOption(oRequestHandle, INTERNET_OPTION_SECURITY_FLAGS, @vDWFlags, vDWFlagsLen) then begin
aErrorMsg := 'Internal error in SetToIgnoreCerticateErrors when trying to get wininet flags.' + GetWininetError(GetLastError);
Exit;
end;
vDWFlags := vDWFlags or SECURITY_FLAG_IGNORE_UNKNOWN_CA or SECURITY_FLAG_IGNORE_CERT_DATE_INVALID or SECURITY_FLAG_IGNORE_CERT_CN_INVALID or SECURITY_FLAG_IGNORE_REVOCATION;
if not InternetSetOption(oRequestHandle, INTERNET_OPTION_SECURITY_FLAGS, @vDWFlags, vDWFlagsLen) then begin
aErrorMsg := 'Internal error in SetToIgnoreCerticateErrors when trying to set wininet INTERNET_OPTION_SECURITY_FLAGS flag .' + GetWininetError(GetLastError);
Exit;
end;
Result := True;
except
on E: Exception do begin
aErrorMsg := 'Unknown error in SetToIgnoreCerticateErrors.' + E.Message;
end;
end;
end;
function httpPostData(const UserAgent: Ansistring; const url: Ansistring; const Data: RawByteString; enableProxy:Boolean= False;
ConnectTimeout:integer=4000;SendTimeOut:integer=60000;ReceiveTimeOut:integer=60000;user:AnsiString='';password:AnsiString='';VerifyCert:Boolean=False):RawByteString;
var
hInet: HINTERNET;
hHTTP: HINTERNET;
hReq: HINTERNET;
uri:TURI;
pdata,
ErrorMsg:AnsiString;
buffer: array[1..1024] of byte;
flags,bytesRead : DWORD;
pos:integer;
dwindex,dwcodelen: cardinal;
dwcode : array[1..20] of Ansichar;
res : PAnsiChar;
puser,ppassword:PAnsiChar;
const
wall : AnsiString = '*/*';
accept: packed array[0..1] of LPSTR = (@wall, nil);
header: AnsiString = 'Content-Type: application/json';
begin
uri := ParseURI(url,'http',80);
if (uri.Protocol = 'https') and (uri.port=80) then
uri.port := 443;
if enableProxy then
hInet := InternetOpen(PAnsiChar(UserAgent),INTERNET_OPEN_TYPE_PRECONFIG,nil,nil,0)
else
hInet := InternetOpen(PAnsiChar(UserAgent),INTERNET_OPEN_TYPE_DIRECT,nil,nil,0);
try
InternetSetOption(hInet,INTERNET_OPTION_CONNECT_TIMEOUT,@ConnectTimeout,sizeof(integer));
InternetSetOption(hInet,INTERNET_OPTION_SEND_TIMEOUT,@SendTimeOut,sizeof(integer));
InternetSetOption(hInet,INTERNET_OPTION_RECEIVE_TIMEOUT,@ReceiveTimeOut,sizeof(integer));
if uri.Username<>'' then
puser:= PAnsiChar(uri.Username)
else
puser := Nil;
if uri.Password<>'' then
ppassword:=PAnsiChar(uri.Password)
else
ppassword:=Nil;
if user<>'' then
puser:=PAnsiChar(user);
if password<>'' then
ppassword:=PAnsiChar(password);
hHTTP := InternetConnect(hInet, PAnsiChar(uri.Host), uri.Port, puser,ppassword, INTERNET_SERVICE_HTTP, 0, 0);
if hHTTP =Nil then
Raise EHTTPException.Create('Unable to connect to '+url+' code: '+IntToStr(GetLastError)+' ('+UTF8Encode(GetWinInetError(GetlastError))+')',0);
try
flags := INTERNET_FLAG_NO_CACHE_WRITE or INTERNET_FLAG_PRAGMA_NOCACHE or INTERNET_FLAG_RELOAD or INTERNET_FLAG_KEEP_CONNECTION;
if uri.Protocol='https' then
flags := flags or INTERNET_FLAG_SECURE;
hReq := HttpOpenRequestA(hHTTP, PAnsiChar('POST'), PAnsiChar(uri.Document), nil, nil, @accept, flags, 1);
if hReq=Nil then
Raise EHTTPException.Create('Unable to POST to: '+url+' code: '+IntToStr(GetLastError)+' ('+UTF8Encode(GetWinInetError(GetlastError))+')',0);
try
pdata := Data;
if not HttpSendRequestA(hReq, PAnsiChar(header), length(header), PAnsiChar(pdata), length(pdata)) then
begin
ErrorCode:=GetLastError;
if (ErrorCode = ERROR_INTERNET_INVALID_CA) then
begin
if not VerifyCert then ignoreCerticateErrors(hReq, ErrorMsg);
if not HttpSendRequestA(hReq, PAnsiChar(header), length(header), PAnsiChar(pdata), length(pdata)) then
Raise EHTTPException.Create('Unable to send request to '+url+' code : '+IntToStr(GetLastError)+' ('+GetWinInetError(GetlastError)+')',0);
end;
end;
dwIndex := 0;
dwCodeLen := 10;
if HttpQueryInfo(hReq, HTTP_QUERY_STATUS_CODE, @dwcode, dwcodeLen, dwIndex) then
begin
res := pAnsichar(@dwcode);
if (res ='200') or (res ='302') then
begin
Result:='';
pos:=1;
repeat
FillChar(buffer,SizeOf(buffer),0);
InternetReadFile(hReq,@buffer,SizeOf(buffer),bytesRead);
SetLength(Result,Length(result)+bytesRead);
Move(Buffer,Result[pos],bytesRead);
inc(pos,bytesRead);
until bytesRead = 0;
end
else
raise EHTTPException.Create('Unable to get return data for: '+URL+' HTTP Status: '+res,StrToInt(res^));
end
else
Raise EHTTPException.Create('Unable to get http status for: '+url+' code: '+IntToStr(GetLastError)+' ('+UTF8Encode(GetWinInetError(GetlastError))+')',0);
finally
InternetCloseHandle(hReq);
end;
finally
InternetCloseHandle(hHTTP);
end;
finally
InternetCloseHandle(hInet);
end;
end;
{ HTTPException }
constructor EHTTPException.Create(const msg: string; AHTTPStatus: Integer);
begin
inherited Create(msg);
HTTPStatus:=AHTTPStatus;
end;
{$endif} // windows
end.