delphi的connection closed Gracefully错误

本文介绍了一个用于从指定URL获取网页内容的函数实现,包括处理重定向和超时设置。特别强调了在访问特定网站如百度时,需要修改HTTP头以避免客户端标识被屏蔽的问题。

将这个放到调用页面上....一个处理地址函数..

function GetWebPage(URL: string):string;

var

IDHTTP: TIDHttp;

ss: String;

begin

IDHTTP:= TIDHTTP.Create(nil);

try

try

idhttp.Disconnect;

idhttp.HandleRedirects:= true; //必须支持重定向否则可能出错

idhttp.ReadTimeout:= 30000; //超过这个时间则不再访问

ss:=idhttp.Get(url);

//ss:=UTF8ToAnsi( ss); //UTF8编码转换

if IDHTTP.ResponseCode=200 then

Result := ss;

except

//ss:='';

end;

finally

IDHTTP.Free;

end;

end;

===========================================================

注意:

get百度时,需要修改http头:
User-Agent: Mozilla/3.0 (compatible; Indy Library)
因为百度屏蔽了 ‘Indy Library’ 这个客户端标识的。

21:40:18 ROOT=D:\TinyPXE\files\ 21:40:18 DHCPd 192.168.29.2:67 started... 21:40:18 TFPTd 192.168.29.2:69 started... 21:40:19 HTTPd:80 started... 21:40:24 DHCPd:DISCOVER received, MAC:00-0C-29-06-CA-AA, XID:2A06CAAA 21:40:24 DHCPd:OFFER sent, IP:192.168.29.3, XID:2A06CAAA 21:40:26 DHCPd:REQUEST received, MAC:00-0C-29-06-CA-AA, XID:2A06CAAA 21:40:26 TFTPd:DoReadFile:ipxe.pxe B:1456 T:0 21:40:26 DHCPd:ACK sent, IP:192.168.29.3, XID:2A06CAAA 21:40:30 DHCPd:DISCOVER received, MAC:00-0C-29-06-CA-AA, XID:F0369A28 21:40:30 DHCPd:iPXE user-class detected 21:40:31 DHCPd:OFFER sent, IP:192.168.29.4, XID:F0369A28 21:40:32 DHCPd:DISCOVER received, MAC:00-0C-29-06-CA-AA, XID:F0369A28 21:40:32 DHCPd:iPXE user-class detected 21:40:32 DHCPd:OFFER sent, IP:192.168.29.5, XID:F0369A28 21:40:35 DHCPd:REQUEST received, MAC:00-0C-29-06-CA-AA, XID:F0369A28 21:40:35 DHCPd:iPXE user-class detected 21:40:35 DHCPd:ACK sent, IP:192.168.29.5, XID:F0369A28 21:40:35 TFTPd:DoReadFile:hds_ipxe3.ipxe B:1432 T:5258 21:40:35 HTTPd:Connect: 192.168.29.5, TID=5596 21:40:35 HTTPd:Client: 192.168.29.5 [GET] /hsds_pxe 21:40:35 HTTPd:Server : Returning /hsds_pxe 21:40:43 HTTPd:Exception: , Connection Closed Gracefully. 21:40:43 HTTPd:DisConnect: TID=5596 21:40:47 HTTPd:Connect: 192.168.29.5, TID=2624 21:40:47 HTTPd:Client: 192.168.29.5 [GET] /ubuntu-22.04.5-desktop-amd64/casper/vmlinuz 21:40:47 HTTPd:Server : Returning /ubuntu-22.04.5-desktop-amd64/casper/vmlinuz 21:40:47 HTTPd:Client: 192.168.29.5 [GET] /ubuntu-22.04.5-desktop-amd64/casper/initrd 21:40:47 HTTPd:Server : Returning /ubuntu-22.04.5-desktop-amd64/casper/initrd 21:40:48 HTTPd:Exception: , Connection Closed Gracefully. 21:40:48 HTTPd:DisConnect: TID=2624 21:44:44 DHCPd:DISCOVER received, MAC:00-0C-29-06-CA-AA, XID:2A06CAAA 21:44:44 DHCPd:OFFER sent, IP:192.168.29.3, XID:2A06CAAA 21:44:46 DHCPd:REQUEST received, MAC:00-0C-29-06-CA-AA, XID:2A06CAAA 21:44:46 DHCPd:ACK sent, IP:192.168.29.3, XID:2A06CAAA 21:44:46 TFTPd:DoReadFile:ipxe.pxe B:1456 T:0 21:44:50 DHCPd:DISCOVER received, MAC:00-0C-29-06-CA-AA, XID:681B422B 21:44:50 DHCPd:iPXE user-class detected 21:44:50 DHCPd:OFFER sent, IP:192.168.29.4, XID:681B422B 21:44:52 DHCPd:DISCOVER received, MAC:00-0C-29-06-CA-AA, XID:681B422B 21:44:52 DHCPd:iPXE user-class detected 21:44:52 DHCPd:OFFER sent, IP:192.168.29.5, XID:681B422B 21:44:55 DHCPd:REQUEST received, MAC:00-0C-29-06-CA-AA, XID:681B422B 21:44:55 DHCPd:iPXE user-class detected 21:44:55 DHCPd:ACK sent, IP:192.168.29.5, XID:681B422B 21:44:55 TFTPd:DoReadFile:hds_ipxe3.ipxe B:1432 T:5273 21:44:55 HTTPd:Connect: 192.168.29.5, TID=1552 21:44:55 HTTPd:Client: 192.168.29.5 [GET] /hsds_pxe 21:44:55 HTTPd:Server : Returning /hsds_pxe 21:44:59 HTTPd:Client: 192.168.29.5 [GET] /ubuntu-22.04.5-desktop-amd64/casper/vmlinuz 21:44:59 HTTPd:Server : Returning /ubuntu-22.04.5-desktop-amd64/casper/vmlinuz 21:44:59 HTTPd:Client: 192.168.29.5 [GET] /ubuntu-22.04.5-desktop-amd64/casper/initrd 21:44:59 HTTPd:Server : Returning /ubuntu-22.04.5-desktop-amd64/casper/initrd 21:45:00 HTTPd:Exception: , Connection Closed Gracefully. 21:45:00 HTTPd:DisConnect: TID=1552
最新发布
06-30
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值