11 Vote

Lazarus: Set User-Agent in TFPHttpClient

Question by Guest | Last update on 2022-10-31 | Created on 2018-04-24

I am using the FPHttpClient to download some websites from the Internet with my Lazarus program.

Now I would like to customize the UserAgent, which is used in the header of my request when retrieving a site. Is there any possibility do adjust this?

ReplyPositiveNegative
1Best Answer1 Vote

The TFPHttpClient provides the AddHeader function, which allows you to add any headers you want to your request.

AddHeader takes two parameters, the name of the header and the value. To set the user agent for example to Mozilla/5.0, it would be this:

AddHeader('User-Agent', 'Mozilla/5.0');

Altogether, the call can be implemented like that:

hc := TFPHttpClient.Create(Nil);
try
  hc.AddHeader('User-Agent', 'Mozilla/5.0');
  html := hc.Get(url);
finally
  hc.Free;
end;

Of course, instead of Mozilla/5.0, you can also pass any other string of your desire.
Last update on 2022-10-31 | Created on 2018-04-25

ReplyPositive Negative
Reply

Related Topics

VirtualBox: Change Date and Time

Tutorial | 10 Comments

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.