[delphi] indy idhttp post 방법

5120 단어 Delphiidhttpindy
idhttp에서 post 방법에 대한 정의:
    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 재부팅 방법은 모두 중첩에 사용됩니다.
procedure TIdCustomHTTP.Post(AURL: string; ASource, AResponseContent: TStream);

매개변수:
AURL: string 	// post  URL
ASource: TIdMultiPartFormDataStream 	// TStream    ,          mime  ,       
ASource: TStream 	//       
AResponseContent: TStream //      ASource: TIdStrings // TString    ,          

Asource는 TidStrings 데이터로 사용되는 MIME는 기본 '응용 프로그램/x-ww-form-urlencoded' 이고 TidMultiForm DataStream은 보내는 내용/파일에 따라 MIME 형식을 설정합니다.
예:
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
  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.

데모 다운로드:
http://download.csdn.net/detail/none01/5130108

좋은 웹페이지 즐겨찾기