-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnet.pas
More file actions
128 lines (112 loc) · 3.45 KB
/
net.pas
File metadata and controls
128 lines (112 loc) · 3.45 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
unit Net;
interface
uses
classes;
type
TResponse = record
text: AnsiString;
data: Array of Byte;
code: Integer;
end;
TFile = record
name: String;
filename: String;
contenttype: String;
contents: TStream;
end;
function get(url: String; timeout: Integer=0): TResponse;
function post(url: String; formData: Array of AnsiString; fileData: Array of TFile;
timeout: Integer=0): TResponse;
implementation
uses
SysUtils, fphttpclient, fpopenssl, openssl;
var
encoder: TEncoding;
//function writeFunction(pBuff: Pointer; size: Integer; nmemb: Integer; pUserData: Pointer): Integer;
//begin
// //writeLn(String(pBuff));
// //Response(pUserData).text := Response(pUserData).text + String(pBuff);
// TStream(pUserData).write(pBuff^, size*nmemb);
// writeFunction := size*nmemb;
//end;
function get(url: String; timeout: Integer=0): TResponse;
var
bs: TBytesStream;
resp: TResponse;
client: TFPHTTPClient;
begin
bs := TBytesStream.Create();
client := TFPHttpClient.Create(nil);
client.IOTimeout := timeout;
client.get(url, bs);
resp.code := client.ResponseStatusCode;
resp.text := encoder.GetString(bs.Bytes);
resp.data := bs.Bytes;
FreeAndNil(client);
FreeAndNil(bs);
result := resp;
end;
function post(url: String; formData: Array of AnsiString; fileData: Array of TFile;
timeout: Integer=0): TResponse;
const
CRLF = #13#10;
var
bs: TBytesStream;
client: TFPHTTPClient;
S, Sep : String;
SS : TStringStream;
I: Integer;
N,V: String;
begin
bs := TBytesStream.Create();
client := TFPHTTPClient.Create(nil);
client.IOTimeout := timeout;
Sep:=Format('%.8x_multipart_boundary',[Random($ffffff)]);
client.AddHeader('Content-Type','multipart/form-data; boundary='+Sep);
SS:=TStringStream.Create('');
if (length(formData)<>0) or (length(fileData)<>0) then
begin
S := '--'+Sep+CRLF;
SS.WriteBuffer(s[1], length(S));
end;
if (length(formData)<>0) then
for I:=0 to length(formData) -1 do
begin
// not url encoded
n := copy(formData[i], 0, pos('=', formData[i])-1);
v := copy(formData[i], pos('=', formData[i])+1, length(formData[i])-pos('=', formData[i]));
S := '';
S:=S+Format('Content-Disposition: form-data; name="%s"'+CRLF+CRLF+'%s'+CRLF,[n, v]);
S:=S+'--'+Sep+CRLF;
SS.WriteBuffer(S[1],Length(S));
end;
//writeln(ss.DataString);
if (length(fileData)<>0) then
for I:=0 to length(fileData) -1 do
begin
S:='';
s:=s+Format('Content-Disposition: form-data; name="%s"; filename="%s"'+CRLF,[fileData[i].name,ExtractFileName(fileData[i].filename)]);
s:=s+'Content-Type: '+fileData[i].contentType+CRLF+CRLF;
SS.WriteBuffer(S[1],Length(S));
fileData[i].contents.Seek(0, soFromBeginning);
SS.CopyFrom(fileData[i].contents,fileData[i].contents.Size);
S:=CRLF+'--'+Sep+CRLF;
SS.WriteBuffer(S[1],Length(S));
end;
//writeln(ss.DataString);
SS.Position:=0;
client.RequestBody:=SS;
client.Post(url, bs);
result.code := client.ResponseStatusCode;
result.text := encoder.GetString(bs.Bytes);
result.data := bs.Bytes;
FreeAndNil(bs);
client.RequestBody:=Nil;
FreeAndNil(client);
FreeAndNil(SS);
end;
initialization
begin
encoder := TEncoding.UTF8;
end;
end.