一、IDHTTP的基本用法 

  1. IDHttp和WebBrowser一样,都可以实现抓取远端网页的功能,但是http方式更快、更节约资源,缺点是需要手动维护cook,连接等 
  2. IDHttp的创建,需要引入IDHttp 
  3. procedure InitHttp(); 
  4. begin
  5.     http := TIdHTTP.Create(nil); 
  6.     http.ReadTimeout := 30000; 
  7.     http.OnRedirect := OnRedirect; 
  8.     http.Request.Accept := 'image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, */*'; 
  9.     http.Request.AcceptLanguage := 'zh-cn'; 
  10.     http.Request.ContentType := 'application/x-www-form-urlencoded'; 
  11.     http.Request.UserAgent := 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 1.1.4322)'; 
  12.     http.ProxyParams.ProxyServer := '代理服务器地址'; 
  13.     http.ProxyParams.ProxyPort := '代理服务器端口'; 
  14. end; 
  15. 二、如何取得服务端返回的cookie信息,并添加到http的request对象中 
  16. procedure Setcookie; 
  17. var
  18.    i: Integer; 
  19.    tmp, cookie: String; 
  20. begin
  21.    cookie := ''; 
  22. for i := 0 to http.Response.RawHeaders.Count - 1 do
  23. begin
  24.        tmp := http.Response.RawHeaders[i]; 
  25. if pos('set-cookie: ', LowerCase(tmp)) = 0 then Continue; 
  26.     tmp := Trim(Copy(tmp, Pos('Set-cookie: ', tmp) + Length('Set-cookie: '), Length(tmp))); 
  27.     tmp := Trim(Copy(tmp, 0, Pos(';', tmp) - 1)); 
  28. if cookie = '' then cookie := tmp else cookie := cookie + '; ' + tmp; 
  29. end; 
  30. if cookie <> '' then
  31. begin
  32. for i := 0 to http.Request.RawHeaders.Count - 1 do
  33. begin
  34.       tmp := http.Request.RawHeaders[i]; 
  35. if Pos('cookie', LowerCase(tmp)) = 0 then Continue; 
  36.       http.Request.RawHeaders.Delete(i); 
  37.       Break; 
  38. end; 
  39.     http.Request.RawHeaders.Add('cookie: ' + cookie); 
  40. end; 
  41. end; 
  42. 三、如何取得网页中的所有连接,对代码做修改你也可以实现查找所有图片等等 
  43. function GetURLList(Data: String): TStringList; 
  44. var
  45. i: Integer; 
  46. List: TStringList; 
  47. tmp: String; 
  48. function Split(Data, Node: String): TStringList; 
  49. var
  50.     Count, i, j: Integer; 
  51. function GetFieldCount(Data, Node: String): Integer; 
  52. var
  53.          i: Integer; 
  54. begin
  55.           Result := -1; 
  56.           i := Pos(Node, Data); 
  57. if i = 0 then Exit; 
  58.              Result := 0; 
  59. while i <> 0 do
  60. begin
  61.             Inc(Result); 
  62.              Delete(Data, 1, i + Length(Node) - 1); 
  63.             i := Pos(Node, Data); 
  64. end; 
  65. end; 
  66. begin
  67.       Result := TStringList.Create; 
  68. Count := GetFieldCount(Data, Node); 
  69. for i := 0 to Count - 1 do
  70. begin
  71.      j := Pos(Node, Data); 
  72.      Result.Add(Copy(Data, 1, j - 1)); 
  73.      Delete(Data, 1, j + Length(Node) - 1); 
  74. end; 
  75. Result.Add(Data); 
  76. end; 
  77. begin
  78. Result := TStringList.Create; 
  79. try
  80.     List := split(Data, 'href='); 
  81. for i := 1 to List.Count - 1 do
  82. begin
  83.       tmp := List[i]; 
  84.        tmp := Copy(tmp, 0, Pos('</a>', tmp) - 1); 
  85.        tmp := Copy(tmp, 0, Pos('>', tmp) - 1); 
  86. if Pos(' ', tmp) <> 0 then
  87.           tmp := Copy(tmp, 0, Pos(' ', tmp) - 1); 
  88.        tmp := Q_ReplaceStr(tmp, Char(34), ''); 
  89.      tmp := Q_ReplaceStr(tmp, Char(39), ''); 
  90. if not Compare(CI.Key, tmp) then Continue; 
  91. if Copy(tmp, 1, 7) <> 'http://' then
  92. begin
  93. if Copy(tmp, 1, 1) = '.' then tmp := StringReplace(tmp, '.', '', []); 
  94. if Copy(tmp, 1, 1) = '.' then tmp := StringReplace(tmp, '.', '', []); 
  95. try
  96.          tmp := 'http://' + http.URL.Host + ':' + http.URL.Port + http.URL.Path + tmp; 
  97. except
  98. end; 
  99. end; 
  100. if Result.IndexOf(tmp) <> -1 then Continue; 
  101.           Result.Add(tmp); 
  102. end; 
  103.    FreeAndNil(List); 
  104. except
  105. end; 
  106. end; 
  107. 四、如何模拟http的get方法打开一个网页 
  108. function GetMethod(http: TIDhttp; URL: String; Max: Integer): String; 
  109. var
  110. RespData: TStringStream; 
  111. begin
  112. RespData := TStringStream.Create(''); 
  113. try
  114. try
  115.       Http.Get(URL, RespData); 
  116.       Http.Request.Referer := URL; 
  117.       Result := RespData.DataString; 
  118. except
  119.       Dec(Max); 
  120. if Max = 0 then
  121. begin
  122.         Result := ''; 
  123.         Exit; 
  124. end; 
  125.       Result := GetMethod(http, URL, Max); 
  126. end; 
  127. finally
  128.     FreeAndNil(RespData); 
  129. end; 
  130. end; 
  131. 五、如何模拟http的post方法提交一个网页 
  132. function PostMethod(URL, Data: String; max: Integer): String; 
  133. var
  134. PostData, RespData: TStringStream; 
  135. begin
  136. RespData := TStringStream.Create(''); 
  137. PostData := TStringStream.Create(Data); 
  138. try
  139. try
  140. if http = nil then Exit; 
  141.       Http.Post(URL, PostData, RespData); 
  142.       Result := RespData.DataString; 
  143.       http.Request.Referer := URL; 
  144. except
  145.       Dec(Max); 
  146. if Max = 0 then
  147. begin
  148.         Result := ''; 
  149.         Exit; 
  150. end; 
  151.       Result := PostMethod(URL, Data, Max); 
  152. end; 
  153. finally
  154.     http.Disconnect; 
  155.     FreeAndNil(RespData); 
  156.     FreeAndNil(PostData); 
  157. end; 
  158. end; 
  159. 六、伪造session 
  160. var
  161. My_Cookie,tmpcookie:string; 
  162. begin
  163. aIdHttp.Get('http://www.huochepiao.net/'); 
  164. tmpcookie:=aIdHttp.Request.CustomHeaders.Values['Set-Cookie']; 
  165. if Pos(';',tmpcookie)>0 then
  166.      My_Cookie:=LeftBStr(tmpcookie,Pos(';',tmpcookie)-1) 
  167. else
  168.      My_Cookie:= tmpcookie; 
  169. //
  170. aIdHTTP.Request.CustomHeaders.Clear; 
  171. aIdHTTP.Request.CustomHeaders.Add('Cookie:'+My_COOKIE); 
  172. end; 

