0 0 Votes

Lazarus: Set User-Agent in TFPHttpClient

Question by Guest | 24/04/2018 at 20:43

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

Now I would like to customize the User Agent, which is used in the header of my request when retrieving a site. Is there a possibility?



Show Profile | Message
0Best Answer
0 Votes

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 to Mozilla/5.0, this would be for example:

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

Altogether the usage can be implemented like that:

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

Of course, instead of Mozilla/5.0, you can also pass any other string of your desire.
25/04/2018 at 10:51

ReplyPositive Negative

Related Topics

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.


Ask your own question or write your own articles on askingbox.com. How to do.