How to set custom User-Agent with apache http client library 4.1?

一曲冷凌霜 提交于 2019-11-28 17:26:22

The line

request.setHeader("User-Agent", "MySuperUserAgent");

is missing. Add it and enjoy.

You can also set a global user agent value instead of per request:

String userAgent = "NewUseAgent/1.0";
HttpClient httpClient = new DefaultHttpClient();
httpclient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, userAgent);

With httpcomponents 4.3 you should use the client builder to set the user agent:

HttpClient httpClient = HttpClients.custom()
                            .setUserAgent("my UserAgent 5.0")
                            .build();

httpClient.execute(new HttpGet("http://www.google.de"));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!