• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Delphi使用进行post数据时超时设置

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

因项目需要进行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 

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
matlab图形操作发布时间:2022-07-18
下一篇:
Matlab Robotic Toolbox工具箱学习笔记发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap