The Road to Delphi

Delphi – Free Pascal – Oxygene

Creating a TinyUrl from Delphi

4 Comments

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

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('https://theroadtodelphi.wordpress.com/'));
     Readln;
  except
    on E:Exception do
      Writeln(E.Classname, ': ', E.Message);
  end;
end.

and the response is

http://tinyurl.com/2ca52me

Author: Rodrigo

Just another Delphi guy.

4 thoughts on “Creating a TinyUrl from Delphi

  1. thanks.

  2. Pingback: Tweets that mention Creating a TinyUrl from Delphi « The Road to Delphi – a Blog About Delphi Programming (mostly) -- Topsy.com

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

    Thanks.

  4. Pingback: Sip from the Firehose : Developer Christmas Countdown - December 11 - T minus 14 days

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s