我打算利用 Webbrowser对一批URL进行存在性检测,但有一些URL是不存在的,请问在delphi中,WEBBrowser如何设置超时的时间?我想如果3秒没有反应就跳过去。

解决方案 »

  1.   

    用它来进行URL检查太麻烦了吧。function CheckUrl(const S: String): Boolean;
    var
      hSession, hfile: hInternet;
      dwindex,dwcodelen: dword;
      dwcode: array[1..20] of char;
      res: pchar;
      url:String;
    begin
      url:=S;
      if pos('http://',lowercase(url)) = 0 then
         url := 'http://' + url;
      Result := false;
      hSession := InternetOpen('InetURL:/1.0',
           INTERNET_OPEN_TYPE_PRECONFIG,nil, nil, 0);
      if assigned(hsession) then
      begin
        hfile := InternetOpenUrl(
             hsession,
             pchar(url),
             nil,
             0,
             INTERNET_FLAG_RELOAD,
             0);
        dwIndex  := 0;
        dwCodeLen := 10;
        HttpQueryInfo(hfile, HTTP_QUERY_STATUS_CODE,
                @dwcode, dwcodeLen, dwIndex);
        res := pchar(@dwcode);
        result := (res = '200') or (res = '302');
        if assigned(hfile) then
          InternetCloseHandle(hfile);
        InternetCloseHandle(hsession);
      end;
    end;
      

  2.   

    忘记说一个要求了。除了验证这个URL是否存在外,还需要判断这个 URL 是否包含特定的字符串。如果有才可用, 如果没有, 不能用。
      

  3.   

    單純檢測網址建議用Tidhttp
    var 
    httpget:Tidhttp;
    begin       HttpGet:= TIdhttp.Create(nil);
           HttpGet.ReadTimeout:= 30000;(超時30秒)
           HttpGet.get('http://www');
            ...       httpget.free;
      

  4.   

    用它:ansicontainsstr(網址,'特定字符串') 返回boolean
      

  5.   

    楼上的师兄。我用IDhttp,GEt出来的网页源码,怎么会有乱码呢(据说是utf-16还是什么的)?以下是我原来写的Get的函数。Function TCheckThread.GetWebPage(const Url: string):string;
    var 
      Session,
      HttpFile:HINTERNET;
      szSizeBuffer:Pointer;
      dwLengthSizeBuffer:DWord;
      dwReserved:DWord; 
      dwFileSize:DWord;
      dwBytesRead:DWord;
      Contents:PChar;
      TimeOut:Integer;
    begin
      TimeOut:=5000;  //设置超时时间
      try
        InternetSetOption(Session, Internet_OPTION_CONNECT_TIMEOUT, @TimeOut, 4);
        Session:=InternetOpen('Mozilla/4.0',INTERNET_OPEN_TYPE_PRECONFIG,niL,niL,0);
        HttpFile:=InternetOpenUrl(Session,PChar(Url),niL,0,0,0);
        dwLengthSizeBuffer:=1024;
        HttpQueryInfo(HttpFile,5,szSizeBuffer,dwLengthSizeBuffer,dwReserved);
        GetMem(Contents,dwFileSize);
        InternetReadFile(HttpFile,Contents,dwFileSize,dwBytesRead);
        InternetCloseHandle(HttpFile);
        InternetCloseHandle(Session); 
        Result:=StrPas(Contents);
        FreeMem(Contents);
      Except
      End;
    End;
    另外如果这个函数放在多线程里面,怎么确保Get到HTML:在单线程的环境里,可以取到源代码,但在多线程里面好像不行。我想知道怎么知道IDhttp下载网页源码完毕了?
      

  6.   

    如果是在多線程中,且線程只有這個動作的話。下載完畢就會就線程結束所以FreeOnTerminate := true; 后就可以用OnTerminate:= 事件來告訴你已下載完畢!
      

  7.   

    我的问题是线程里面好像没有等待取到html的源码,而直接就跳过去了。如何在子线程里面一直等待到 httpget 完成取得网页源码的动作后,才进行下一步。