在 servlet 中使用 httpclient 4.1 作为代理

Using httpclient 4.1 as a proxy within a servlet

我们正在编写一个 J2EE Java servlet,它作为浏览器客户端和另一个服务器之间的代理。我们在 servlet 中使用 httpclient 4.1 代码,充当浏览器请求和其他服务器调用之间的代理。这是问题的主要部分,httpclient 4.1 将 return 用于下一组请求的 cookie,但它不会 return cookie,因为它们是 returned从服务器。是否可以从httpclient.

中的服务器获取'real''set-cookie'头信息

例如(这里是将在 servlet 级别设置的代码的伪代码):

DefaultHttpClient httpClient = new DefaultHttpClient();
// Connect to another server //
List list = httpClient.getCookieStore().getCookies();

cookie 列表是(我相信)下一组请求所需的 cookie,但不是来自服务器的 cookie 的确切表示。

例如,如果服务器响应:

设置 Cookie:myval=;

...

在getCookies的列表中,列表为空。因为我们正在尝试创建一个代理调用,所以我们不希望列表为空,我们可以发送 myv​​al=;从 servlet 回到浏览器客户端。

是否可以在httpclient 处理响应时注入某种侦听器?或者 httpclient 是否有一个 api 调用所有 returned 'set-cookie' 值?此外,当连接到服务器时,我们可能会遇到重定向,我们也希望在那里收集所有 set-cookie 调用。

编辑:基本上当使用 httpclient 向另一台服务器发出请求时,对 httpclient 的调用包括重定向。是否可以沿重定向路径收集cookie信息。

由于使用 header 设置 cookie,您可以直接从响应中提取 header 并转发它们:

示例使用 http://bing.com 因为它设置了一堆 cookie 而不管用户代理:

HttpGet get = new HttpGet("http://bing.com");
final HttpResponse response = client.execute(get);
for(Header header: response.getHeaders("set-cookie")) {
    System.out.printf("%s: %s%n", header.getName(), header.getValue());
}

输出:

Set-Cookie: _FS=NU=1; domain=.bing.com; path=/
Set-Cookie: _HOP=; domain=.bing.com; path=/
Set-Cookie: _SS=SID=0E33DFF1E74942258D088E964991329D; domain=.bing.com; path=/
Set-Cookie: SRCHD=AF=NOFORM; expires=Fri, 11-Aug-2017 07:53:36 GMT; domain=.bing.com; path=/
Set-Cookie: SRCHUID=V=2&GUID=F9F751F2254D41AEBA076AF7737236E3; expires=Fri, 11-Aug-2017 07:53:36 GMT; path=/
Set-Cookie: SRCHUSR=AUTOREDIR=0&GEOVAR=&DOB=20150812; expires=Fri, 11-Aug-2017 07:53:36 GMT; domain=.bing.com; path=/
Set-Cookie: _EDGE_S=F=1&SID=1F7F788103616B0C246A708002856ABD; path=/; httponly; domain=bing.com
Set-Cookie: _EDGE_V=1; path=/; httponly; expires=Fri, 11-Aug-2017 07:53:36 GMT; domain=bing.com
Set-Cookie: MUID=3702A96D37A16DAA320AA16C36456C90; path=/; expires=Fri, 11-Aug-2017 07:53:36 GMT; domain=bing.com
Set-Cookie: MUIDB=3702A96D37A16DAA320AA16C36456C90; path=/; httponly; expires=Fri, 11-Aug-2017 07:53:36 GMT

此外,由于您 运行 作为代理,完全禁用 cookie 管理可能是个好主意,这样您就不会用来自不同客户端的 cookie 污染请求:

client.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.IGNORE_COOKIES);

更新:

这里有一个 RedirectStrategy 示例,它将所有遇到的 set-cookie header 存储在 HttpContext - HttpContext 对于所有重定向都是相同的,但在 execute 的调用之间不相同 - 然后恢复所有他们对最终的回应:

public class CookieTrackingRedirectStrategy extends DefaultRedirectStrategy {

    @Override
    public boolean isRedirected(final HttpRequest request, final HttpResponse response, final HttpContext context) throws ProtocolException {
        boolean isRedirected = super.isRedirected(request, response, context);
        List<String> allCookies = (List<String>) context.getAttribute("all-cookies");
        if(isRedirected) {
            // Store cookies from this response for future restoration
            if(allCookies == null) {
                allCookies = new ArrayList<>();
                context.setAttribute("all-cookies", allCookies);
            }
            Header[] cookies = response.getHeaders("set-cookie");
            for(Header cookie : cookies) {
                allCookies.add(cookie.getValue());
            }
        } else if(allCookies != null) {
            // Restore all cookies to this response
            for(String cookie : allCookies) {
                response.addHeader("set-cookie", cookie);
            }
        }
        return isRedirected;
    }
}

使用DefaultHttpClient.setRedirectStrategy设置。