[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 ...
随机推荐
- javascript在html中的加载顺序
参考:[1]http://coolshell.cn/articles/9749.html(酷壳) [2]http://shaozhuqing.com/?p=2756 [3]http://www.cnb ...
- Repeater 根据某一列的值加颜色
//排队中 protected void rptOrdersList_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterIt ...
- 如何得到自定义UITableViewCell中的按钮所在的cell的indexPath.row
在自定义UITableViewCell中创建了一个按钮. 想在点击该按钮时知道该按钮所在的cell在TableView中的行数.就是cell的 indexPath.row两种方法都很好.-(IBAct ...
- android 编译
编译 Android完全编译,耗时 1 小时 25 分$ make编译当前目录下的模块,耗时 1 小时 31 分mm编译指定目录下的模块mmm 模块的根目录清除上次编译输出make clean单独编译 ...
- ie 8 下post提交提交了两次
擦你吗呀,IE8! 老子写一个登录功能,IE他妈的给我登录了两次,导致权限校验错误,什么他妈的鬼问题,调了两天....fuck,都是泪水. 解决方案:提交按钮加返回值<input type=&q ...
- 文件操作 模式r+与w+
r+与w+ r+是读写模式,在文件的末尾进行追加操作. >>> myfile=open('pwd.txt', ... 'r+') >>> myfile.read() ...
- sublime插件使用整理
考虑到后续要有更多的时间来写js,周末好好的把sublime整理了下,很多插件非常好.下面一一来说 1. 注释生成插件 DocBlockr 之前每次写函数的注释都要复制其他函数的注释,然后在写,非常 ...
- Linux监控分析实战-1
监控概述及5个大指标 cpu mem 内存 io 磁盘交互 load 负载 Network 网络 它们之间关系是相互彼此依赖,任何一个高负载都会到导致其他指标出现问题: 网卡 ...
- docker 私有镜像管理工具harbor 安装
因为各种原因,官方的离线安装包下载比较费事,经常不成功,所以通过分部安装解决问题 1. docker yum install libdevmapper* -y -H tcp://0.0.0.0:237 ...
- mybatis与 Exception
mybatis将所有的异常全部包成了运行时异常,减少在高层代码中频繁的try-catch导致的代码臃肿问题.Persistence是它们共有的父类,继承自RuntimeException非检查型异常. ...