• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2979
  • Last Modified:

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

0
dirkil2
Asked:
dirkil2
  • 3
1 Solution
 
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

Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now