techiepc的博客

万事如意

日志

 
 
 

[delphi]indy idhttp post方法

2013-10-13 10:49:40|  分类: 默认分类 |  标签:delphi  idhttp  post  indy  |举报|字号 订阅

 
 

idhttp中对于post方法的定义:

  1. function Post(AURL: string; ASource: TIdStrings): string; overload;
  2. function Post(AURL: string; ASource: TStream): string; overload;
  3. function Post(AURL: string; ASource: TIdMultiPartFormDataStream): string; overload;
  4. procedure Post(AURL: string; ASource: TIdMultiPartFormDataStream; AResponseContent: TStream); overload;
  5. procedure Post(AURL: string; ASource: TIdStrings; AResponseContent: TStream); overload;
  6. procedure Post(AURL: string; ASource, AResponseContent: TStream); overload;

其中的基本方法是下面的过程类方法,其他post重载方法均为嵌套使用此方法:

  1. procedure TIdCustomHTTP.Post(AURL: string; ASource, AResponseContent: TStream);

参数:

  1. AURL: string    // post请求URL
  2. ASource: TIdMultiPartFormDataStream     // TStream派生的类,其中为发送的流数据及mime信息,可用于上传文件
  3. ASource: TStream    // 发送的流数据
  4. AResponseContent: TStream // 响应内容流ASource: TIdStrings // TString派生的类,用于向服务器提交数据

ASource 为TIdStrings的数据,使用的MIME是默认的“application/x-www-form-urlencoded”,而TIdMultiFormDataStream则是根据发送的内容/文件来设定MIME类型。

示例:

  1. unit Umain;
  2. interface
  3. uses
  4. Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  5. Dialogs, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
  6. IdHTTP, StdCtrls, IdMultipartFormData;
  7. type
  8. TForm1 = class(TForm)
  9. IdHTTP1: TIdHTTP;
  10. Memo1: TMemo;
  11. btnOne: TButton;
  12. btnTwo: TButton;
  13. btnThree: TButton;
  14. btnFour: TButton;
  15. btnFive: TButton;
  16. btnSix: TButton;
  17. procedure btnOneClick(Sender: TObject);
  18. procedure btnTwoClick(Sender: TObject);
  19. procedure btnThreeClick(Sender: TObject);
  20. procedure btnFourClick(Sender: TObject);
  21. procedure btnFiveClick(Sender: TObject);
  22. procedure btnSixClick(Sender: TObject);
  23. private
  24. { Private declarations }
  25. public
  26. { Public declarations }
  27. end;
  28. var
  29. Form1: TForm1;
  30. implementation
  31. {$R *.dfm}
  32. const
  33. sPostUrl = 'http://cne.csu.edu.cn/reg/mima-pass.asp?path=';
  34. procedure TForm1.btnOneClick(Sender: TObject);
  35. var
  36. postcmd : TStringList;
  37. begin
  38. postcmd := TStringList.Create;  // 组合参数列表
  39. postcmd.Add('AutoGet=1');
  40. postcmd.Add('Logintype=0');
  41. postcmd.Add('password=test');
  42. postcmd.Add('username=test');
  43. Memo1.Text := IdHTTP1.Post(sPostUrl, postcmd);  // 以post的方式发送到服务器
  44. end;
  45. procedure TForm1.btnTwoClick(Sender: TObject);
  46. var
  47. postStream : TStringStream;
  48. begin
  49. IdHTTP1.Request.ContentType := 'application/x-www-form-urlencoded'; // 定义发送mime类型
  50. postStream := TStringStream.Create('AutoGet=1&Logintype=0&password=test&username=test');  // 发送内容
  51. Memo1.Text := IdHTTP1.Post(sPostUrl, postStream);
  52. end;
  53. procedure TForm1.btnThreeClick(Sender: TObject);
  54. var
  55. postStream : TIdMultiPartFormDataStream;
  56. begin
  57. IdHTTP1.HandleRedirects := true;  // 允许重定向,因为这个站点会发生重定向
  58. IdHTTP1.Request.Referer := 'http://www.qiuziti.com/'; // 设置来路,此网站要求
  59. postStream := TIdMultiPartFormDataStream.Create;  // 创建TIdMultiPartFormDataStream类
  60. postStream.AddFormField('textfield', 'd:\temp\test.png'); // 表单参数
  61. postStream.AddFile('uploaded_file', 'd:\temp\test.png', 'image/png'); // 表单文件
  62. Memo1.Text := Utf8ToAnsi(IdHTTP1.Post('http://www.qiuziti.com/s/upload.ashx', postStream));
  63. end;
  64. procedure TForm1.btnFourClick(Sender: TObject);
  65. var
  66. postStream : TIdMultiPartFormDataStream;
  67. respStream : TStringStream;
  68. begin
  69. IdHTTP1.HandleRedirects := true;  // 允许重定向,因为这个站点会发生重定向
  70. IdHTTP1.Request.Referer := 'http://www.qiuziti.com/'; // 设置来路,此网站要求
  71. postStream := TIdMultiPartFormDataStream.Create;  // 创建TIdMultiPartFormDataStream类
  72. respStream := TStringStream.Create('');
  73. postStream.AddFormField('textfield', 'd:\temp\test.png'); // 表单参数
  74. postStream.AddFile('uploaded_file', 'd:\temp\test.png', 'image/png'); // 表单文件
  75. IdHTTP1.Post('http://www.qiuziti.com/s/upload.ashx', postStream, respStream);
  76. Memo1.Text := Utf8ToAnsi(respStream.DataString);
  77. end;
  78. procedure TForm1.btnFiveClick(Sender: TObject);
  79. var
  80. respStream : TStringStream;
  81. postcmd : TStringList;
  82. begin
  83. postcmd := TStringList.Create;
  84. respStream := TStringStream.Create('');
  85. postcmd.Add('AutoGet=1');
  86. postcmd.Add('Logintype=0');
  87. postcmd.Add('password=test');
  88. postcmd.Add('username=test');
  89. IdHTTP1.Post(sPostUrl, postcmd, respStream);
  90. Memo1.Text := respStream.DataString;
  91. end;
  92. procedure TForm1.btnSixClick(Sender: TObject);
  93. var
  94. postStream, respStream : TStringStream;
  95. begin
  96. postStream := TStringStream.Create('AutoGet=1&Logintype=0&password=test&username=test');
  97. respStream := TStringStream.Create('');
  98. IdHTTP1.Request.ContentType := 'application/x-www-form-urlencoded'; // 定义发送mime类型
  99. IdHTTP1.Post(sPostUrl, postStream, respStream);
  100. Memo1.Text := respStream.DataString;
  101. end;
  102. end.

