【解决】HttpClient:org.apache.http.client.protocol.ResponseProcessCookies

错误:

2019-03-02 19:18:53 WARN org.apache.http.client.protocol.ResponseProcessCookies
  - Invalid cookie header: 
"Set-Cookie: FSSBBIl1UgzbN7N80T=111Wl2a.lZ4ICh0
Path=/; expires=Tue, 27 Feb 2029 11:25:09 GMT". 
Invalid 'expires' attribute: Tue, 27 Feb 2029 11:25:09 GMT

通过这个报错,我们可以看出来是因为解析cookie的expires属性导致的解析异常,从而导致HttpContent无法解析出正确的cookie。进入这个类ResponseProcessCookies打一个断点我们就可以定位到问题所在。可以看到网页返回的cookie与期望解析的格式不一致(多了横线),从而导致解析失败的。
在这里插入图片描述
解决办法:
  Ⅰ.重写cookie解析的方法:原文地址

  通过重写DefaultCookieSpec类的parse方法,将EEE, dd MMM yy HH:mm:ss格式的expires转换为EEE, dd-MMM-yy HH:mm:ss格式再交给DefaultCookieSpec来处理。

  Ⅱ.更换解析cookie的格式(推荐此种方法):原文地址RFC文档

  HttpClient实现的cookie解析肯定也是有规范的,这个规范就是RFC文档。HttpClient实现了cookie的多种解析格式,我们可以切换它的解析版本就可以了。如下是Httpclient实现的RFC版本。

//设置CookieSpecs.STANDARD的cookie解析模式,下面为源码,对应解析格式我给出了备注
CloseableHttpClient httpClient= HttpClients.custom()
        .setDefaultRequestConfig(RequestConfig.custom()
                .setCookieSpec(CookieSpecs.STANDARD).build())
        .build();
/**
 * Standard cookie specifications supported by HttpClient.
 *
 * @since 4.3
 */
public final class CookieSpecs {

    /**
     * The policy that provides high degree of compatibility
     * with common cookie management of popular HTTP agents.
     *
     * @deprecated (4.4) use {link #DEFAULT}.
     */
    @Deprecated
    public static final String BROWSER_COMPATIBILITY = "compatibility";

    /**
     * The Netscape cookie draft compliant policy.
     * Wdy, DD-Mon-YY HH:MM:SS GMT
     */
    public static final String NETSCAPE = "netscape";

    /**
     * The RFC 6265 compliant policy (interoprability profile).
     * EEE, dd MMM yyyy HH:mm:ss zzz
     */
    public static final String STANDARD = "standard";

    /**
     * The RFC 6265 compliant policy (strict profile).
     *EEE, dd MMM yyyy HH:mm:ss zzz
     *EEE, dd-MMM-yy HH:mm:ss zzz
     *EEE MMM d HH:mm:ss yyyy
     * @since 4.4
     */
    public static final String STANDARD_STRICT = "standard-strict";

    /**
     * The default 'best match' policy.
     *
     * @deprecated (4.4) use {link #DEFAULT}.
     */
    @Deprecated
    public static final String BEST_MATCH = "best-match";

    /**
     * The default policy. This policy provides a higher degree of compatibility
     * with common cookie management of popular HTTP agents for non-standard
     * (Netscape style) cookies.
     * EEE, dd-MMM-yy HH:mm:ss z
     */
    public static final String DEFAULT = "default";

    /**
     * The policy that ignores cookies.
     * 不解析cookie
     */
    public static final String IGNORE_COOKIES = "ignoreCookies";

    private CookieSpecs() {
    }
}

posted @ 2019-03-02 20:25  李子君啊  阅读(1749)  评论(0编辑  收藏  举报