zoukankan      html  css  js  c++  java
  • Delphi使用Indy、ICS组件读取网页

    使用Indy 10中TIdHTTP的例子:

    代码
    uses
      IdHttp;
    .
    .
    .
    function HttpGet(const Url: stringvar Html: string): Boolean;
    var
      HttpClient: TIdHTTP;
    begin
      Result :
    = False;
      HttpClient :
    = TIdHTTP.Create(nil);
      
    try
        Html :
    = HttpClient.Get(Url);
        Result :
    = True;
      
    except
        on e: Exception 
    do
        
    begin
        
    end;
      
    end;
      HttpClient.Free;
    end;

    Indy采用的是同步I/O的方式,而且在连接超时控制方面存在bug,因此TIdHttp.Get()有时会发生陷入死锁无法返回的问题。

    使用ICS中THttpCli的例子:

     代码

    uses
      HttpProt;
    .
    .
    .
    function HttpGet(const Url: stringvar Html: string): Boolean;
    var
      HttpClient: THttpCli;
      DataLen: Int64;
      FailMsg: string;
    begin
      Result := False;
      HttpClient := THttpCli.Create(nil);
      HttpClient.URL := Url;
      HttpClient.NoCache := True;
      HttpClient.RcvdStream := TMemoryStream.Create;
      
    try
        
    try
          HttpClient.Get;
          DataLen := HttpClient.RcvdStream.Size;
          SetLength(Html, DataLen);
          HttpClient.RcvdStream.Position := 0;
          HttpClient.RcvdStream.Read(PChar(Html)^, DataLen);
          Result := True;
        
    except
          on E: EHttpException do
          
    begin
            FailMsg := Format('Failed : %d %s',
              [HttpClient.StatusCode, HttpClient.ReasonPhrase]);
          
    end else
            
    raise;
        
    end;
      
    finally
        HttpClient.RcvdStream.Free;
        HttpClient.RcvdStream := nil;
        HttpClient.Free;
      
    end;
    end;

    ICS使用的是异步I/O,其TFtpClient组件有Timout属性可以对连接超时进行控制,而THttpCli组件没有。但可以采用在定时器中调用THttpCli.Abort()取消连接的方式控制超时,也可以显式调用异步方法:

    代码
    var
      HttpClient: THttpCli;
      DataLen: Int64;
      FailMsg: 
    string;
      Tick: Cardinal;
    begin
      .
      .
      .
      Tick :
    = GetTickCount;
      
    try
        
    try
          HttpClient.GetASync;
          
    while HttpClient.State <> httpReady do
          
    begin
            
    if GetTickCount - Tick > 30*1000 then
            
    begin
              HttpClient.Abort;
              Exit;
            
    end;
            Application.ProcessMessages;
          
    end;
      .
      .
      .


  • 相关阅读:
    HDU 1285 确定比赛名次(拓扑排序模板)
    POJ 1679 The Unique MST(次小生成树)
    POJ 3026 Borg Maze(Prim+bfs求各点间距离)
    POJ 2349 Arctic Network(最小生成树+求第k大边)
    POJ 3169 Layout (spfa+差分约束)
    给定两个list A ,B,请用找出 A ,B中相同的元素,A ,B中不同的元素 ??
    什么是http协议??
    Python2中range 和xrange的区别??
    死锁 ??
    调度算法 ??
  • 原文地址:https://www.cnblogs.com/ddgg/p/1808058.html
Copyright © 2011-2022 走看看