aincopyprint? function Post(AURL: string; ASource: TIdStrings): string; overload; function Post(AURL: string; ASource: TStream): string; overload; function Post(AURL: string; ASource: TIdMultiPartFormDataStream): string; overload; procedure Post(AURL: string; ASource: TIdMultiPartFormDataStream; AResponseContent: TStream); overload; procedure Post(AURL: string; ASource: TIdStrings; AResponseContent: TStream); overload; procedure Post(AURL: string; ASource, AResponseContent: TStream); overload; 其中的基本方法是下面的過程類方法,其他post重載方法均為嵌套使用此方法: [delphi] view plaincopyprint? procedure TIdCustomHTTP.Post(AURL: string; ASource, AResponseContent: TStream); 參數: [delphi] view plaincopyprint? AURL: string // post請求URL ASource: TIdMultiPartFormDataStream // TStream派生的類,其中為發送的流數據及mime信息,可用於上傳文件 ASource: TStream // 發送的流數據 AResponseContent: TStream // 響應內容流ASource: TIdStrings // TString派生的類,用於向服務器提交數據 ASource 為TIdStrings的數據,使用的MIME是默認的“application/x-www-form-urlencoded”,而TIdMultiFormDataStream則是根據發送的內容/文件來設定MIME類型。 示例: [delphi] view plaincopyprint? unit Umain; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdHTTP, StdCtrls, IdMultipartFormData; type TForm1 = class(TForm) IdHTTP1: TIdHTTP; Memo1: TMemo; btnOne: TButton; btnTwo: TButton; btnThree: TButton; btnFour: TButton; btnFive: TButton; btnSix: TButton; procedure btnOneClick(Sender: TObject); procedure btnTwoClick(Sender: TObject); procedure btnThreeClick(Sender: TObject); procedure btnFourClick(Sender: TObject); procedure btnFiveClick(Sender: TObject); procedure btnSixClick(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} const sPostUrl = 'http://cne.csu.edu.cn/reg/mima-pass.asp?path='; procedure TForm1.btnOneClick(Sender: TObject); var postcmd : TStringList; begin postcmd := TStringList.Create; // 組合參數列表 postcmd.Add('AutoGet=1'); postcmd.Add('Logintype=0'); postcmd.Add('password=test'); postcmd.Add('username=test'); Memo1.Text := IdHTTP1.Post(sPostUrl, postcmd); // 以post的方式發送到服務器 end; procedure TForm1.btnTwoClick(Sender: TObject); var postStream : TStringStream; begin IdHTTP1.Request.ContentType := 'application/x-www-form-urlencoded'; // 定義發送mime類型 postStream := TStringStream.Create('AutoGet=1&Logintype=0&password=test&username=test'); // 發送內容 Memo1.Text := IdHTTP1.Post(sPostUrl, postStream); end; procedure TForm1.btnThreeClick(Sender: TObject); var postStream : TIdMultiPartFormDataStream; begin IdHTTP1.HandleRedirects := true; // 允許重定向,因為這個站點會發生重定向 IdHTTP1.Request.Referer := 'http://www.qiuziti.com/'; // 設置來路,此網站要求 postStream := TIdMultiPartFormDataStream.Create; // 創建TIdMultiPartFormDataStream類 postStream.AddFormField('textfield', 'd:\temp\test.png'); // 表單參數 postStream.AddFile('uploaded_file', 'd:\temp\test.png', 'image/png'); // 表單文件 Memo1.Text := Utf8ToAnsi(IdHTTP1.Post('http://www.qiuziti.com/s/upload.ashx', postStream)); end; procedure TForm1.btnFourClick(Sender: TObject); var postStream : TIdMultiPartFormDataStream; respStream : TStringStream; begin IdHTTP1.HandleRedirects := true; // 允許重定向,因為這個站點會發生重定向 IdHTTP1.Request.Referer := 'http://www.qiuziti.com/'; // 設置來路,此網站要求 postStream := TIdMultiPartFormDataStream.Create; // 創建TIdMultiPartFormDataStream類 respStream := TStringStream.Create(''); postStream.AddFormField('textfield', 'd:\temp\test.png'); // 表單參數 postStream.AddFile('uploaded_file', 'd:\temp\test.png', 'image/png'); // 表單文件 IdHTTP1.Post('http://www.qiuziti.com/s/upload.ashx', postStream, respStream); Memo1.Text := Utf8ToAnsi(respStream.DataString); end; procedure TForm1.btnFiveClick(Sender: TObject); var respStream : TStringStream; postcmd : TStringList; begin postcmd := TStringList.Create; respStream := TStringStream.Create(''); postcmd.Add('AutoGet=1'); postcmd.Add('Logintype=0'); postcmd.Add('password=test'); postcmd.Add('username=test'); IdHTTP1.Post(sPostUrl, postcmd, respStream); Memo1.Text := respStream.DataString; end; procedure TForm1.btnSixClick(Sender: TObject); var postStream, respStream : TStringStream; begin www.2cto.com postStream := TStringStream.Create('AutoGet=1&Logintype=0&password=test&username=test'); respStream := TStringStream.Create(''); IdHTTP1.Request.ContentType := 'application/x-www-form-urlencoded'; // 定義發送mime類型 IdHTTP1.Post(sPostUrl, postStream, respStream); Memo1.Text := respStream.DataString; end; end.