Link to home
Start Free TrialLog in
Avatar of dirkil2
dirkil2

asked on

InternetOpenUrl: how to increase timeout?

In my Delphi program I need to download a file. Since when the request arrives it takes some time for the server to compile the data to download it could happen that the client has to wait for some time. Unfortunately, my client doesn't wait long enough and the call to InternetOpenUrl returns error code 12002.

Therefore my question: how can I increase the time allowed to wait for the InternetOpenUrl call to return?

See attached code for details.

Regards,
Dirk.
hSession := InternetOpen('MyApp', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
  if not Assigned(hSession) then Exit;
  try
    hService := InternetOpenUrl(hSession, PChar(aUrl), nil, 0, 0, 0);
    if not Assigned(hService) then
    begin
      FErrorText := SysErrorMessage(GetLastError);

      Exit;
    end;

Open in new window

Avatar of Ferruccio Accalai
Ferruccio Accalai
Flag of Italy image

Try like this


function DownloadFileToStream(const aUrl: string; const pStream: TStream): Boolean;
var
  hSession: HINTERNET;
  hService: HINTERNET;
  lpBuffer: array[0..1023] of Byte;
  dwBytesRead: DWORD;
  dwTimeOut: DWORD;
begin
  Result := False;
  hSession := InternetOpen('MyApp', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
  if not Assigned(hSession) then Exit;
  try
    hService := InternetOpenUrl(hSession, PChar(aUrl), nil, 0, 0, 0);
    if not Assigned(hService) then Exit;
    try
      dwTimeOut := 180000; // in milleseconds.
      InternetSetOption(hService, INTERNET_OPTION_RECEIVE_TIMEOUT, @lTimeOut, SizeOf(lTimeOut));
      repeat
        if not InternetReadFile(hService, @lpBuffer[0], SizeOf(lpBuffer), dwBytesRead) then Exit;
        if dwBytesRead = 0 then Break;
        pStream.WriteBuffer(lpBuffer[0], dwBytesRead);
      until False;
    finally
      InternetCloseHandle(hService);
    end;
  finally
    InternetCloseHandle(hSession);
  end;
  Result := True;
end;

Open in new window

Avatar of dirkil2
dirkil2

ASKER

If I am not mistaken this is exactly the same as my code. The call to InternetOpenUrl times out and then your code exits as mine does.
Avatar of dirkil2

ASKER

See attachment for information which timeouts I tried. But unfortunately I am always getting error 317 back.

When I try and enter the same request into Internet Explorer it downloads the file.

I am still hoping that someone has an idea how I can avoid this timeout.
dwTimeOut    : DWORD;
begin
  dwTimeOut := 1800000;
  InternetSetOption(hService, INTERNET_OPTION_CONNECT_TIMEOUT, @dwTimeOut, SizeOf(dwTimeOut));
  InternetSetOption(hService, INTERNET_OPTION_SEND_TIMEOUT, @dwTimeOut, SizeOf(dwTimeOut));
  InternetSetOption(hService, INTERNET_OPTION_RECEIVE_TIMEOUT, @dwTimeOut, SizeOf(dwTimeOut));

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of dirkil2
dirkil2

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial