idhttp中對於post方法的定義

idhttp中對於post方法的定義:

  1. function Post(AURL: string; ASource: TIdStrings): string; overload;  
  2. function Post(AURL: string; ASource: TStream): string; overload;  
  3. function Post(AURL: string; ASource: TIdMultiPartFormDataStream): string; overload;  
  4. procedure Post(AURL: string; ASource: TIdMultiPartFormDataStream; AResponseContent: TStream); overload;  
  5. procedure Post(AURL: string; ASource: TIdStrings; AResponseContent: TStream); overload;  
  6. procedure Post(AURL: string; ASource, AResponseContent: TStream); overload;  

其中的基本方法是下面的過程類方法,其他post重載方法均爲嵌套使用此方法:

  1. procedure TIdCustomHTTP.Post(AURL: string; ASource, AResponseContent: TStream);  

參數:
  1. AURL: string    // post請求URL   
  2. ASource: TIdMultiPartFormDataStream     // TStream派生的類,其中爲發送的流數據及mime信息,可用於上傳文件   
  3. ASource: TStream    // 發送的流數據   
  4. AResponseContent: TStream // 響應內容流ASource: TIdStrings // TString派生的類,用於向服務器提交數據  

ASource 爲TIdStrings的數據,使用的MIME是默認的“application/x-www-form-urlencoded”,而TIdMultiFormDataStream則是根據發送的內容/文件來設定MIME類型。

示例:

  1. unit Umain;  
  2.   
  3. interface  
  4.   
  5. uses  
  6.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,  
  7.   Dialogs, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,  
  8.   IdHTTP, StdCtrls, IdMultipartFormData;  
  9.   
  10. type  
  11.   TForm1 = class(TForm)  
  12.     IdHTTP1: TIdHTTP;  
  13.     Memo1: TMemo;  
  14.     btnOne: TButton;  
  15.     btnTwo: TButton;  
  16.     btnThree: TButton;  
  17.     btnFour: TButton;  
  18.     btnFive: TButton;  
  19.     btnSix: TButton;  
  20.     procedure btnOneClick(Sender: TObject);  
  21.     procedure btnTwoClick(Sender: TObject);  
  22.     procedure btnThreeClick(Sender: TObject);  
  23.     procedure btnFourClick(Sender: TObject);  
  24.     procedure btnFiveClick(Sender: TObject);  
  25.     procedure btnSixClick(Sender: TObject);  
  26.   private  
  27.     { Private declarations }  
  28.   public  
  29.     { Public declarations }  
  30.   end;  
  31.   
  32. var  
  33.   Form1: TForm1;  
  34.   
  35. implementation  
  36.   
  37. {$R *.dfm}  
  38.   
  39. const  
  40.   sPostUrl = 'http://cne.csu.edu.cn/reg/mima-pass.asp?path=';  
  41.   
  42. procedure TForm1.btnOneClick(Sender: TObject);  
  43. var  
  44.   postcmd : TStringList;  
  45. begin  
  46.   postcmd := TStringList.Create;  // 組合參數列表   
  47.   postcmd.Add('AutoGet=1');  
  48.   postcmd.Add('Logintype=0');  
  49.   postcmd.Add('password=test');  
  50.   postcmd.Add('username=test');  
  51.   Memo1.Text := IdHTTP1.Post(sPostUrl, postcmd);  // 以post的方式發送到服務器   
  52. end;  
  53.   
  54. procedure TForm1.btnTwoClick(Sender: TObject);  
  55. var  
  56.   postStream : TStringStream;  
  57. begin  
  58.   IdHTTP1.Request.ContentType := 'application/x-www-form-urlencoded'; // 定義發送mime類型   
  59.   postStream := TStringStream.Create('AutoGet=1&Logintype=0&password=test&username=test');  // 發送內容   
  60.   Memo1.Text := IdHTTP1.Post(sPostUrl, postStream);  
  61. end;  
  62.   
  63. procedure TForm1.btnThreeClick(Sender: TObject);  
  64. var  
  65.   postStream : TIdMultiPartFormDataStream;  
  66. begin  
  67.   IdHTTP1.HandleRedirects := true;  // 允許重定向,因爲這個站點會發生重定向   
  68.   IdHTTP1.Request.Referer := 'http://www.qiuziti.com/'; // 設置來路,此網站要求  
  69.   
  70.   postStream := TIdMultiPartFormDataStream.Create;  // 創建TIdMultiPartFormDataStream類   
  71.   
  72.   postStream.AddFormField('textfield', 'd:\temp\test.png'); // 表單參數   
  73.   postStream.AddFile('uploaded_file', 'd:\temp\test.png', 'image/png'); // 表單文件   
  74.   Memo1.Text := Utf8ToAnsi(IdHTTP1.Post('http://www.qiuziti.com/s/upload.ashx', postStream));  
  75. end;  
  76.   
  77. procedure TForm1.btnFourClick(Sender: TObject);  
  78. var  
  79.   postStream : TIdMultiPartFormDataStream;  
  80.   respStream : TStringStream;  
  81. begin  
  82.   IdHTTP1.HandleRedirects := true;  // 允許重定向,因爲這個站點會發生重定向   
  83.   IdHTTP1.Request.Referer := 'http://www.qiuziti.com/'; // 設置來路,此網站要求  
  84.   
  85.   postStream := TIdMultiPartFormDataStream.Create;  // 創建TIdMultiPartFormDataStream類   
  86.   respStream := TStringStream.Create('');  
  87.   
  88.   postStream.AddFormField('textfield', 'd:\temp\test.png'); // 表單參數   
  89.   postStream.AddFile('uploaded_file', 'd:\temp\test.png', 'image/png'); // 表單文件   
  90.   
  91.   IdHTTP1.Post('http://www.qiuziti.com/s/upload.ashx', postStream, respStream);  
  92.   
  93.   Memo1.Text := Utf8ToAnsi(respStream.DataString);  
  94. end;  
  95.   
  96. procedure TForm1.btnFiveClick(Sender: TObject);  
  97. var  
  98.   respStream : TStringStream;  
  99.   postcmd : TStringList;  
  100. begin  
  101.   postcmd := TStringList.Create;  
  102.   respStream := TStringStream.Create('');  
  103.   
  104.   postcmd.Add('AutoGet=1');  
  105.   postcmd.Add('Logintype=0');  
  106.   postcmd.Add('password=test');  
  107.   postcmd.Add('username=test');  
  108.   
  109.   IdHTTP1.Post(sPostUrl, postcmd, respStream);  
  110.   
  111.   Memo1.Text := respStream.DataString;  
  112. end;  
  113.   
  114. procedure TForm1.btnSixClick(Sender: TObject);  
  115. var  
  116.   postStream, respStream : TStringStream;  
  117. begin  
  118.   postStream := TStringStream.Create('AutoGet=1&Logintype=0&password=test&username=test');  
  119.   respStream := TStringStream.Create('');  
  120.   IdHTTP1.Request.ContentType := 'application/x-www-form-urlencoded'; // 定義發送mime類型   
  121.   IdHTTP1.Post(sPostUrl, postStream, respStream);  
  122.   
  123.   Memo1.Text := respStream.DataString;  
  124. end;  
  125.   
  126. end.  

Demo下載:

http://download.csdn.net/detail/none01/5130108

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章