加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Delphi IDHTTP用法详解

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读