bobonwhidbey
bobonwhidbey

Reputation: 505

InternetReadFile timing out?

To grab an html page I've used the following for some time with no problem. I'm now getting I/O 1784 errors whenever it takes the website a long time (1 minute plus) to serve the page. I'm guessing the real error is that my process is timing out. How would I extend that time?

function GetInternetFile(const fileURL, FileName: string): boolean;
const
  BufferSize = 1024;
  var
  hSession, hURL: HInternet;
  Buffer: array[1..BufferSize] of Byte;
  BufferLen: DWORD;
  f: file;
  sAppName: string;
begin
  sAppName := ExtractFileName(Application.ExeName);
  hSession := InternetOpen(PChar(sAppName), INTERNET_OPEN_TYPE_PRECONFIG, nil,
    nil, 0);
  try
    Screen.Cursor := crHourGlass;
    hURL := InternetOpenURL(hSession, PChar(fileURL), nil, 0, 0, 0);
    try
      AssignFile(f, FileName);
      Rewrite(f, 1);
      i := 0;
      repeat
        InternetReadFile(hURL, @Buffer, SizeOf(Buffer), BufferLen);
        BlockWrite(f, Buffer, BufferLen);
      until BufferLen = 0;
      CloseFile(f);
      result := True;
    finally
      InternetCloseHandle(hURL)
    end
  finally
    InternetCloseHandle(hSession);
    Screen.Cursor := crDefault;
  end
end;

Upvotes: 2

Views: 573

Answers (1)

Chris Thornton
Chris Thornton

Reputation: 15817

Try calling InternetSetOption(); Here is how we set both the send and receive timeout, prior to lengthy SOAP calls:

var
  TimeOut: Integer;
begin
  TimeOut := (NumSecs * 1000);
  InternetSetOption(Data, INTERNET_OPTION_RECEIVE_TIMEOUT,  
                    Pointer(@TimeOut),       SizeOf(TimeOut));
  InternetSetOption(Data, INTERNET_OPTION_SEND_TIMEOUT,  Pointer(@TimeOut),  
                    SizeOf(TimeOut));

Upvotes: 2

Related Questions