[delphi]indy idhttp post方法
techiepc的博客
万事如意
日志
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
网易公司版权所有 ©1997-2015
[delphi]indy idhttp post方法的更多相关文章
- Delphi Indy IDHttp 403 forbidden
http://hbk777.blog.163.com/blog/static/6058086200681594333361/ Delphi Indy IDHttp 403 forbidden 2006 ...
- delphi indy Idhttp error:1409442E:SSL routines:SSL3_READ_BYTES:tlsv1 alert protocol version
在使用 indy 中的 idhttp 组件访问 https 网站时,出现如下错误: error:1409442E:SSL routines:SSL3_READ_BYTES:tlsv1 alert pr ...
- Delphi之静态方法,虚方法virtual,动态dynamic,抽象abstract,消息
Delphi之静态方法,虚方法virtual,动态dynamic,抽象abstract,消息 http://www.cnblogs.com/zhwx/archive/2012/08/28/266055 ...
- Delphi的移动文件方法(转)/删除文件:/文件的复制
RenameFile,DeleteFile,MoveFile Delphi的移动文件方法 uses ShellApi; procedure ShellFileOperation(fromFile: ...
- delphi操作文本文件的方法简介
delphi操作文本文件的方法简介减小字体 增大字体 作者佚名来源不详发布时间2008-5-31 10:31:16发布人xuedelphi1 文件类型和标准过程 Delphi同Object ...
- delphi使用IdHTTP模拟提交页面方法总结
http://blog.csdn.net/lxdcyh/article/details/3986800 1.拖入TIdHTTP控件,HandleRedirect设为True,否则可能会出现HTTP 3 ...
- Delphi的idhttp报508 Loop Detected错误的原因
一般是访问https时才出现“508 Loop Detected”,idhttp+IdSSLIOHandlerSocketOpenSSL,这个在上篇文章中讲过了. 由于该问题网上资料极少,连外文资料也 ...
- delphi 用idhttp做web页面数据抓取 注意事项
这里不讨论webbrowse方式了 .直接采用indy的 idhttp Get post 可以很方便的获取网页数据. 但如果要抓取大量数据 程序稳定运行不崩溃就不那么容易了.这几年也做了不少类似工具 ...
- INDY idhttp Post用法
http://www.cnblogs.com/tk-del/archive/2013/05/10/3071541.html function Post(AURL: string; ASource: T ...
随机推荐
- 不就ideas嘛,谁没有!
20160214 survey of current RDF triple storage systems survey of semantic web stack inference mechani ...
- mysql 用法记录和常见错误,持续更新。
2016-10-20 08:31:46 在navicat创建表的时候,遇到"#1166 - Incorrect column name'Id'"问题,原因是创建的字段中有空格(是直 ...
- setTimeout的应用
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- C#的选择语句练习2
1.输入a,b,c三个数,计算一元二次方程ax²+bx+c的根:若a=0,则不是一元二次方程:△=b²-4ac,根的计算公式为-b±√b²-4ac/2a:若△=b²-4ac>0,则方程有两个不一 ...
- xxxxxxx
int c; int b; bool h;
- hadoop 集群跑的时候用到hbasejar 文件的引用问题
1. 创建软连接 ln -s /home/hadoop/bigdater/hbase-0.98.6-cdh5.3.6/conf/hbase-site.xml ./hbase-site.xml(记得这里 ...
- C# 模拟按下回车键自动登录
private void Form1_Load(object sender, EventArgs e) { //this.Show(); this.Activate(); //this.Focus() ...
- AT指令(转)
资料来自网络 附录AT指令简编一. 一般命令1.AT+CGMI 给出模块厂商的标识.2.AT+CGMM 获得模块标识.这个命令用来得到支持的频带(GSM 900,DCS 1800 或PCS 1900) ...
- JSBinding / Plugins & Build Mozjswrap Library
There are 2 libraries in Plugins: mozjs-31. This is SpiderMonkey library, built from https://github. ...
- jdbc/DAO模式
DAO设计模式: 1.DAO: DAO设计模式是属于J2EE数据层的操作,使用DAO设计模式可以简化大量代码,增强程序的可移植性. 2.DAO各部分详解:DAO设计模式包括以下4个主要部分: ...
湖南郴州现"多彩空调墙"