[delphi]indy idhttp post方法

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是默认的“application/x-www-form-urlencoded”,而TIdMultiFormDataStream则是根据发送的内容/文件来设定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.

Demo下载:

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

Delphi 7是一种集成开发环境(IDE),用于编写Windows应用程序。在Delphi 7中进行HTTP POSTHTTPS请求的实例下载,可以通过使用Indy组件库来实现。 Indy是一个强大的开放源代码的网络通信库,它包含了用于HTTPHTTPS请求的组件。首先,我们需要下载Indy组件库并将其添加到Delphi 7的工程中。 接下来,我们可以使用以下代码来进行HTTP POST请求的实例下载: ```delphi uses IdHTTP; procedure DownloadFile(const AURL, AFileName: string); var HTTP: TIdHTTP; begin HTTP := TIdHTTP.Create(nil); try HTTP.Get(AURL, AFileName); finally HTTP.Free; end; end; ``` 在上述代码中,`DownloadFile`过程接受一个URL参数和一个目标文件名参数,然后使用`TIdHTTP`组件的`Get`方法将URL中的内容下载到指定的文件中。 要实现HTTPS请求的实例下载,我们需要在上述代码中添加一些额外的设置,以确保安全连接。以下是一个示例: ```delphi uses IdHTTP, IdSSLOpenSSL; procedure DownloadFileHTTPS(const AURL, AFileName: string); var HTTP: TIdHTTP; SSL: TIdSSLIOHandlerSocketOpenSSL; begin SSL := TIdSSLIOHandlerSocketOpenSSL.Create(nil); HTTP := TIdHTTP.Create(nil); try HTTP.IOHandler := SSL; HTTP.Get(AURL, AFileName); finally HTTP.Free; SSL.Free; end; end; ``` 在上述代码中,我们创建了一个`TIdSSLIOHandlerSocketOpenSSL`对象,并将其分配给`TIdHTTP`组件的`IOHandler`属性。这将确保我们使用SSL/TLS协议进行安全连接。 以上就是在Delphi 7中实现HTTP POSTHTTPS请求的实例下载的方法。在使用这些代码之前,请确保你在Delphi 7中正确配置了Indy组件库。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值