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

dirkil2Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
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

0
dirkil2Author Commented:
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.
0
dirkil2Author Commented:
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

0
dirkil2Author Commented:
Ok, now it's clear what the problem is. I made a stupid mistake by trying to call InternetSetOption before opening the connection. When I do it the other way round and set the above timeout option then it'll work.

Sorry, for not posting my full code. With the information I gave the problem was impossible to solve.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Browsers

From novice to tech pro — start learning today.