最近在使用 HttpClient 升级到 4.4+ 之后,发现原来代码无法发送 Cookies 信息了,示例代码如下:
示例程序
Maven 依赖
1 | <dependencies> |
示例代码
1 | import org.apache.http.HttpEntity; |
日式输出
1 | ... |
调试调用链
- execute:82, CloseableHttpClient
- doExecute:184, InternalHttpClient
- execute:110, RedirectExec
- execute:182, ProtocolExec
- process:132, ImmutableHttpProcessor
- process:165, RequestAddCookies
默认的 Cookie 规范
1 | public class DefaultCookieSpec implements CookieSpec { |
NetscapeDraftSpec
BasicDomainHandler
1 |
|
domainMatch
1 | static boolean domainMatch(final String domain, final String host) { |
解决方案
Cookies 增加 额外的属性
1 | CookieStore cookieStore = new BasicCookieStore(); |
Domain 设置成跟主机名一样
1 | CookieStore cookieStore = new BasicCookieStore(); |