Delphi IDHTTP用法详解
一、IDHTTP的基本用法
- IDHttp和WebBrowser一样,都可以实现抓取远端网页的功能,但是http方式更快、更节约资源,缺点是需要手动维护cook,连接等
- IDHttp的创建,需要引入IDHttp
- procedure InitHttp();
- begin
- http := TIdHTTP.Create(nil);
- http.ReadTimeout := 30000;
- http.OnRedirect := OnRedirect;
- http.Request.Accept := 'image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, */*';
- http.Request.AcceptLanguage := 'zh-cn';
- http.Request.ContentType := 'application/x-www-form-urlencoded';
- http.Request.UserAgent := 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 1.1.4322)';
- http.ProxyParams.ProxyServer := '代理服务器地址';
- http.ProxyParams.ProxyPort := '代理服务器端口';
- end;
- 二、如何取得服务端返回的cookie信息,并添加到http的request对象中
- procedure Setcookie;
- var
- i: Integer;
- tmp, cookie: String;
- begin
- cookie := '';
- for i := 0 to http.Response.RawHeaders.Count - 1 do
- begin
- tmp := http.Response.RawHeaders[i];
- if pos('set-cookie: ', LowerCase(tmp)) = 0 then Continue;
- tmp := Trim(Copy(tmp, Pos('Set-cookie: ', tmp) + Length('Set-cookie: '), Length(tmp)));
- tmp := Trim(Copy(tmp, 0, Pos(';', tmp) - 1));
- if cookie = '' then cookie := tmp else cookie := cookie + '; ' + tmp;
- end;
- if cookie <> '' then
- begin
- for i := 0 to http.Request.RawHeaders.Count - 1 do
- begin
- tmp := http.Request.RawHeaders[i];
- if Pos('cookie', LowerCase(tmp)) = 0 then Continue;
- http.Request.RawHeaders.Delete(i);
- Break;
- end;
- http.Request.RawHeaders.Add('cookie: ' + cookie);
- end;
- end;
- 三、如何取得网页中的所有连接,对代码做修改你也可以实现查找所有图片等等
- function GetURLList(Data: String): TStringList;
- var
- i: Integer;
- List: TStringList;
- tmp: String;
- function Split(Data, Node: String): TStringList;
- var
- Count, i, j: Integer;
- function GetFieldCount(Data, Node: String): Integer;
- var
- i: Integer;
- begin
- Result := -1;
- i := Pos(Node, Data);
- if i = 0 then Exit;
- Result := 0;
- while i <> 0 do
- begin
- Inc(Result);
- Delete(Data, 1, i + Length(Node) - 1);
- i := Pos(Node, Data);
- end;
- end;
- begin
- Result := TStringList.Create;
- Count := GetFieldCount(Data, Node);
- for i := 0 to Count - 1 do
- begin
- j := Pos(Node, Data);
- Result.Add(Copy(Data, 1, j - 1));
- Delete(Data, 1, j + Length(Node) - 1);
- end;
- Result.Add(Data);
- end;
- begin
- Result := TStringList.Create;
- try
- List := split(Data, 'href=');
- for i := 1 to List.Count - 1 do
- begin
- tmp := List[i];
- tmp := Copy(tmp, 0, Pos('</a>', tmp) - 1);
- tmp := Copy(tmp, 0, Pos('>', tmp) - 1);
- if Pos(' ', tmp) <> 0 then
- tmp := Copy(tmp, 0, Pos(' ', tmp) - 1);
- tmp := Q_ReplaceStr(tmp, Char(34), '');
- tmp := Q_ReplaceStr(tmp, Char(39), '');
- if not Compare(CI.Key, tmp) then Continue;
- if Copy(tmp, 1, 7) <> 'http://' then
- begin
- if Copy(tmp, 1, 1) = '.' then tmp := StringReplace(tmp, '.', '', []);
- if Copy(tmp, 1, 1) = '.' then tmp := StringReplace(tmp, '.', '', []);
- try
- tmp := 'http://' + http.URL.Host + ':' + http.URL.Port + http.URL.Path + tmp;
- except
- end;
- end;
- if Result.IndexOf(tmp) <> -1 then Continue;
- Result.Add(tmp);
- end;
- FreeAndNil(List);
- except
- end;
- end;
- 四、如何模拟http的get方法打开一个网页
- function GetMethod(http: TIDhttp; URL: String; Max: Integer): String;
- var
- RespData: TStringStream;
- begin
- RespData := TStringStream.Create('');
- try
- try
- Http.Get(URL, RespData);
- Http.Request.Referer := URL;
- Result := RespData.DataString;
- except
- Dec(Max);
- if Max = 0 then
- begin
- Result := '';
- Exit;
- end;
- Result := GetMethod(http, URL, Max);
- end;
- finally
- FreeAndNil(RespData);
- end;
- end;
- 五、如何模拟http的post方法提交一个网页
- function PostMethod(URL, Data: String; max: Integer): String;
- var
- PostData, RespData: TStringStream;
- begin
- RespData := TStringStream.Create('');
- PostData := TStringStream.Create(Data);
- try
- try
- if http = nil then Exit;
- Http.Post(URL, PostData, RespData);
- Result := RespData.DataString;
- http.Request.Referer := URL;
- except
- Dec(Max);
- if Max = 0 then
- begin
- Result := '';
- Exit;
- end;
- Result := PostMethod(URL, Data, Max);
- end;
- finally
- http.Disconnect;
- FreeAndNil(RespData);
- FreeAndNil(PostData);
- end;
- end;
- 六、伪造session
- var
- My_Cookie,tmpcookie:string;
- begin
- aIdHttp.Get('http://www.huochepiao.net/');
- tmpcookie:=aIdHttp.Request.CustomHeaders.Values['Set-Cookie'];
- if Pos(';',tmpcookie)>0 then
- My_Cookie:=LeftBStr(tmpcookie,Pos(';',tmpcookie)-1)
- else
- My_Cookie:= tmpcookie;
- //
- aIdHTTP.Request.CustomHeaders.Clear;
- aIdHTTP.Request.CustomHeaders.Add('Cookie:'+My_COOKIE);
- end;
Delphi IDHTTP用法详解的更多相关文章
- Delphi IDHTTP用法详解(六种用法)
一.IDHTTP的基本用法 IDHttp和WebBrowser一样,都可以实现抓取远端网页的功能,但是http方式更快.更节约资源,缺点是需要手动维护cook,连接等 IDHttp的创建,需要引入ID ...
- Delphi TStringHelper用法详解
Delphi TStringHelper用法详解 (2013-08-27 22:45:42) 转载▼ 标签: delphi_xe5 it 分类: Delphi Delphi XE4的TStringHe ...
- delphi TStringList 用法详解
转自: http://blog.163.com/you888@188/blog/static/67239619201472365642633/ delphi TStringList 用法详解 2014 ...
- IDHTTP用法详解 good
一.IDHTTP的基本用法 IDHttp和WebBrowser一样,都可以实现抓取远端网页的功能,但是http方式更快.更节约资源,缺点是需要手动维护cook,连接等 IDHttp的创建,需要引入ID ...
- 教程-Delphi中Spcomm使用属性及用法详解
Delphi中Spcomm使用属性及用法详解 Delphi是一种具有 功能强大.简便易用和代码执行速度快等优点的可视化快速应用开发工具,它在构架企业信息系统方面发挥着越来越重要的作用,许多程序员愿意选 ...
- delphi中Application.MessageBox函数用法详解
delphi中Application.MessageBox函数用法详解 Application.MessageBox是TApplication的成员函数,声明如下:functionTApplicati ...
- Delphi XE4 TStringHelper用法详解
原文地址:Delphi XE4 TStringHelper用法详解作者:天下为公 Delphi XE4的TStringHelper,对操作字符串进一步带来更多的方法,估计XE5还能继续用到. Syst ...
- Delphi Format函数功能及用法详解
DELPHI中Format函数功能及用法详解 DELPHI中Format函数功能及用法详解function Format(const Format: string; const Args: array ...
- C#中string.format用法详解
C#中string.format用法详解 本文实例总结了C#中string.format用法.分享给大家供大家参考.具体分析如下: String.Format 方法的几种定义: String.Form ...
随机推荐
- 【文档】六、Mysql Binlog版本
binlog文件格式有以下几种: v1:用于3.23版本 v3:用于4.0.2到4.1版本 v4:用于5.0及以上版本 v2版本只在4.0.x版本中使用,目前已经不再支持了. 处理binlog的程序必 ...
- ERROR 1064 (42000): You have an error in your SQL syntax;
出现: ERROR 1064 (42000): You have an error in your SQL syntax; 1.SQL语句拼写错误. 具体很简单.慢慢查看 2.使用到了SQL关键字. ...
- AngularJS国际化配置
AngularJS国际化配置 下载angular-translate 下载zip包:https://github.com/angular-translate/bower-angular-transla ...
- linux 将一个服务器上的文件或者文件夹复制到另一台服务器上
使用scp将一个Linux系统中的文件或文件夹复制到另一台Linux服务器上 复制文件或文件夹(目录)命令: 一.复制文件: (1)将本地文件拷贝到远程 scp 文件名 用户名@计算机IP或者计 ...
- java几个经典的算法题目----------查询子串和等于已知数字
给出一个排序好的数组和一个数,求数组中连续元素的和等于所给数的子数组 public class testClockwiseOutput { public static void main(String ...
- memcached 学习笔记 3
适合什么场合 memcached不是万能的,它也不是适用在所有场合. Memcached是“分布式”的内存对象缓存系统,那么就是说,那些不需要“分布”的,不需要共享的,或者干脆规模小到只有一台服务器的 ...
- angular2自学笔记(二)---路由、服务等八大主要构造块
angular的思想:总是把数据访问工作委托给一个支持性服务类. Angular 应用的:用 Angular 扩展语法编写 HTML 模板, 用组件类管理这些模板,用服务添加应用逻辑, 用模块打包发布 ...
- 开源代码ViewPageIndicator的使用
1. 导入Android studio 使用SlidingMenu的方式导入Android studio不行,不知道为何,过会懂了再写上 2. 代码 activity_main.xml <?xm ...
- ie6的设置外边距margin变双倍的问题
子元素避免同时使用float和margin. 如: 需要子元素的margin-bottom:20px时,可以给用父元素设置padding-bottom:20px代替.
- Office 卸载问题(安装包的语言不受系统支持)
本人系统Win7 这个问题搞了一下午.各种网站找解决办法.下载下来的都是一些垃圾软件. Win7以上调成兼容模式运行理论可行. 放上微软的解决方法: * 彻底卸载Office 2003: http:/ ...