在 Java httpclient 中设置用户代理并允许重定向为 true

发布于 2024-08-27 09:02:32 字数 158 浏览 8 评论 0原文

我正在尝试在 Java 中的 HttpClient apache 对象中设置我的用户代理字符串,但我不知道如何执行此操作。

请帮忙!

另外,我试图将重定向启用为 true,但也无法在 HttpClient 对象中找到此选项。

谢谢安迪

I am trying to set my user agent string in the HttpClient apache object in Java but I cannot find out how to do it.

Please help!

Also I am trying to enable redirects to true but also cannot find this option within the HttpClient object.

Thanks

Andy

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦明 2024-09-03 09:02:33

使用 HttpClient 4.0,以下内容对我有用:

import org.apache.http.params.HttpProtocolParams;

HttpClient httpclient = new HttpClient();
HttpProtocolParams.setUserAgent(httpclient.getParams(), "My fancy UA");

HttpProtocolParams 驻留在 httpcore JAR 文件中: http:// /hc.apache.org/httpcomponents-core/download.html

With HttpClient 4.0, the following worked for me:

import org.apache.http.params.HttpProtocolParams;

HttpClient httpclient = new HttpClient();
HttpProtocolParams.setUserAgent(httpclient.getParams(), "My fancy UA");

HttpProtocolParams resides in the httpcore JAR file: http://hc.apache.org/httpcomponents-core/download.html

傻比既视感 2024-09-03 09:02:33
HttpClient httpclient = new HttpClient();
httpclient.getParams().setParameter(
    HttpMethodParams.USER_AGENT,
    "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2"
);
HttpClient httpclient = new HttpClient();
httpclient.getParams().setParameter(
    HttpMethodParams.USER_AGENT,
    "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.2) Gecko/20100316 Firefox/3.6.2"
);
π浅易 2024-09-03 09:02:33

使用 AndroidHttpClient,并将用户代理作为参数传递给 newInstance:

AndroidHttpClient client = AndroidHttpClient.newInstance(String userAgent);

还有其他充分的理由使用 AndroidHttpClient 而不是原始 HttpClient。

Use AndroidHttpClient, and pass the user agent as a parameter to newInstance:

AndroidHttpClient client = AndroidHttpClient.newInstance(String userAgent);

There are other good reasons to use AndroidHttpClient instead of the raw HttpClient as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文