程序大体结构是这样的:
procedure findbt.Execute ;
var
aidhttp1:tidhttp;
cookies,swznr:string;
s3:widestring;
begin
aidhttp1:=tidhttp.Create(nil);
aidhttp1.HandleRedirects :=true;
aidhttp1.ReadTimeout :=20000;
aidhttp1.Request.UserAgent :='User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727)';
aidhttp1.AllowCookies := true;
while true do
begin
.......
swznr:=_Get(s3,cookies,aidhttp1);
end;end;//显然,这个线程是一直在运行的function findbt._Get(url,scookie : String;idh:tidhttp) : String;
var
stream,outstream : TMemoryStream;
decomp : TIdCompressorZLibex;
resp : TStrings;
begin
//http := TIdHTTP.Create(nil);
idh.Request.CustomHeaders.Text :=scookie;
stream := TMemoryStream.Create;
resp := TStringList.Create;
try
idh.Get(url,stream);
stream.Position := 0;
if AnsiCompareText('gzip',idh.Response.ContentEncoding) = 0 then
begin
decomp := TIdCompressorZLibex.Create(nil);
outstream := TMemoryStream.Create;
try
decomp.DecompressGZipStream(stream,outstream);
outstream.Position := 0;
resp.LoadFromStream(outstream);
finally
decomp.Free;
outstream.Free;
end;
end
else
resp.LoadFromStream(stream);
Result := resp.Text;
finally
stream.Free;
resp.Free;
end;
end;现在的问题是:
这个程序运行是不知道什么时候出现下图错误
目前能确定的是:
1、程序运行出错在IDHTTP,因为错误总停留在IDHTTP单元,且程序停留在IDHTTP单元的FHTTPProto.BuildAndSendRequest(URL)的下一行,应该确定是FHTTPProto.BuildAndSendRequest(URL);这一行出错了。2、貌视这个错误是在IDHTTP的Readtime out之后才会产生。但也不是每次Readtime out之后都会产生,有时产生这个错误,有时不产生这个错误。个人认为:
上图的错误,是IDHTTP被释放或者异常了,再次调用是出现了内存读写错误。实时上我的IDHTTP自创建后没有释放过,那错误到底是怎么样产生的呢?我该如何避免?
procedure findbt.Execute ;
var
aidhttp1:tidhttp;
cookies,swznr:string;
s3:widestring;
begin
aidhttp1:=tidhttp.Create(nil);
aidhttp1.HandleRedirects :=true;
aidhttp1.ReadTimeout :=20000;
aidhttp1.Request.UserAgent :='User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727)';
aidhttp1.AllowCookies := true;
while true do
begin
.......
swznr:=_Get(s3,cookies,aidhttp1);
end;end;//显然,这个线程是一直在运行的function findbt._Get(url,scookie : String;idh:tidhttp) : String;
var
stream,outstream : TMemoryStream;
decomp : TIdCompressorZLibex;
resp : TStrings;
begin
//http := TIdHTTP.Create(nil);
idh.Request.CustomHeaders.Text :=scookie;
stream := TMemoryStream.Create;
resp := TStringList.Create;
try
idh.Get(url,stream);
stream.Position := 0;
if AnsiCompareText('gzip',idh.Response.ContentEncoding) = 0 then
begin
decomp := TIdCompressorZLibex.Create(nil);
outstream := TMemoryStream.Create;
try
decomp.DecompressGZipStream(stream,outstream);
outstream.Position := 0;
resp.LoadFromStream(outstream);
finally
decomp.Free;
outstream.Free;
end;
end
else
resp.LoadFromStream(stream);
Result := resp.Text;
finally
stream.Free;
resp.Free;
end;
end;现在的问题是:
这个程序运行是不知道什么时候出现下图错误
目前能确定的是:
1、程序运行出错在IDHTTP,因为错误总停留在IDHTTP单元,且程序停留在IDHTTP单元的FHTTPProto.BuildAndSendRequest(URL)的下一行,应该确定是FHTTPProto.BuildAndSendRequest(URL);这一行出错了。2、貌视这个错误是在IDHTTP的Readtime out之后才会产生。但也不是每次Readtime out之后都会产生,有时产生这个错误,有时不产生这个错误。个人认为:
上图的错误,是IDHTTP被释放或者异常了,再次调用是出现了内存读写错误。实时上我的IDHTTP自创建后没有释放过,那错误到底是怎么样产生的呢?我该如何避免?
解决方案 »
- ****************关于中英文对照的问题????????????????????
- 告诉大家一个好消息,Delphi已经卖了是DEVCo公司,DEVCo是由borland的开发部门独立出来的公司!
- 请问各位高手自定义消息的ID号 是怎样与消息的数据结构关联在一起的呢
- 跪求:哇哇(痛苦!)adoquery问题!
- 向表中添加记录时出错!在线等……急啊!
- 我是刚学delphi的 我想请教大侠 怎样用delphi做一个模拟键盘的程序呢??
- delphi 与 oracle
- 怎样提高Indy接收邮件的速度?
- 我用ado连数据库。。。。请问在一个没装delphi的机器上运行程序都在做什么??
- 密码的加密
- 程序启动连数据库时如何做一个Splash窗口????
- 导出excel的问题
FHTTPProto.BuildAndSendRequest(URL);
可是这个错误屏蔽不了。
2.个人感觉问题可能出在function findbt._Get(url,scookie : String;idh:tidhttp) : String;函数内部,多输出一些标志量判断;
3.你传入的url参数,是不是都已http开头的,idhttp接收的参数必须以http开头的。感觉应该是function findbt._Get(url,scookie : String;idh:tidhttp) : String;里面的问题。因为一些异常原因,没有考虑充分造成的。
function findbt._Get(url,scookie : String;idh:tidhttp) : String
已在第一帖给出了,看看有什么问题吗?
我现在的问题不是在任何时候都有,貌视只是在READTIME OUT的时候(也不是所有的超时时间都产生这个问题)才会产生这个问题。郁闷啊!
idh.Get(url,stream);
stream.Position := 0;
if AnsiCompareText('gzip',idh.Response.ContentEncoding) = 0 then
begin
decomp := TIdCompressorZLibex.Create(nil);
outstream := TMemoryStream.Create;
try
decomp.DecompressGZipStream(stream,outstream);
outstream.Position := 0;
resp.LoadFromStream(outstream);
finally
decomp.Free;
outstream.Free;
end;仔细看了一下,这里免怎么没有try except? 你设置了超时,这里面肯定要有try except捕获的呀,超时了以后,你这里不捕获超时的异常,idhttp.get超时以后继续往下走,肯定会异常。所以我觉得要用类似下面的:try
idhtt.get()...
except
{程序退出,或者是重试什么的}
end;
if AnsiCompareText('gzip',idh.Response.ContentEncoding) = 0 then
begin
decomp := TIdCompressorZLibex.Create(nil);
这个时候idh.Response有可能就是nil,因为超时了,完全没有返回。
而其他的错误,404什么的,毕竟是有返回的,Response不是nil。
瞎猜的~ 哈哈哈
idhttp.get()
finallyend;这种结构中,出错也不会继续往下执行啊,出错应该到FINALLY段执行。
decomp.Free;
outstream.Free;
end;你确定没问题?(如果get出错,这两个对象还没创建,free方法会不会报错?)ps:做网络方面的程序要多考虑异常情况,不能只考虑正常流程。尤其是和网络交互的代码,get/post/send/recive什么的一句代码一句代码的抠,如果这句代码出错了,会对程序流程产生怎样的影响~
言尽于此~就像前面说的一样,也只能给你个思路。
目前我确定没有其他代码了,出错后就是到:
finally
decomp.Free;
outstream.Free;
end;
就如我在原帖最上面给出的那样,没有其他代码了。帮看看这段有什么错误没有:
function findbt._Get(url,scookie : String;idh:tidhttp) : String;
var
stream,outstream : TMemoryStream;
decomp : TIdCompressorZLibex;
resp : TStrings;
begin
//http := TIdHTTP.Create(nil);
idh.Request.CustomHeaders.Text :=scookie;
stream := TMemoryStream.Create;
resp := TStringList.Create;
try
idh.Get(url,stream);
stream.Position := 0;
if AnsiCompareText('gzip',idh.Response.ContentEncoding) = 0 then
begin
decomp := TIdCompressorZLibex.Create(nil);
outstream := TMemoryStream.Create;
try
decomp.DecompressGZipStream(stream,outstream);
outstream.Position := 0;
resp.LoadFromStream(outstream);
finally
decomp.Free;
outstream.Free;
end;
end
else
resp.LoadFromStream(stream);
Result := resp.Text;
finally
stream.Free;
resp.Free;
end;
end;
resp.Free;出错
可是resp已创建啊,释放怎么可能出错?