-
Notifications
You must be signed in to change notification settings - Fork 0
/
JSajax.pas
63 lines (51 loc) · 1.66 KB
/
JSajax.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
unit JSajax;
interface
uses
msxml, classes, System.Net.HttpClient, Web.HTTPApp, Variants, SysUtils,
windows, jsdownloader;
type
TAjaxResponse = record
mime: string;
response: widestring;
result: integer;
end;
function httpGet(url: string): TAjaxResponse;
function httpPost(url, payload: string; const _type: string = 'application/x-www-form-urlencoded'): TAjaxResponse;
implementation
function httpPost(url, payload: string; const _type: string = 'application/x-www-form-urlencoded'): TAjaxResponse;
var
XMLHTTPRequest: IServerXMLHTTPRequest;
begin
XMLHTTPRequest := CoServerXMLHTTP.Create;
XMLHTTPRequest._AddRef;
XMLHTTPRequest.open('POST', url, false, EmptyParam, EmptyParam);
XMLHTTPRequest.setRequestHeader('Cache-Control', 'no-cache');
XMLHTTPRequest.setRequestHeader('Content-Type', _type);
XMLHTTPRequest.setTimeouts(2 * 1000, 2 * 1000, 6 * 1000, 6 * 1000);
XMLHTTPRequest.send(payload);
result.response := XMLHTTPRequest.responseText;
result.result := XMLHTTPRequest.status;
result.mime := XMLHTTPRequest.getResponseHeader('Content-Type');
// result.resultText := XMLHTTPRequest.statusText;
XMLHTTPRequest._Release;
XMLHTTPRequest := nil;
end;
function httpGet(url: string): TAjaxResponse;
var
HttpClient: THTTPClient;
Response: IHTTPResponse;
begin
HttpClient := THTTPClient.Create;
try
Response := HttpClient.Get(url);
if assigned(Response) then
begin
result.result := Response.StatusCode;
result.response := Response.ContentAsString();
result.mime := Response.MimeType;
end;
finally
HttpClient.Free;
end;
end;
end.