Delphi IDHTTP用法详解(六种用法)
一、IDHTTP的基本用法 IDHttp和WebBrowser一样,都可以实现抓取远端网页的功能,但是http方式更快、更节约资源,缺点是需要手动维护cook,连接等 IDHttp的创建,需要引入IDHttp procedure InitHttp();
begin
http := TIdHTTP.Create(nil);
http.ReadTimeout := ;
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 := to http.Response.RawHeaders.Count - do
begin
tmp := http.Response.RawHeaders[i];
if pos('set-cookie: ', LowerCase(tmp)) = then Continue;
tmp := Trim(Copy(tmp, Pos('Set-cookie: ', tmp) + Length('Set-cookie: '), Length(tmp)));
tmp := Trim(Copy(tmp, , Pos(';', tmp) - ));
if cookie = '' then cookie := tmp else cookie := cookie + '; ' + tmp;
end;
if cookie <> '' then
begin
for i := to http.Request.RawHeaders.Count - do
begin
tmp := http.Request.RawHeaders[i];
if Pos('cookie', LowerCase(tmp)) = 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 := -;
i := Pos(Node, Data);
if i = then Exit;
Result := ;
while i <> do
begin
Inc(Result);
Delete(Data, , i + Length(Node) - );
i := Pos(Node, Data);
end;
end;
begin
Result := TStringList.Create;
Count := GetFieldCount(Data, Node);
for i := to Count - do
begin
j := Pos(Node, Data);
Result.Add(Copy(Data, , j - ));
Delete(Data, , j + Length(Node) - );
end;
Result.Add(Data);
end;
begin
Result := TStringList.Create;
try
List := split(Data, 'href=');
for i := to List.Count - do
begin
tmp := List[i];
tmp := Copy(tmp, , Pos('</a>', tmp) - );
tmp := Copy(tmp, , Pos('>', tmp) - );
if Pos(' ', tmp) <> then tmp := Copy(tmp, , Pos(' ', tmp) - );
tmp := Q_ReplaceStr(tmp, Char(), '');
tmp := Q_ReplaceStr(tmp, Char(), '');
if not Compare(CI.Key, tmp) then Continue;
if Copy(tmp, , ) <> 'http://' then
begin
if Copy(tmp, , ) = '.' then tmp := StringReplace(tmp, '.', '', []);
if Copy(tmp, , ) = '.' then tmp := StringReplace(tmp, '.', '', []);
try
tmp := 'http://' + http.URL.Host + ':' + http.URL.Port + http.URL.Path + tmp;
except
end;
end;
if Result.IndexOf(tmp) <> - 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 = 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 = 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)> then
My_Cookie:=LeftBStr(tmpcookie,Pos(';',tmpcookie)-)
else
My_Cookie:= tmpcookie;
//
aIdHTTP.Request.CustomHeaders.Clear;
aIdHTTP.Request.CustomHeaders.Add('Cookie:'+My_COOKIE); end;
http://blog.csdn.net/yanjiaye520/article/details/8199016
Delphi IDHTTP用法详解(六种用法)的更多相关文章
- 结构体定义 typedef struct 用法详解和用法小结
typedef是类型定义的意思.typedef struct 是为了使用这个结构体方便.具体区别在于:若struct node {}这样来定义结构体的话.在申请node 的变量时,需要这样写,stru ...
- 教程-Delphi中Spcomm使用属性及用法详解
Delphi中Spcomm使用属性及用法详解 Delphi是一种具有 功能强大.简便易用和代码执行速度快等优点的可视化快速应用开发工具,它在构架企业信息系统方面发挥着越来越重要的作用,许多程序员愿意选 ...
- delphi中Application.MessageBox函数用法详解
delphi中Application.MessageBox函数用法详解 Application.MessageBox是TApplication的成员函数,声明如下:functionTApplicati ...
- 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 ...
- 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 ...
- @RequestMapping 用法详解之地址映射
@RequestMapping 用法详解之地址映射 引言: 前段时间项目中用到了RESTful模式来开发程序,但是当用POST.PUT模式提交数据时,发现服务器端接受不到提交的数据(服务器端参数绑定没 ...
- linux管道命令grep命令参数及用法详解---附使用案例|grep
功能说明:查找文件里符合条件的字符串. 语 法:grep [-abcEFGhHilLnqrsvVwxy][-A<显示列数>][-B<显示列数>][-C<显示列数>] ...
随机推荐
- ubuntu12.04管理员账户登录不了桌面,只能客人会话登录
ubuntu12.04管理员账户登录不了桌面,只能客人会话登录 求助!!ubuntu12.04管理员账户登录不了桌面,只能客人会话登录. 登录管理员账户时,输入密码后,一直在登录界面循环 费了好大劲啊 ...
- 单元测试利器JUnit4
引言 毋庸置疑,程序员要对自己编写的代码负责,您不仅要保证它能通过编译,正常地运行,而且要满足需求和设计预期的效果.单元测试正是验证代码行为是否满足预期的有效手段之一.但不可否认,做测试是件很枯燥无趣 ...
- [置顶] vb报表的设计
敲机房收费系统,最难的部分应该就是关于报表的部分了.相对于学生信息管理系统,报表是新内容,在vb中添加报表需要添加第三方控件,首先我们要下载水晶报表,下面就向大家展示一下我设计报表的步骤(我用的新版本 ...
- CSS3之渐变Gradient
渐变是CSS3中比较好玩的属性,学会了渐变,那么可以做出非常炫的东东哟.CSS3 中渐变——Gradient分为linear-gradient(线性渐变)和radial-gradient(径向渐变). ...
- Thinkpad Edge E440 Ubuntu14.04 无线网卡驱动 解决
http://ubuntuforums.org/showthread.php?t=2190347 正文: Thinkpad Edge E440 安装 Ubuntu12.04 后 无法使用无线网卡, 须 ...
- js通用对象数组冒牌排序
数组对象通用 function sort(data, sortFiled, orderby) { var result = data, temp; for (var i = 0; i < res ...
- 线段树---HDU1754 I hate it
这个题也是线段树的基础题,有了上一个题的基础,在做这个题就显得比较轻松了,大体都是一样的,那个是求和,这个改成求最大值,基本上思路差不多,下面是代码的实现 #include <cstdio> ...
- swift 类 结构体 作为参数 以及可变参数
Class class Person{ var age = 22, name = "frank" func growolder() { self.age++ //++ 要跟住 不要 ...
- Swift隐式可选型简单介绍
/* 隐式可选型 */ // 隐式可选型同样可以赋值为nil, 而且在后面对这个变量的使用也可以不用进行解包 var value: String! = nil // print(value) 这行代码 ...
- VS专案项目无法生成pdb文件解决办法
在系统盘里找到该项目对应的dll文件(我的是在C:\Windows\Microsoft.NET\assembly\GAC_MSIL目录下),删除该dll后重新生成项目即可.