Skip to content
The Road to Delphi — Delphi, VCL, VCL Styles, UxTheme and WinAPI

Creating a TinyUrl from Delphi

The TinyUrl free service convert a long URL to small url, which is useful for example to make twitter posts.

to use this service from delphi you must make a request to this link http://tinyurl.com/api-create.php?url=the_url_to_convert_goes_here and the response is the TinyUrl generated

check the sample code to get the response

[sourcecode wraplines=”false” language=”delphi”]
program DelphiTinyUrl;

{$APPTYPE CONSOLE}

uses
WinInet,
SysUtils;

function GetTinyUrl(Url:string):string;
const
tinyurl = ‘http://tinyurl.com/api-create.php?url=%s’;
BuffSize = 2048;
var
hInter : HINTERNET;
UrlHandle: HINTERNET;
BytesRead: Cardinal;
Buffer : Pointer;
begin
Result:=”;
hInter := InternetOpen(”, INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
if Assigned(hInter) then
begin
GetMem(Buffer,BuffSize);
try
UrlHandle := InternetOpenUrl(hInter, PChar(Format(tinyurl,[Url])), nil, 0, INTERNET_FLAG_RELOAD, 0);
if Assigned(UrlHandle) then
begin
InternetReadFile(UrlHandle, Buffer, BuffSize, BytesRead);
if BytesRead>0 then
SetString(Result, PAnsiChar(Buffer), BytesRead);
InternetCloseHandle(UrlHandle);
end;
finally
FreeMem(Buffer);
end;
InternetCloseHandle(hInter);
end
end;

begin
try
Writeln(GetTinyUrl(‘http://theroadtodelphi.com/’));
Readln;
except
on E:Exception do
Writeln(E.Classname, ‘: ‘, E.Message);
end;
end.
[/sourcecode]

and the response is

http://tinyurl.com/2ca52me

4 responses

  1. mohammaddesign Avatar
    mohammaddesign

    thanks.

  2. […] This post was mentioned on Twitter by MarcoCantu, Rodrigo Ruz. Rodrigo Ruz said: Creating a TinyUrl from Delphi: The TinyUrl free service convert a long URL to small url, which is useful for ex… http://bit.ly/i7SeH9 […]

  3. Hello.
    It’s a usefull tip. Clear and simple.

    Thanks.

Leave a Reply