IDHTTP的超时怎么解决? ( 积分: 50 )

  • 主题发起人 主题发起人 citybug_ch
  • 开始时间 开始时间
C

citybug_ch

Unregistered / Unconfirmed
GUEST, unregistred user!
使用IDHTTP向一个网站不断发送数据,但是碰到网络发生问题或者网站服务器出问题,IDHTTP没有返回值,那我的程序就一直在等待,就象程序死了一样。请问如何在程序中处理这种情况?
 
使用IDHTTP向一个网站不断发送数据,但是碰到网络发生问题或者网站服务器出问题,IDHTTP没有返回值,那我的程序就一直在等待,就象程序死了一样。请问如何在程序中处理这种情况?
 
IdHTTP1.ReadTimeout := 3000; // 读取超时:3秒
 
如何在程序中处理超时?如果我发过去数据,即没有正确消息返回,又没有错误消息返回,程序怎么判断,怎么样能让程序继续走下去?
 
我也正好有这个问题
 
刚才试验一下,原来出错都是会产生exception的,捕捉一下就行了
 
比如下面这样的代码:
procedure ThreadTracData.Execute;
var
strsql: string;
i: integer;
PostStr: string;
strMsg: string;
sParam: TStringStream;
begin
{ Place thread code here }
SuperviseInfaceService.Query.Connection :=SuperviseInfaceService.Conn ;
SuperviseInfaceService.idHttp.Request.ContentType :='application/x-www-form-urlencoded';
SuperviseInfaceService.IdHTTP.ReadTimeout:=5000;
try
while not Terminated do
begin
//受理项目
strsql:='select * from AcceptItem order by Serialno';
SuperviseInfaceService.Query.SQL.Clear ;
SuperviseInfaceService.Query.SQL.Text:=strsql;
SuperviseInfaceService.Query.Open ;
if not SuperviseInfaceService.Query.Eof then
begin
for i:=0 to SuperviseInfaceService.Query.RecordCount -1 do
begin
SuperviseInfaceService.Conn.BeginTrans;
PostStr:=SuperviseInfaceService.Query.fieldByName('SerialNO').AsString ;
PostStr:=PostStr + '|&' + SuperviseInfaceService.Query.FieldByName('CreateTime').AsString ;
PostStr:=PostStr + '|&' + SuperviseInfaceService.Query.fieldByName('RecordType').AsString ;
PostStr:=PostStr + '|&' + SuperviseInfaceService.Query.FieldByName('DepartmentID').AsString ;
PostStr:=PostStr + '|&' + SuperviseInfaceService.Query.fieldByName('ItemClassID').AsString ;
PostStr:=PostStr + '|&' + SuperviseInfaceService.Query.fieldByName('ItemObjectID').AsString ;
PostStr:=PostStr + '|&' + SuperviseInfaceService.Query.fieldByName('ItemType').AsString ;
PostStr:=PostStr + '|&' + SuperviseInfaceService.Query.fieldByName('IsUnitObject').AsString ;
PostStr:=PostStr + '|&' + SuperviseInfaceService.Query.fieldByName('UnitOrder').AsString ;
PostStr:=PostStr + '|&' + SuperviseInfaceService.Query.fieldByName('AcceptDate').AsString ;
PostStr:=PostStr + '|&' + SuperviseInfaceService.Query.fieldByName('AcceptTime').AsString ;
PostStr:=PostStr + '|&' + SuperviseInfaceService.Query.fieldByName('OperatorID').AsString ;
PostStr:=PostStr + '|&' + SuperviseInfaceService.Query.fieldByName('OperatorName').AsString ;
PostStr:=PostStr + '|&' + SuperviseInfaceService.Query.fieldByName('OperatorBus').AsString ;
PostStr:=PostStr + '|&' + SuperviseInfaceService.Query.fieldByName('ApplicantOrgName').AsString ;
PostStr:=PostStr + '|&' + SuperviseInfaceService.Query.fieldByName('ApplicantPassType').AsString ;
PostStr:=PostStr + '|&' + SuperviseInfaceService.Query.fieldByName('ApplicantPassID').AsString ;
PostStr:=PostStr + '|&' + SuperviseInfaceService.Query.fieldByName('Material').AsString ;
PostStr:=PostStr + '|&' + SuperviseInfaceService.Query.fieldByName('Telephone').AsString ;
PostStr:=PostStr + '|&' + SuperviseInfaceService.Query.fieldByName('MobileTelephone').AsString ;
PostStr:=PostStr + '|&' + SuperviseInfaceService.Query.fieldByName('Fax').AsString ;
PostStr:=PostStr + '|&' + SuperviseInfaceService.Query.fieldByName('EMail').AsString ;
PostStr:=PostStr + '|&' + SuperviseInfaceService.Query.fieldByName('PostCode').AsString ;
PostStr:=PostStr + '|&' + SuperviseInfaceService.Query.fieldByName('Address').AsString ;
PostStr:=PostStr + '|&' + SuperviseInfaceService.Query.fieldByName('TagReturn').AsString ;
PostStr:=PostStr + '|&' + SuperviseInfaceService.Query.fieldByName('WriteReturn').AsString ;
PostStr:=PostStr + '|&' + SuperviseInfaceService.Query.fieldByName('IsAccepted').AsString ;
PostStr:=PostStr + '|&' + SuperviseInfaceService.Query.fieldByName('Mark').AsString ;
//SuperviseInfaceService.LogMessage(PostStr);

