以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.
uIdhttpEx.pas

相关文章:

  • 2021-11-30
  • 2022-12-23
  • 2022-12-23
  • 2021-11-26
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案