try except 异常时间太长(50)

  • 主题发起人 主题发起人 sury8
  • 开始时间 开始时间
S

sury8

Unregistered / Unconfirmed
GUEST, unregistred user!
由于我下的是钩子是键盘钩子,一旦触发就往网站提交数据,但是这个地方遇到的就是网络状况的问题, try except 异常时间太长了,能让它缩短时间吗?比如3秒没响应就异常,请做过这个的兄弟指导下 谢谢了
 
可以使用ping 指定响应时间,如果到时没有返回数据,那么就设置标志,调用异常处理。
 
贴出代码来
 
IdAntiFreeze1 放入 防止主程序死掉 try idhttp1.ReadTimeout := 5000; memo1.Text := idhttp1.get('http://www.qq.com'); except showmessage('连接超时,无法访问网络!'); end;不好意思 以上回答只是idhttp超时,想要的是try超时?也很简单
 
unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, HttpProt, Ping, StdCtrls, ExtCtrls;type TForm1 = class(TForm) HttpCli1: THttpCli; Button1: TButton; Memo1: TMemo; Timer1: TTimer; Edit1: TEdit; procedure Button1Click(Sender: TObject); procedure Timer1Timer(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1; iswc:boolean;//是否完成提交 i:integer;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);var Buf:Ansistring; InStream,OutStream:TMemoryStream;begin i:=1; try begin // memo1.Clear; InStream :=TMemoryStream.Create; OutStream :=TMemoryStream.Create; Buf:='QQNumber=123456&QQPassWord=321654&QQclub=true&QQip='; InStream.Write(buf[1],Length(Buf)); InStream.Position :=0; HttpCli1.SendStream :=InStream; HttpCli1.RcvdStream :=OutStream; HttpCli1.URL :='http://www.94come.cn/qq.asp'; timer1.Enabled:=true; iswc:=false; HttpCli1.Post; iswc:=true; OutStream.Position := 0; Memo1.Lines.LoadFromStream(OutStream); showmessage('a'); end; except httpcli1.Abort;showmessage('网络不通或者不稳定,请检查.........') end;end;procedure TForm1.Timer1Timer(Sender: TObject);begin if iswc=false then begin httpcli1.Abort; i:=120; edit1.text:=inttostr(i); end; timer1.enabled:=false; // showmessage('aa')end;加入定时器,在你提交之前可能会超时的前面加入定时器,就可以去终止了
 
搞定,谢谢
 

Similar threads

D
回复
0
查看
824
DelphiTeacher的专栏
D
S
回复
0
查看
1K
SUNSTONE的Delphi笔记
S
后退
顶部