سؤال

I'm using code I found here to get movie titles from www.imdb.com and it works fine, but since imdb has some sort of geolocation I need to figure how to force the results in english with a "Accept-Language: en" header but I'm not sure if I can with this code. Is it possible?

uses
  WinInet,
  StrUtils,
  SysUtils;

function GetHTMLTitle(const Url:string):string;
    const
    BuffSize     = 64*1024;
    TitleTagBegin='<title>';
    TitleTagEnd  ='</title>';
    var
      hInter   : HINTERNET;
      UrlHandle: HINTERNET;
      BytesRead: Cardinal;
      Buffer   : Pointer;
      i,f      : Integer;
    begin
      Result:='';
      hInter := InternetOpen('', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
      if Assigned(hInter) then
      begin
        GetMem(Buffer,BuffSize);
        try
           UrlHandle := InternetOpenUrl(hInter, PChar(Url), nil, 0, INTERNET_FLAG_RELOAD,0);
           try
            if Assigned(UrlHandle) then
            begin
              InternetReadFile(UrlHandle, Buffer, BuffSize, BytesRead);
              if BytesRead>0 then
              begin
                SetString(Result, PAnsiChar(Buffer), BytesRead);
                i:=Pos(TitleTagBegin,Result);
                if i>0 then
                begin
                  f:=PosEx(TitleTagEnd,Result,i+Length(TitleTagBegin));
                  Result:=Copy(Result,i+Length(TitleTagBegin),f-i-Length(TitleTagBegin));
                end;
              end;
            end;
           finally
             InternetCloseHandle(UrlHandle);
           end;
        finally
          FreeMem(Buffer);
        end;
        InternetCloseHandle(hInter);
      end
    end;

procedure TForm1.Button1Click(Sender: TObject);
var
  Title : string;
begin
  Title := GetHTMLTitle('http://www.imdb.com/title/tt1490017/');
  Label1.Caption := Copy(Title, 1, LastDelimiter(')', Title));
end;
هل كانت مفيدة؟

المحلول

The 3rd parameter of InternetOpenUrl() allows you to specify custom headers, eg:

UrlHandle := InternetOpenUrl(hInter, PChar(Url), 'Accept-Language: en', -1, INTERNET_FLAG_RELOAD,0);

BTW, there is no guarantee that InternetReadFile() will return everything in a single buffer. You should be calling it in a loop, appending each buffer to a local cache, until you have cached everything you need, then process the cache as needed.

Update: IMDB does indeed use IP geolocation, so apparently it does not use Accept-Language like it used to. You will have to adjust your HTTP requests according:

Why am I seeing titles in a different language than before?

We recently modified the way titles are displayed on the site. Users will now by default see titles listed in the language used for release in their country (i.e. US-based users will see films listed under the title used for the North American release, while users browsing the site from France will see titles as they appeared on the French release of those movies etc.) If you are registered on IMDb.com and you prefer to see original titles, or the title in any other language/country of release by default, you can set your user preferences accordingly.

To do this, please click on the 'Your Account' link (located in the upper right corner of every page on IMDb.com) and then click on the 'Site Preferences' option, or go directly to https://secure.imdb.com/register-imdb/siteprefs , and then choose the desired options from the "Title Display Country" and "Title Display Language" dropdown menus. Click 'OK' to save your choice.

For more help/information, please refer to our site preferences help page.

Please also remember that if you access the site through the following URL, you will always see films listed under their original titles regardless of your login status or site preferences:

http://akas.imdb.com

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top