Demo下载:

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

 
 
 
 
阅读(1547)| 评论(0)

 

 
喜欢推荐转载
 

 

 

 

 

 

 

 

 
 

 

在LOFTER的更多文章

关闭

玩LOFTER,免费冲印20张照片,人人有奖!     我要抢>

评论

  该内容仅供欣赏。
 

网易公司版权所有 ©1997-2015

 
 
 
 
加入网易博客注册

[delphi]indy idhttp post方法的更多相关文章

  1. Delphi Indy IDHttp 403 forbidden

    http://hbk777.blog.163.com/blog/static/6058086200681594333361/ Delphi Indy IDHttp 403 forbidden 2006 ...

  2. 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 ...

  3. Delphi之静态方法,虚方法virtual,动态dynamic,抽象abstract,消息

    Delphi之静态方法,虚方法virtual,动态dynamic,抽象abstract,消息 http://www.cnblogs.com/zhwx/archive/2012/08/28/266055 ...

  4. Delphi的移动文件方法(转)/删除文件:/文件的复制

    RenameFile,DeleteFile,MoveFile Delphi的移动文件方法 uses  ShellApi; procedure ShellFileOperation(fromFile: ...

  5. delphi操作文本文件的方法简介

    delphi操作文本文件的方法简介减小字体 增大字体 作者佚名来源不详发布时间2008-5-31 10:31:16发布人xuedelphi1 文件类型和标准过程       Delphi同Object ...

  6. delphi使用IdHTTP模拟提交页面方法总结

    http://blog.csdn.net/lxdcyh/article/details/3986800 1.拖入TIdHTTP控件,HandleRedirect设为True,否则可能会出现HTTP 3 ...

  7. Delphi的idhttp报508 Loop Detected错误的原因

    一般是访问https时才出现“508 Loop Detected”,idhttp+IdSSLIOHandlerSocketOpenSSL,这个在上篇文章中讲过了. 由于该问题网上资料极少,连外文资料也 ...

  8. delphi 用idhttp做web页面数据抓取 注意事项

    这里不讨论webbrowse方式了 .直接采用indy的 idhttp  Get post 可以很方便的获取网页数据. 但如果要抓取大量数据 程序稳定运行不崩溃就不那么容易了.这几年也做了不少类似工具 ...

  9. INDY idhttp Post用法

    http://www.cnblogs.com/tk-del/archive/2013/05/10/3071541.html function Post(AURL: string; ASource: T ...

随机推荐

  1. javascript在html中的加载顺序

    参考:[1]http://coolshell.cn/articles/9749.html(酷壳) [2]http://shaozhuqing.com/?p=2756 [3]http://www.cnb ...

  2. Repeater 根据某一列的值加颜色

    //排队中 protected void rptOrdersList_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterIt ...

  3. 如何得到自定义UITableViewCell中的按钮所在的cell的indexPath.row

    在自定义UITableViewCell中创建了一个按钮. 想在点击该按钮时知道该按钮所在的cell在TableView中的行数.就是cell的 indexPath.row两种方法都很好.-(IBAct ...

  4. android 编译

    编译 Android完全编译,耗时 1 小时 25 分$ make编译当前目录下的模块,耗时 1 小时 31 分mm编译指定目录下的模块mmm 模块的根目录清除上次编译输出make clean单独编译 ...

  5. ie 8 下post提交提交了两次

    擦你吗呀,IE8! 老子写一个登录功能,IE他妈的给我登录了两次,导致权限校验错误,什么他妈的鬼问题,调了两天....fuck,都是泪水. 解决方案:提交按钮加返回值<input type=&q ...

  6. 文件操作 模式r+与w+

    r+与w+ r+是读写模式,在文件的末尾进行追加操作. >>> myfile=open('pwd.txt', ... 'r+') >>> myfile.read() ...

  7. sublime插件使用整理

    考虑到后续要有更多的时间来写js,周末好好的把sublime整理了下,很多插件非常好.下面一一来说 1.  注释生成插件 DocBlockr 之前每次写函数的注释都要复制其他函数的注释,然后在写,非常 ...

  8. Linux监控分析实战-1

    监控概述及5个大指标 cpu mem   内存 io        磁盘交互 load     负载 Network 网络 它们之间关系是相互彼此依赖,任何一个高负载都会到导致其他指标出现问题: 网卡 ...

  9. docker 私有镜像管理工具harbor 安装

    因为各种原因,官方的离线安装包下载比较费事,经常不成功,所以通过分部安装解决问题 1. docker yum install libdevmapper* -y -H tcp://0.0.0.0:237 ...

  10. mybatis与 Exception

    mybatis将所有的异常全部包成了运行时异常,减少在高层代码中频繁的try-catch导致的代码臃肿问题.Persistence是它们共有的父类,继承自RuntimeException非检查型异常. ...