Try
sParam:=TStringStream.Create('');
sParam.WriteString('message='+UrlEncode(PostStr,true));
SuperviseInfaceService.IdHTTP.Post(SuperviseInfaceService.sURL,sParam);
sParam.Free;

if SuperviseInfaceService.IdHTTP.ResponseCode=200 then
begin
try
SuperviseInfaceService.Conn.BeginTrans;
InsertStr:='insert into AcceptItem_History(SerialNO,CreateTime,RecordType,DepartmentID,ItemClassID,ItemObjectID,';
InsertStr:=InsertStr + 'ItemType,IsUnitObject,UnitOrder,AcceptDate,AcceptTime,OperatorID,OperatorName,';
InsertStr:=InsertStr + 'OperatorBus,ApplicantOrgName,ApplicantPassType,ApplicantPassID,Material,Telephone,';
InsertStr:=InsertStr + 'MobileTelephone,Fax,Email,PostCode,Address,TagReturn,WriteReturn,IsAccepted,Mark,TranDate) values (';
InsertStr:=InsertStr + '''' +SuperviseInfaceService.Query.fieldByName('SerialNo').AsString + '''';
InsertStr:=InsertStr + ',''' +SuperviseInfaceService.Query.fieldByName('CreateTime').AsString + '''' ;
InsertStr:=InsertStr + ',''' +SuperviseInfaceService.Query.fieldByName('RecordType').AsString + '''' ;
InsertStr:=InsertStr + ',''' +SuperviseInfaceService.Query.fieldByName('DepartmentID').AsString + '''' ;
InsertStr:=InsertStr + ',''' +SuperviseInfaceService.Query.fieldByName('ItemClassID').AsString + '''' ;
InsertStr:=InsertStr + ',''' +SuperviseInfaceService.Query.fieldByName('ItemObjectID').AsString + '''' ;
InsertStr:=InsertStr + ',''' +SuperviseInfaceService.Query.fieldByName('ItemType').AsString + '''' ;
InsertStr:=InsertStr + ',''' +SuperviseInfaceService.Query.fieldByName('IsUnitObject').AsString + '''' ;
InsertStr:=InsertStr + ',''' +SuperviseInfaceService.Query.fieldByName('UnitOrder').AsString + '''' ;
InsertStr:=InsertStr + ',''' +SuperviseInfaceService.Query.fieldByName('AcceptDate').AsString + '''' ;
InsertStr:=InsertStr + ',''' +SuperviseInfaceService.Query.fieldByName('AcceptTime').AsString + '''' ;
InsertStr:=InsertStr + ',''' +SuperviseInfaceService.Query.fieldByName('OperatorID').AsString + '''' ;
InsertStr:=InsertStr + ',''' +SuperviseInfaceService.Query.fieldByName('OperatorName').AsString + '''' ;
InsertStr:=InsertStr + ',''' +SuperviseInfaceService.Query.fieldByName('OperatorBus').AsString + '''' ;
InsertStr:=InsertStr + ',''' +SuperviseInfaceService.Query.fieldByName('ApplicantOrgName').AsString + '''' ;
InsertStr:=InsertStr + ',''' +SuperviseInfaceService.Query.fieldByName('ApplicantPassType').AsString + '''' ;
InsertStr:=InsertStr + ',''' +SuperviseInfaceService.Query.fieldByName('ApplicantPassID').AsString + '''' ;
InsertStr:=InsertStr + ',''' +SuperviseInfaceService.Query.fieldByName('Material').AsString + '''' ;
InsertStr:=InsertStr + ',''' +SuperviseInfaceService.Query.fieldByName('Telephone').AsString + '''' ;
InsertStr:=InsertStr + ',''' +SuperviseInfaceService.Query.fieldByName('MobileTelephone').AsString + '''' ;
InsertStr:=InsertStr + ',''' +SuperviseInfaceService.Query.fieldByName('Fax').AsString + '''' ;
InsertStr:=InsertStr + ',''' +SuperviseInfaceService.Query.fieldByName('Email').AsString + '''' ;
InsertStr:=InsertStr + ',''' +SuperviseInfaceService.Query.fieldByName('PostCode').AsString + '''' ;
InsertStr:=InsertStr + ',''' +SuperviseInfaceService.Query.fieldByName('Address').AsString + '''' ;
InsertStr:=InsertStr + ',''' +SuperviseInfaceService.Query.fieldByName('TagReturn').AsString + '''' ;
InsertStr:=InsertStr + ',''' +SuperviseInfaceService.Query.fieldByName('WriteReturn').AsString + '''' ;
InsertStr:=InsertStr + ',''' +SuperviseInfaceService.Query.fieldByName('IsAccepted').AsString + '''' ;
InsertStr:=InsertStr + ',''' +SuperviseInfaceService.Query.fieldByName('Mark').AsString + '''' ;
InsertStr:=InsertStr + ',getdate())';
//SuperviseInfaceService.LogMessage(InsertStr);
SuperviseInfaceService.Conn.Execute(InsertStr);

strsql:='delete AcceptItem where SerialNO=''' + SuperviseInfaceService.Query.fieldByName('SerialNO').AsString + '''';
SuperviseInfaceService.Conn.Execute(strsql);
SuperviseInfaceService.Conn.CommitTrans;
except
SuperviseInfaceService.Conn.RollbackTrans;
end;
end;
except
//SuperviseInfaceService.LogMessage ('受理发送异常');
end;
SuperviseInfaceService.Query.Next;
if SuperviseInfaceService.Query.Eof then Break;
end;
end;
SuperviseInfaceService.Query.Close ;

sleep(5000);
end
except on E: Exception do
begin
SuperviseInfaceService.Conn.RollbackTrans;
SuperviseInfaceService.LogMessage('数据库错误:' + e.message);
end;
end;
end;
 
忘记说明了
这个是服务中的一个线程。数据库连接是在服务开始的时候配置的。IDHTTP的超时设置为5000。在服务运行过程中,在晚上的某个时候就停止发送数据了,但是服务还在运行中。只有重新启动服务才能恢复数据发送功能。请问:问题可能出现在什么地方?是不是HTTP的设置上有问题还是程序的逻辑上有问题?
 
哪用这么麻烦,idhttp1.ConnectTimeout:=10000;ok!
 
后退
顶部