在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
因项目需要进行http的post提交数据,开始时用indy的idHttp组件,但是测试时发现当网络中断(如拔掉网线),idHttp的超时设置无效果,要等20秒才提示超时(参考网上的做法,将indy9升级到indy10,且设置了idHttp的connetTimeout,ReadTimeout属性,均无效果)。 后来发现用ICS组件THttpCli可以很好的控制提交超时,达到我的目的。 ICS版本:OverbyteIcsV7Gold 下载:http://www.overbyte.be/arch/OverbyteIcsV7Gold.zip Delphi7安装ICS 1、下载完成后解压到英文目录,建议解压缩到Delphi7安装目录,如:C:\Program Files\Borland\OverbyteIcsV7Gold 2、Tools——Environment Options——library,在library path里加入OverbyteIcsV7Gold->Delphi->Vc32目录。 3、从File->Open中打开OverbyteIcsV7Gold->Delphi->Vc32->OverbyteIcsD7Design.dpk文件,compile——install。
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, OverbyteIcsWndControl, OverbyteIcsHttpProt, StdCtrls; type TForm1 = class(TForm) HttpCli1: THttpCli; Button1: TButton; Memo1: TMemo; Edit1: TEdit; procedure Button1Click(Sender: TObject); procedure HttpCli1RequestDone(Sender: TObject; RqType: THttpRequest; ErrCode: Word); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); var Data : String; retStr: string; DataLen: Int64; FailMsg: string; begin Data := '{' + '"machineCode":"123",' + '"orgCode":"123",' + '"data": {' + '"cardNo": "12345678",' + '"cardType": "2"' + '}' + '}'; HttpCli1.SendStream := TMemoryStream.Create; HttpCli1.RcvdStream := TMemoryStream.Create; try HttpCli1.SendStream.Write(Data[1], Length(Data)); HttpCli1.SendStream.Seek(0, 0); HttpCli1.URL := Trim(Edit1.Text); HttpCli1.Timeout := 2;//V7.04 sync only! try HttpCli1.ContentTypePost := 'application/json;charset=utf-8'; HttpCli1.Post; DataLen := HttpCli1.RcvdStream.Size; SetLength(retStr,DateDelta); HttpCli1.RcvdStream.Position := 0; HttpCli1.RcvdStream.Read(PChar(retStr)^,DataLen); Memo1.Text := retStr; //ShowMessage(retStr); except on E: Exception do begin ShowMessage(e.Message); end; end; finally HttpCli1.RcvdStream.Free; HttpCli1.RcvdStream := nil; HttpCli1.SendStream.Free; HttpCli1.SendStream := nil; end; end; procedure TForm1.HttpCli1RequestDone(Sender: TObject; RqType: THttpRequest; ErrCode: Word); var Data : String; begin // HttpCli1.SendStream.Free; // HttpCli1.SendStream := nil; // //ShowMessage(IntToStr(ErrCode)); // if ErrCode <> 0 then //有异常 // begin // HttpCli1.RcvdStream.Free; // HttpCli1.RcvdStream := nil; // Exit; // end; // if HttpCli1.StatusCode <> 200 then //返回错误 // begin // HttpCli1.RcvdStream.Free; // HttpCli1.RcvdStream := nil; // Exit; // end; // HttpCli1.RcvdStream.Seek(0, 0); // SetLength(Data, HttpCli1.RcvdStream.Size); // HttpCli1.RcvdStream.Read(Data[1], Length(Data)); end; end.
安装包和Demo下载: http://download.csdn.net/detail/tc310/9384426
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论