[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 ...
随机推荐
- ng-class的用法
最近在学习angular框架,ng-class是angular框架的一个指令,这里是ng-class指令的官方解释: ng-class 指令用于给 HTML 元素动态绑定一个或多个 CSS 类. ng ...
- 为什么js加事件时不要写括号
function al(){alert("我擦")}此时,al代表这个函数的对象,al()就是直接运行了.var nfun=al;就是让nfun也指向了al,此时nfun()就相当 ...
- php操作Memcache
一.memcache基本的介绍 是一个高性能的分布式内存对象缓存系统. 和mysql比较,进行理解memcache (1)该软件是一个c/s架构, (2)mysql的数据是存储到哪里的?是存储到磁盘里 ...
- git subtree 使用
这个是备忘录.原网页(https://medium.com/@porteneuve/mastering-git-subtrees-943d29a798ec , http://cncc.bingj.co ...
- 2016HUAS_ACM暑假集训4B - 递推
这种数学推理题目题意极其明显,在做的时候,可以多写几组,这样找起规律来会容易些.概括起来就是:题意简单暴力,案例毫无价值. 一个三角形最多可以把一个平面分成两部分,两个三角形最多是8(2+6)部分,而 ...
- 【PKU1001】Exponentiation(高精度乘法)
Exponentiation Time Limit: 500MS Memory Limit: 10000K Total Submissions: 145642 Accepted: 35529 ...
- Http中 Post和 Get的区别
1.表面上的区别 1.GET在浏览器回退时,是无害的,而Post会再次提交请求 2.Get产生的Url地址会被Bookmark,而Post不会 3.Get请求会被浏览器主动Cache,而Post不会, ...
- 深入理解CSS中的层叠上下文和层叠顺序(转)
by zhangxinxu from http://www.zhangxinxu.com 本文地址:http://www.zhangxinxu.com/wordpress/?p=5115 零.世间的道 ...
- jquery筛选器
1.过滤 eq(index|-index) 获取当前链式操作中第N个jquery对象,正数从0开始,负数从-1开始. first 获取当前链式操作中第1个jquery对象 last 获取当前链式 ...
- Java高级开发工程师面试考纲 转
转 http://www.sanesee.com/article/java-engineer-interview-of-content-tree 1 Java基础 1.1 Collection和Map ...
湖南郴州现"多彩空调墙"