Mengetes Suatu URL Ada/Tidak

Gimana ya caranya kalau mau cek apakah suatu URL (misal
http://www.coba.com/files/12345.exe ) itu ada / tidak.

Sebenarnya operasi-operasi ringan dengan HTTP di Windows bisa memakai
WinInet. Kebetulan saya pernah oprek, jadi kalo sekedar perintah ‘HEAD’ bisa seperti berikut.


uses WinInet;

function GetHttpStatus(Host, fileURL: string): Integer;
var
  hSession, hConnect, hRequest: HInternet;
  BufferLen, Dummy: Cardinal;
begin
  Result := 0;
  hSession := InternetOpen(nil, INTERNET_OPEN_TYPE_DIRECT, nil, nil, 0);
  if not Assigned(hSession) then RaiseLastOSError;  {CMIIW}
  hConnect := InternetConnect(hSession, PChar(Host), INTERNET_DEFAULT_HTTP_PORT,
    nil, nil, INTERNET_SERVICE_HTTP, 0, 0);
  if Assigned(hConnect) then begin
    hRequest := HttpOpenRequest(hConnect, ‘HEAD’, PChar(fileURL), ‘HTTP/1.0’,
      nil, nil, INTERNET_FLAG_NO_COOKIES or INTERNET_FLAG_DONT_CACHE, 0);
    if Assigned(hRequest) then try
      HttpSendRequest(hRequest, nil, 0, nil, 0);
      Dummy := 0; BufferLen := SizeOf(Result);
      if not HttpQueryInfo(hRequest, HTTP_QUERY_STATUS_CODE or HTTP_QUERY_FLAG_NUMBER,
        @Result, BufferLen, Dummy) then Result := HTTP_STATUS_BAD_REQUEST;
    finally
      InternetCloseHandle(hConnect);
    end;
    InternetCloseHandle(hRequest);
  end;
  InternetCloseHandle(hSession);
end;


if GetHttpStatus(‘www.coba.com’, ‘/files/12345.exe’) <> 2xx then

Parameter-parameter query, dan seterusnya, bisa dilihat di WinInet.Pas.

1 Komentar »

  1. Kecret said

    Mangstabsssss emank bro Mpu Gondrong …… BTW ga pernah ada KopDar Delphindo lagi nehh ???….

RSS feed for comments on this post · TrackBack URI

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s