How can I fetch the source of a Web page in Delphi?

I'm fetching Web page source with IdHTTP Indy component, but after reading about a problem with it and some questions on this Web site, it seems that it's not always the best choice.

What are your suggestions for fetching Web pages?

Upvotes: 1

Views: 1690

Answers (3)

Max Kleiner
Max Kleiner

Reputation: 1612

With COM/ActiveX objects, WinHttp.WinHttpRequest.5.1:

function getPostTranslateLibre3(feedstream, fromlang, tolang: string): string;
var
  Url,aAPI_KEY, source: string;
  jo, locate: TJSONObject;
  httpReq,hr: Olevariant;
  strm: TStringStream;
begin
  httpReq:= CreateOleObject('WinHttp.WinHttpRequest.5.1');
  // Open the HTTPs connection.  
  try              
    hr:= httpReq.Open('POST','https://libretranslate.pussthecat.org/translate', false);
    httpReq.setRequestheader('user-agent',CUSERAGENT  );
    httpReq.setRequestheader('content-type','application/x-www-form-urlencoded');  
             
    if hr= S_OK then HttpReq.Send('q='+HTTPEncode(feedstream)+
                                  '&source='+fromlang+'&target='+tolang);
    If HttpReq.Status = 200 Then
       result:= HttpReq.responseText
    Else result:= 'Failed at getting response:'+itoa(HttpReq.Status)+HttpReq.responseText;
    //writeln('debug response '+HttpReq.GetAllResponseHeaders);     
  finally
    httpreq:= unassigned;  
  end;                  
end; 

Upvotes: 2

Stijn Sanders
Stijn Sanders

Reputation: 36840

An alternative is calling the WinInet functions, which is doable but a lot of work. If you already use COM/ActiveX objects, you could consider using MSXML2's XmlHttpRequest or the WinInet component: see here for an example http://yoy.be/item.asp?i142

Or if you want to show the web-page on screen, you could use the TWebBrowser component: http://yoy.be/item.asp?i598

Upvotes: 1

Rob Kennedy
Rob Kennedy

Reputation: 163247

Use Indy. Create a TIdHTTP object and call its Get method. It returns the Web page source as a string, or places it in a stream that you provide.

Upvotes: 3

Related Questions