[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 ...
随机推荐
- HTC One M7简易刷Recovery教程
HTC One M7作为当下HTC旗下的旗舰热门机,用户们对于刷机的需求都比较强烈,对于刷ROM的前提就是要刷入Recovery,当然作为安卓智能手机HTC one而言也不例外,最近有些用 ...
- git下载教程
1.git for windows的下载链接 1.*的版本 https://github.com/msysgit/msysgit/releases 2.*的版本 https://github. ...
- PullToRefreshGridView刷新加载2
@XStreamAlias("result")public class Myresult { @XStreamImplicit(itemFieldName="item&q ...
- 【转载】让你的MATLAB代码飞起来
原文地址:http://developer.51cto.com/art/201104/255128_all.htm MATLAB语言是一种被称为是"演算纸"式的语言,因此追求的是方 ...
- hdu 1010 深搜+剪枝
深度搜索 剪枝 还不是很理解 贴上众神代码 //http://blog.csdn.net/vsooda/article/details/7884772#include<iostream> ...
- 欢迎你,phpWeChat 开发者
感谢您使用 phpWeChat 来作为自己网站或者微信公共号的开发工具.phpWeChat 是一款高效.稳定的网站+微信公共号内容管理系统(CMS),也可称之为一个PHP开发框架. phpWeChat ...
- sql server 条件 not in (null)总是false
SELECT 1 WHERE 2 NOT IN ( 1 ); 结果: (无列名) 1 SELECT 1 WHERE 2 NOT IN ( 1, NULL ); 结果:(无列名) ...
- 关于电信cdma基站nid,sid,bid的解释
通过cdma的基站代码确定该基站的经纬度位置,必须知道Sid.Nid.Bid这三个基站数据,缺一不可. SID 是系统识别码,每个地级市只有一个sid,是唯一的. NID是网络识别码,由各本地网管理, ...
- Ubuntu14.04+RabbitMQ3.6.3+Golang的最佳实践
目录 [TOC] 1.RabbitMQ介绍 1.1.什么是RabbitMQ? RabbitMQ 是由 LShift 提供的一个 Advanced Message Queuing Protocol ...
- PHP PDO的错误处理模式
PDO默认的错误处理方式是返回一个数组格式的错误代码.如果想要判断SQL有没有出错时,就需要写一组代码来检测这个返回的数组.觉得这样有些麻烦.还好PDO还提供了另外两种处理方式,只要根据需要设置一下就 ...
湖南郴州现"多彩空调墙"