source: trunk/Packages/synapse/source/demo/http/Unit1.pas

Last change on this file was 2, checked in by chronos, 12 years ago
  • Přidáno: Základní kostra projektu.
  • Přidáno: Knihovna synapse.
File size: 3.5 KB
Line 
1unit Unit1;
2
3interface
4
5uses
6 Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
7 HTTPSend, StdCtrls, ExtCtrls;
8
9type
10 TForm1 = class(TForm)
11 Panel1: TPanel;
12 Panel2: TPanel;
13 Panel3: TPanel;
14 Panel4: TPanel;
15 Memo1: TMemo;
16 Memo2: TMemo;
17 Label1: TLabel;
18 Edit1: TEdit;
19 Button1: TButton;
20 Label2: TLabel;
21 Label3: TLabel;
22 Edit2: TEdit;
23 Button2: TButton;
24 Label4: TLabel;
25 Label5: TLabel;
26 Edit3: TEdit;
27 Label6: TLabel;
28 Edit4: TEdit;
29 Label7: TLabel;
30 Label8: TLabel;
31 Edit5: TEdit;
32 Button3: TButton;
33 Label9: TLabel;
34 Edit6: TEdit;
35 Edit7: TEdit;
36 Label10: TLabel;
37 Panel5: TPanel;
38 Label11: TLabel;
39 Label12: TLabel;
40 Edit8: TEdit;
41 Edit9: TEdit;
42 Label13: TLabel;
43 procedure Button1Click(Sender: TObject);
44 procedure Button2Click(Sender: TObject);
45 procedure Button3Click(Sender: TObject);
46 private
47 { Private declarations }
48 public
49 { Public declarations }
50 end;
51
52var
53 Form1: TForm1;
54
55implementation
56
57{$R *.DFM}
58
59function ProxyHttpPostURL(const URL, URLData: string; const Data: TStream): Boolean;
60var
61 HTTP: THTTPSend;
62begin
63 HTTP := THTTPSend.Create;
64 try
65 HTTP.ProxyHost := Form1.Edit8.Text;
66 HTTP.ProxyPort := Form1.Edit9.Text;
67 HTTP.Document.Write(Pointer(URLData)^, Length(URLData));
68 HTTP.MimeType := 'application/x-www-form-urlencoded';
69 Result := HTTP.HTTPMethod('POST', URL);
70 Data.CopyFrom(HTTP.Document, 0);
71 finally
72 HTTP.Free;
73 end;
74end;
75
76function ProxyHttpPostFile(const URL, FieldName, FileName: string;
77 const Data: TStream; const ResultData: TStrings): Boolean;
78const
79 CRLF = #$0D + #$0A;
80var
81 HTTP: THTTPSend;
82 Bound, s: string;
83begin
84 Bound := IntToHex(Random(MaxInt), 8) + '_Synapse_boundary';
85 HTTP := THTTPSend.Create;
86 try
87 HTTP.ProxyHost := Form1.Edit8.Text;
88 HTTP.ProxyPort := Form1.Edit9.Text;
89 s := '--' + Bound + CRLF;
90 s := s + 'content-disposition: form-data; name="' + FieldName + '";';
91 s := s + ' filename="' + FileName +'"' + CRLF;
92 s := s + 'Content-Type: Application/octet-string' + CRLF + CRLF;
93 HTTP.Document.Write(Pointer(s)^, Length(s));
94 HTTP.Document.CopyFrom(Data, 0);
95 s := CRLF + '--' + Bound + '--' + CRLF;
96 HTTP.Document.Write(Pointer(s)^, Length(s));
97 HTTP.MimeType := 'multipart/form-data, boundary=' + Bound;
98 Result := HTTP.HTTPMethod('POST', URL);
99 ResultData.LoadFromStream(HTTP.Document);
100 finally
101 HTTP.Free;
102 end;
103end;
104
105
106procedure TForm1.Button1Click(Sender: TObject);
107var
108 HTTP: THTTPSend;
109begin
110 HTTP := THTTPSend.Create;
111 try
112 HTTP.ProxyHost := Edit8.Text;
113 HTTP.ProxyPort := Edit9.Text;
114 HTTP.HTTPMethod('GET', Edit1.text);
115 Memo1.Lines.Assign(HTTP.Headers);
116 Memo2.Lines.LoadFromStream(HTTP.Document);
117 finally
118 HTTP.Free;
119 end;
120end;
121
122procedure TForm1.Button2Click(Sender: TObject);
123var
124 st: TMemoryStream;
125begin
126 st:=TMemoryStream.Create;
127 try
128 ProxyHTTPpostURL(Edit2.Text, Edit3.Text + '=' + Edit4.Text, st);
129 st.Seek(0,soFromBeginning);
130 Memo2.Lines.LoadFromStream(st);
131 finally
132 st.Free;
133 end;
134end;
135
136procedure TForm1.Button3Click(Sender: TObject);
137var
138 st: TFileStream;
139begin
140 st := TFileStream.Create(Edit7.Text, fmOpenRead or fmShareDenyWrite);
141 try
142 ProxyHTTPPostFile(Edit5.Text, Edit6.Text, ExtractFilename(Edit7.Text), st, TStringList(memo2.Lines));
143 finally
144 st.Free;
145 end;
146end;
147
148end.
Note: See TracBrowser for help on using the repository browser.