Delphi IDHTTP用法详解的更多相关文章

  1. Delphi IDHTTP用法详解(六种用法)

    一.IDHTTP的基本用法 IDHttp和WebBrowser一样,都可以实现抓取远端网页的功能,但是http方式更快.更节约资源,缺点是需要手动维护cook,连接等 IDHttp的创建,需要引入ID ...

  2. Delphi TStringHelper用法详解

    Delphi TStringHelper用法详解 (2013-08-27 22:45:42) 转载▼ 标签: delphi_xe5 it 分类: Delphi Delphi XE4的TStringHe ...

  3. delphi TStringList 用法详解

    转自: http://blog.163.com/you888@188/blog/static/67239619201472365642633/ delphi TStringList 用法详解 2014 ...

  4. IDHTTP用法详解 good

    一.IDHTTP的基本用法 IDHttp和WebBrowser一样,都可以实现抓取远端网页的功能,但是http方式更快.更节约资源,缺点是需要手动维护cook,连接等 IDHttp的创建,需要引入ID ...

  5. 教程-Delphi中Spcomm使用属性及用法详解

    Delphi中Spcomm使用属性及用法详解 Delphi是一种具有 功能强大.简便易用和代码执行速度快等优点的可视化快速应用开发工具,它在构架企业信息系统方面发挥着越来越重要的作用,许多程序员愿意选 ...

  6. delphi中Application.MessageBox函数用法详解

    delphi中Application.MessageBox函数用法详解 Application.MessageBox是TApplication的成员函数,声明如下:functionTApplicati ...

  7. Delphi XE4 TStringHelper用法详解

    原文地址:Delphi XE4 TStringHelper用法详解作者:天下为公 Delphi XE4的TStringHelper,对操作字符串进一步带来更多的方法,估计XE5还能继续用到. Syst ...

  8. Delphi Format函数功能及用法详解

    DELPHI中Format函数功能及用法详解 DELPHI中Format函数功能及用法详解function Format(const Format: string; const Args: array ...

  9. C#中string.format用法详解

    C#中string.format用法详解 本文实例总结了C#中string.format用法.分享给大家供大家参考.具体分析如下: String.Format 方法的几种定义: String.Form ...

随机推荐

  1. 浅谈js中的垃圾两种回收机制

    一.标记清除 标记清除的主要思想是先建立各个对象的关联,然后从根节点出发,使用广度优先搜索依次标记所有对象,那些不能被标记的对象就应该作为垃圾回收. 这种方式的主要缺点就是如果某些对象被清理后,内存是 ...

  2. unity批量设置图片为etc2格式或者astc格式

    网上找了半天,没一个能用的,干脆自己写个,直接拷贝这个脚本就行 这个是ios版本的,安卓的话写在注释里面,去掉注释就能用了 现在ios支持一种新格式叫astc比原本的pvrtc压缩比更高,而且质量更高 ...

  3. PTA (Advanced Level) 1010 Radix

    Radix Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? ...

  4. JAVA练手--异常

    1. 基本的 public static void main(String[] args) { //1. try catch基本用法 { try{ int[] intA = new int[2]; i ...

  5. 手把手教你使用 VuePress 搭建个人博客

    手把手教你使用 VuePress 搭建个人博客 有阅读障碍的同学,可以跳过第一至四节,下载我写好的工具包: git clone https://github.com/zhangyunchencc/vu ...

  6. js实现点击图片,然后图片放大

    HTML <td width="350"> <img height="100" width="100" class=&qu ...

  7. C# 之StringBulider简单用法

    StringBuild的是个动态对象,可直接拼加上字符串:而string对象的步骤:先初始化对象并赋值了,而后在拼加字符串时,先要创建需要拼加的字符串,然后再拼加,所以这就是StirngBuild远比 ...

  8. 远程连接MongoDB数据库

    不使用用户名和密码 安装MongoDB后,默认不使用用户名和密码即可在本地登录,如需远程登录,只要修改/bin/mongo.conf文件即可

  9. 出现<authentication mode="Windows"/>错误解决办法

    转自:https://blog.csdn.net/clever101/article/details/39671715 网上下载的asp.net源码出现 <authentication mode ...

  10. 记一次吐血的暴力模拟qaq 【多项式输出】

    题目描述 一元 n 次多项式可用如下的表达式表示: 其中,aixi称为 i 次项,ai 称为 i 次项的系数.给出一个一元多项式各项的次数和系数,请按照如下规定的格式要求输出该多项式: 1. 多项式中 ...