Recent

Author Topic: [solved] Obtain an expanded URL from tinyurl.com  (Read 3504 times)

bobonwhidbey

  • Hero Member
  • *****
  • Posts: 630
    • Double Dummy Solver - free download
[solved] Obtain an expanded URL from tinyurl.com
« on: September 07, 2016, 05:15:11 am »
When I put this URL:
 
Code: HTML5  [Select][+][-]
  1. http://tinyurl.com/jrs3grj
in my browser, the tinyurl.com website expands the URL to a very long URL and redirects my browser to open the large URL.

I use the following code to download an HTML file from the expanded URL.

Code: Pascal  [Select][+][-]
  1. function GetInternetFile(const fileURL, FileName: string): boolean;
  2. const
  3.   BufferSize = 1024;
  4. var
  5.   hSession, hURL: HInternet;
  6.   Buffer: array[0..BufferSize - 1] of byte;
  7.   BufferLen: DWORD;
  8.   f: file;
  9.   sAppName: string;
  10.   MyTimeOut, Minutes: integer;
  11. begin
  12.   Minutes := 10;
  13.   myTimeOut := 1000 * 60 * Minutes;
  14.   InternetSetOption(nil, INTERNET_OPTION_CONNECT_TIMEOUT, Pointer(@myTimeOut),
  15.     SizeOf(myTimeOut));
  16.   InternetSetOption(nil, INTERNET_OPTION_SEND_TIMEOUT, Pointer(@myTimeOut),
  17.     SizeOf(myTimeOut));
  18.   InternetSetOption(nil, INTERNET_OPTION_RECEIVE_TIMEOUT, Pointer(@myTimeOut),
  19.     SizeOf(myTimeOut));
  20.   sAppName := ExtractFileName(Application.ExeName);
  21.   hSession := InternetOpen(PChar(sAppName), INTERNET_OPEN_TYPE_PRECONFIG,
  22.     nil, nil, 0);
  23.   try
  24.     hURL := InternetOpenURL(hSession, PChar(fileURL), nil, 0, 0, 0);
  25.     try
  26.       AssignFile(f, FileName);
  27.       Rewrite(f, 1);
  28.       repeat
  29.         try
  30.           InternetReadFile(hURL, @Buffer, SizeOf(Buffer), BufferLen);
  31.           BlockWrite(f, Buffer, BufferLen);
  32.         except
  33.           on E: EInOutError do begin
  34.             ShowMessage('The ' + IntToStr(Minutes) +
  35.               ' minute maximum download time' +
  36.               ' has been exceded. Please try again at another time');
  37.             break;
  38.           end;
  39.         end; // except
  40.       until BufferLen = 0;
  41.       CloseFile(f);
  42.       Result := True;
  43.     finally
  44.       InternetCloseHandle(hURL)
  45.     end
  46.   finally
  47.     InternetCloseHandle(hSession);
  48.   end;
  49. end;
  50.  
  51.  

What I want to do is grab the expanded URL. It seems to me that it must exist in an hInternet Handle variable. Anyone know?
« Last Edit: September 07, 2016, 03:38:55 pm by bobonwhidbey »
Lazarus 3.8 FPC 3.2.2 x86_64-win64-win32/win64


bobonwhidbey

  • Hero Member
  • *****
  • Posts: 630
    • Double Dummy Solver - free download
Re: Obtain an expanded URL from tinyurl.com
« Reply #2 on: September 07, 2016, 06:54:17 am »
I don't really understand this function. I tried this on a guess - with no luck.

          InternetSetOption(nil, INTERNET_STATUS_REDIRECT, @Buffer, BufferLen);

How should this be formated?
Lazarus 3.8 FPC 3.2.2 x86_64-win64-win32/win64

Leledumbo

  • Hero Member
  • *****
  • Posts: 8835
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Obtain an expanded URL from tinyurl.com
« Reply #3 on: September 07, 2016, 07:58:55 am »
MUCH shorter and cross platform:
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. uses
  3.   SysUtils,fphttpclient;
  4. begin
  5.   with TFPHTTPClient.Create(nil) do
  6.     try
  7.       HTTPMethod('HEAD','http://tinyurl.com/jrs3grj',nil,[301]); // just use HEAD, we're not interested in the body, there will be none anyway
  8.       ResponseHeaders.NameValueSeparator := ':'; // HTTP header uses colon as separator
  9.       WriteLn(Trim(ResponseHeaders.Values['Location'])); // there will be a space in front of the value, hence the Trim()
  10.     finally
  11.       Free;
  12.     end;
  13. end.
  14.  

bobonwhidbey

  • Hero Member
  • *****
  • Posts: 630
    • Double Dummy Solver - free download
Re: Obtain an expanded URL from tinyurl.com
« Reply #4 on: September 07, 2016, 03:31:29 pm »
This worked perfectly Leledumbo.  Thank you very much.
« Last Edit: September 07, 2016, 03:37:56 pm by bobonwhidbey »
Lazarus 3.8 FPC 3.2.2 x86_64-win64-win32/win64

 

TinyPortal © 2005-2018