Delphi使用Indy、ICS组件读取网页
使用Indy 10中TIdHTTP的例子:
代码 uses
IdHttp;
.
.
.
function HttpGet(const Url: string; var Html: string): Boolean;
var
HttpClient: TIdHTTP;
begin
Result := False;
HttpClient := TIdHTTP.Create(nil);
try
Html := HttpClient.Get(Url);
Result := True;
except
on e: Exception do
begin
end;
end;
HttpClient.Free;
end;
Indy采用的是同步I/O的方式,而且在连接超时控制方面存在bug,因此TIdHttp.Get()有时会发生陷入死锁无法返回的问题。
使用ICS中THttpCli的例子:
uses
HttpProt;
.
.
.
function HttpGet(const Url: string; var Html: string): Boolean;
var
HttpClient: THttpCli;
DataLen: Int64;
FailMsg: string;
begin
Result := False;
HttpClient := THttpCli.Create(nil);
HttpClient.URL := Url;
HttpClient.NoCache := True;
HttpClient.RcvdStream := TMemoryStream.Create;
try
try
HttpClient.Get;
DataLen := HttpClient.RcvdStream.Size;
SetLength(Html, DataLen);
HttpClient.RcvdStream.Position := ;
HttpClient.RcvdStream.Read(PChar(Html)^, DataLen);
Result := True;
except
on E: EHttpException do
begin
FailMsg := Format('Failed : %d %s',
[HttpClient.StatusCode, HttpClient.ReasonPhrase]);
end else
raise;
end;
finally
HttpClient.RcvdStream.Free;
HttpClient.RcvdStream := nil;
HttpClient.Free;
end;
end;
ICS使用的是异步I/O,其TFtpClient组件有Timout属性可以对连接超时进行控制,而THttpCli组件没有。但可以采用在定时器中调用THttpCli.Abort()取消连接的方式控制超时,也可以显式调用异步方法:
var
HttpClient: THttpCli;
DataLen: Int64;
FailMsg: string;
Tick: Cardinal;
begin
Result := False;
HttpClient := THttpCli.Create(nil);
HttpClient.URL := Url;
HttpClient.NoCache := True;
HttpClient.RcvdStream := TMemoryStream.Create;
Tick := GetTickCount;
try
try
HttpClient.GetASync;
while HttpClient.State <> httpReady do //检测HTTP状态
begin
if GetTickCount - Tick > * then //此处设置了30S超时,可根据需要修改此数值
begin
HttpClient.Abort;
Exit;
end;
Application.ProcessMessages;
end;
DataLen := HttpClient.RcvdStream.Size;
SetLength(Html, DataLen);
HttpClient.RcvdStream.Position := ;
HttpClient.RcvdStream.Read(PChar(Html)^, DataLen);
Result := True;
except
on E: EHttpException do
begin
FailMsg := Format('Failed : %d %s',
[HttpClient.StatusCode, HttpClient.ReasonPhrase]);
end else
raise;
end;
finally
HttpClient.RcvdStream.Free;
HttpClient.RcvdStream := nil;
HttpClient.Free;
end;
end
Delphi使用Indy、ICS组件读取网页的更多相关文章
- php 读取网页源码 , 导出成txt文件, 读取xls,读取文件夹下的所有文件的文件名
<?php // 读取网页源码$curl = curl_init();curl_setopt($curl, CURLOPT_URL, $url);curl_setopt($curl, CURLO ...
- HttpWebRequest后台读取网页类
using System;using System.Linq;using System.Collections.Generic;using System.Web;using System.Config ...
- C#读取网页源码
#region 1.读取 网页源码 + static string ReadHtml(string urlStr,int type) /// <summary> /// 读取 网页源码 + ...
- 10-利用com组件读取office
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...
- Delphi中使用python脚本读取Excel数据
Delphi中使用python脚本读取Excel数据2007-10-18 17:28:22标签:Delphi Excel python原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 . ...
- java读取网页图片路径并下载到本地
java读取网页图片路径并下载到本地 最近公司需要爬取一些网页上的数据,自己就简单的写了一个demo,其中有一些数据是图片,需要下载下来到本地并且 将图片的路径保存到数据库,示例代码如下: packa ...
- 利用backgroundwork----递归读取网页源代码,并下载href链接中的文件
今天闲着没事,研究了一下在线更新程序版本的问题.也是工作中的需要,开始不知道如何下手,各种百度也没有找到自己想要的,因为我的需求比较简单,所以就自己琢磨了一下.讲讲我的需求吧.自己在IIs上发布了一个 ...
- 组件化网页开发 / 步骤一 · 4-4 匹配HTML标签
组件化网页开发 / 步骤一 · 4-4 匹配HTML标签
- 熟练使用jquery ui / easyui的各种组件设计网页界面
熟练使用jquery ui / easyui的各种组件设计网页界面
随机推荐
- SQL中去掉换行符 与空格符
SELECT B.TradeOrderID AS '二段交易号',B.ZipCode AS '邮编', B.Province AS '省',B.City AS '市',B.District AS '区 ...
- NumPy矩阵库
NumPy - 矩阵库 NumPy 包包含一个 Matrix库numpy.matlib.此模块的函数返回矩阵而不是返回ndarray对象. matlib.empty() matlib.empty()函 ...
- 十二道MR习题 – 1 – 排序
题目: 一个文件,大小约为100G.文件的每一行都是一个数字,要求对文件中的所有数字进行排序. 对于这个题目,了解过Hadoop的同学可以笑而不语了.即使用spark实现也是非常简单的事情. 先说下如 ...
- Jni_Linux_01_转
1.Linux下JNI的使用(http://www.cnblogs.com/bastard/archive/2012/05/17/2506877.html) Linux下 JNI的使用 学习Andro ...
- BusyIndicator using MVVM 忙碌状态指示器的的实现
ViewModel 视图模型 public abstract class ViewModelBase : INotifyPropertyChanged { private bool isbusy; p ...
- .net操作Oracle数据库步骤及方法
1.首先安装PL/SQL Developer Oracle客户端软件 2.安装Oracle Instant Client(即时客户端) 安装与配置 配置环境变量ORAClE HOME 地址为insta ...
- day5-hashlib模块
一.概述 在程序开发过程中,很多时候会涉及用户信息验证环节,这类场景下我们往往需要对字符串进行加密处理.python中也有专门的加密模块,它就是hashlib.下面章节将详述它的常见用法. 二.常见加 ...
- JS兼容性汇总
1. Frame (1)问题:在IE中可以用window.top.frameId和window.top.frameName来得到该Frame所代表的Window,Firefox中只能用window ...
- jdbc之防sql注入攻击
1.SQL注入攻击: 由于dao中执行的SQL语句是拼接出来的,其中有一部分内容是由用户从客户端传入,所以当用户传入的数据中包含sql关键字时,就有可能通过这些关键字改变sql语句的语义,从而执 ...
- mcake活动维护常见问题记录【pc端】 ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★
★ ★ ★ ★ ★ ★ ★ ★ ★ ★pc端问题及解决方法 ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ 问题一.pc.弹窗,背景兼容ie8的写法 ;;; -moz-opacity:.7;f ...