如何使用 apache http 客户端库 4.1 设置自定义用户代理?

发布于 2024-10-18 00:10:45 字数 1652 浏览 1 评论 0原文

如何使 HTTPClient 使用自定义 User-Agent 标头?

以下代码提交空用户代理。我缺少什么?

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;

public class TestHTTP {

        public static void main(String[] args) throws ClientProtocolException, IOException {
        HttpGet request = new HttpGet("http://tool.keepmeapi.com/echo");

        HttpContext HTTP_CONTEXT = new BasicHttpContext();
        HTTP_CONTEXT.setAttribute(CoreProtocolPNames.USER_AGENT, "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.13) Gecko/20101206 Ubuntu/10.10 (maverick) Firefox/3.6.13");
        request.setHeader("Referer", "http://www.google.com");

        HttpClient httpClient = new DefaultHttpClient();
        HttpResponse response = httpClient.execute(request, HTTP_CONTEXT);

        if (response.getStatusLine().getStatusCode() < 200 || response.getStatusLine().getStatusCode() >= 400) {
            throw new IOException("Got bad response, error code = " + response.getStatusLine().getStatusCode());
        }

        HttpEntity entity = response.getEntity();
        if (entity != null) {
            System.out.println(EntityUtils.toString(entity));
            EntityUtils.consume(entity);
        }
    }

}

How to make HTTPClient use custom User-Agent header?

The following code submits empty user-agent. What am I missing?

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;

public class TestHTTP {

        public static void main(String[] args) throws ClientProtocolException, IOException {
        HttpGet request = new HttpGet("http://tool.keepmeapi.com/echo");

        HttpContext HTTP_CONTEXT = new BasicHttpContext();
        HTTP_CONTEXT.setAttribute(CoreProtocolPNames.USER_AGENT, "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.13) Gecko/20101206 Ubuntu/10.10 (maverick) Firefox/3.6.13");
        request.setHeader("Referer", "http://www.google.com");

        HttpClient httpClient = new DefaultHttpClient();
        HttpResponse response = httpClient.execute(request, HTTP_CONTEXT);

        if (response.getStatusLine().getStatusCode() < 200 || response.getStatusLine().getStatusCode() >= 400) {
            throw new IOException("Got bad response, error code = " + response.getStatusLine().getStatusCode());
        }

        HttpEntity entity = response.getEntity();
        if (entity != null) {
            System.out.println(EntityUtils.toString(entity));
            EntityUtils.consume(entity);
        }
    }

}

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

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

发布评论

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

评论(3

我是男神闪亮亮 2024-10-25 00:10:45

注意: 该解决方案适用于使用旧版 httpcomponents 4.2 及之前版本的用户。

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

缺少 。添加它并享受。

Note: The solution is for users using the old httpcomponents 4.2 and before.

The line

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

is missing. Add it and enjoy.

埋情葬爱 2024-10-25 00:10:45

注意:从 httpcomponents 4.3 开始,此解决方案已弃用

您还可以设置全局用户代理值而不是每个请求:

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

Note: Starting from httpcomponents 4.3 this solution is deprecated.

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);
情话难免假 2024-10-25 00:10:45

对于 httpcomponents 4.3,您应该使用客户端构建器来设置用户代理:

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

httpClient.execute(new HttpGet("http://www.google.de"));

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