1 unit Unit1; 2 3 interface 4 5 uses 6 Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, 7 Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, 8 WinHttp_TLB, //需要引入 9 ComObj; //需要引入 10 11 type 12 TForm1 = class(TForm) 13 Button1: TButton; 14 Memo1: TMemo; 15 procedure FormCreate(Sender: TObject); 16 procedure Button1Click(Sender: TObject); 17 private 18 { Private declarations } 19 public 20 { Public declarations } 21 Http: WinhttpRequest; 22 function HPost(PostUrl, Post: string): WideString; 23 end; 24 25 var 26 Form1: TForm1; 27 28 implementation 29 30 {$R *.dfm} 31 32 function TForm1.HPost(PostUrl, Post: string): WideString; 33 var 34 // Stream: TMemoryStream; 35 url, wStr: WideString; 36 begin 37 try 38 Url := PostUrl; 39 Http.Open('POST', url, EmptyParam); 40 // Http.SetRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 41 Http.SetRequestHeader('Content-Type', 'application/json'); 42 Http.Option[6] := false; 43 Http.Send(OleVariant(Post)); 44 // stream := OleVariantToMemoryStream(http.ResponseBody); 45 // Stream.Position := 0; 46 wStr := Http.ResponseText; 47 // stream.Free; 48 except 49 // Stream.Free; 50 wStr := ''; 51 end; 52 result := wStr; 53 end; 54 55 procedure TForm1.Button1Click(Sender: TObject); 56 begin 57 Memo1.Text := HPost('https://mail.qq.com/', 'Post'); 58 end; 59 60 procedure TForm1.FormCreate(Sender: TObject); 61 begin 62 Http := CreateComObject(CLASS_WinHttpRequest) as WinHttpRequest; 63 Http.SetTimeouts(10000, 10000, 5000, 5000); 64 end; 65 66 end.