delphi idhttp 实战用法(TIdhttpEx)

时间:2021-07-23 06:09:13

以delphi XE8 自带indy(10.5.8.0)组件为例,分享实战中遇到的问题及解决方法。

TIdHttpEx 用法实例01[多线程获取网页](包含完整源码)

实例02(如何Post参数,如何保存与提取Cookie)待写

TIdHttpEx 已实现了对GZIP的解压,对UTF-8编码解码等

本文包含以下几个单元

uIdhttp.pas (TIdHttpEx)

uIdCookieMgr.pas (TIdCookieMgr)

uOperateIndy.pas 操作 TIdhttpEx 全靠它了

uIdhttp.Pas


复制代码
1 unit uIdHttpEx;
2
3 interface
4
5 uses
6 Classes, Idhttp, uIdCookieMgr, IdSSLOpenSSL;
7 {uIdCookieMgr 是我改进的}
8
9 type
10
11 TIdhttpEx = class(TIdhttp)
12 private
13 FIdCookieMgr: TIdCookieMgr;
14 FIdSSL: TIdSSLIOHandlerSocketOpenSSL;
15 public
16 constructor Create(AOwner: TComponent);
17 property CookieMgr: TIdCookieMgr read FIdCookieMgr;
18 procedure GenRandomUserAgent; //随便生成一个请求头,可以忽略或自己改进
19 property IdSSL: TIdSSLIOHandlerSocketOpenSSL read FIdSSL;
20
21 end;
22
23 implementation
24
25 { TIdhttpEx }
26
27 const
28
29 sUserAgent =
30 ‘Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 2.0.50727)‘;
31 // sAccept = ‘image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, */*‘;
32 sUserAgent2 =
33 ‘Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; InfoPath.3; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)‘;
34 sAccept = ‘application/x-shockwave-flash, image/gif, image/jpeg, image/pjpeg, application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, */*‘;
35
36 sUserAgent3 =
37 ‘Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.65 Safari/537.36‘;
38 sAccept2 = ‘text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8‘;
39
40 MaxUserAgentCount = 3;
41
42 var
43 UserAgent: array [0 .. MaxUserAgentCount - 1] of string;
44
45 constructor TIdhttpEx.Create(AOwner: TComponent);
46 begin
47 inherited;
48
49 HTTPOptions := []; // 禁止POST参数编码,自己手动编 HttpEncodeX
50
51 // HTTPOptions := [hoNoParseMetaHTTPEquiv]; // 禁止POST参数编码,自己手动编 HttpEncodeX
52 // hoNoParseMetaHTTPEquiv 禁止解析html 此可能造成假死!
53
54 FIdCookieMgr := TIdCookieMgr.Create(self);
55 CookieManager := FIdCookieMgr;
56
57 // ssl 需要 libeay32.dll ssleay32.dll 阿里旺旺目录下可以搜索到
58
59 FIdSSL := TIdSSLIOHandlerSocketOpenSSL.Create(self);
60 IOHandler := FIdSSL;
61
62 HandleRedirects := true;
63 AllowCookies := true;
64 ProtocolVersion := pv1_1;
65
66 Request.RawHeaders.FoldLength := 25000; // 参数头长度,重要
67
68 ReadTimeout := 15000;
69 ConnectTimeout := 15000;
70
71 RedirectMaximum := 5;
72 Request.UserAgent := sUserAgent3;
73 Request.Accept := sAccept;
74 Request.AcceptEncoding := ‘gzip‘;
75
76 end;
77
78 procedure TIdhttpEx.GenRandomUserAgent;
79 begin
80 Randomize;
81 self.Request.UserAgent := UserAgent[Random(MaxUserAgentCount)];
82 end;
83
84 initialization
85
86 UserAgent[0] :=
87 ‘Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 2.0.50727)‘;
88 UserAgent[1] :=
89 ‘Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; InfoPath.3; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)‘;
90 UserAgent[2] :=
91 ‘Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.65 Safari/537.36‘;
92
93 // 这三句请忽略,有些网站认求头,我随便写的。请大家根本实际情况改进
94 finalization
95
96 end.
复制代码
uIdCookieMgr.Pas


复制代码
1 unit uIdCookieMgr;
2
3 interface
4
5 uses
6 IdCookieManager, Classes;
7
8 type
9 TIdCookieMgr = class(TIdCookieManager)
10 private
11
12 procedure SetCurCookies(const Value: string);
13
14 function GetCurCookies: string;
15 function GetCookieList: TStringList;
16
17 public
18
19 procedure SaveCookies(const AFileName: string);
20 procedure LoadCookies(const AFileName: string);
21
22 function GetCookieValue(const ACookieName: string): string;
23 property CurCookies: string read GetCurCookies write SetCurCookies;
24
25 end;
26
27 implementation
28
29 uses
30 IdCookie, SysUtils, IdURI, uStrUtils, IdGlobalProtocols, DateUtils;
31 { uStrUtils 一套操作字串的函数单元 }
32
33 function TIdCookieMgr.GetCookieList: TStringList;
34 var
35 C: Tcollectionitem;
36 begin
37 result := TStringList.Create;
38 for C in CookieCollection do
39 result.add((C as TIdCookie).CookieText);
40 end;
41
42 function TIdCookieMgr.GetCookieValue(const ACookieName: string): string;
43 var
44 n: integer;
45 begin
46 result := ‘‘;
47 if IsNotEmptyStr(ACookieName) then
48 begin
49 n := CookieCollection.GetCookieIndex(ACookieName);
50 if n >= 0 then
51 result := CookieCollection.Cookies[n].Value;
52 end;
53 end;
54
55 function TIdCookieMgr.GetCurCookies: string;
56 var
57 strs: TStringList;
58 begin
59 strs := GetCookieList;
60 try
61 result := strs.Text;
62 finally
63 strs.Free;
64 end;
65 end;
66
67 procedure TIdCookieMgr.LoadCookies(const AFileName: string);
68 var
69 StrLst: TStringList;
70 C: TIdCookie;
71 uri: TIdURI;
72 s, t: string;
73 begin
74 StrLst := TStringList.Create;
75 uri := TIdURI.Create;
76 try
77 if FileExists(AFileName) then
78 begin
79 StrLst.LoadFromFile(AFileName);
80 for s in StrLst do
81 begin
82 C := CookieCollection.add;
83 CookieCollection.AddCookie(C, uri);
84 C.ParseServerCookie(s, uri);
85 C.Domain := GetStrBetween(s, ‘Domain=‘, ‘;‘);
86 C.Path := GetStrBetween(s, ‘Path=‘, ‘;‘);
87 t := GetStrBetween(s, ‘Expires=‘, ‘GMT‘) + ‘GMT‘; // GetStrBetween 在 uStrUtils 单元中
88 C.Expires := CookieStrToLocalDateTime(t);
89 end;
90 end;
91 finally
92 uri.Free;
93 StrLst.Free;
94 end;
95 end;
96
97 procedure TIdCookieMgr.SaveCookies(const AFileName: string);
98 var
99 StrLst: TStringList;
100 begin
101 StrLst := GetCookieList;
102 try
103 StrLst.SaveToFile(AFileName);
104 finally
105 StrLst.Free;
106 end;
107 end;
108
109 procedure TIdCookieMgr.SetCurCookies(const Value: string);
110 var
111 StrLst: TStringList;
112 C: TIdCookie;
113 uri: TIdURI;
114 s, t: string;
115 begin
116 StrLst := TStringList.Create;
117 uri := TIdURI.Create;
118 try
119 StrLst.Text := Value;
120 CookieCollection.Clear;
121 for s in StrLst do
122 begin
123 C := CookieCollection.add;
124 CookieCollection.AddCookie(C, uri);
125 C.ParseServerCookie(s, uri);
126 C.Domain := GetStrBetween(s, ‘Domain=‘, ‘;‘);
127 C.Path := GetStrBetween(s, ‘Path=‘, ‘;‘);
128 t := GetStrBetween(s, ‘Expires=‘, ‘GMT‘) + ‘GMT‘;
129 C.Expires := CookieStrToLocalDateTime(t);
130 end;
131 finally
132 uri.Free;
133 StrLst.Free;
134 end;
135 end;
136
137 end.
复制代码
uOperateIndy.pas 非常有用操作 TIdhttpEx 全靠它了