有的平台登陆验证是Http Basic Authentication认证,如下输入用户名、密码完成验证。
如何通过java实现呢,这里仅贴出核心代码。
private CloseableHttpClient createCloseableHttpClientWithBasicAuth(CookieStore cookieStore){
if (httpClientBuilder == null) {
// 创建HttpClientBuilder
httpClientBuilder = HttpClientBuilder.create();
}
// 设置BasicAuth
CredentialsProvider provider = new BasicCredentialsProvider();
// Create the authentication scope
AuthScope scope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM);
// Create credential pair,在此处填写用户名和密码
logger.info(configBean.getKpmUserName());
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(configBean.getKpmUserName(), configBean.getKpmPassWord());
// Inject the credentials
provider.setCredentials(scope, credentials);
// Set the default credentials provider
httpClientBuilder.setDefaultCredentialsProvider(provider);
// Set the default cookieStore
httpClientBuilder.setDefaultCookieStore(cookieStore);
// HttpClient
return httpClientBuilder.build();
}
所依赖的三方库如下:
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.11</version>
</dependency>
实际请求过程中,添加CookieStore时,运行提示异常。
// 设置CookieStore
CookieStore cookieStore = new BasicCookieStore();
BasicClientCookie cookie = new BasicClientCookie("JSESSIONID", jsessionId);
// version4.5 bug fix,should set domain and attribute,otherwise return java.lang.NullPointerException
// cookie.setDomain(".my.domain.com");
// cookie.setAttribute(ClientCookie.DOMAIN_ATTR, "true");
cookieStore.addCookie(cookie);
CloseableHttpClient closeableHttpClient = createCloseableHttpClientWithBasicAuth(cookieStore);
异常信息:
2017-12-23 19:33:14,993 ERROR DtsTaskProcessor-173993-com.amap.chexian.yunxiao.schedulerx.JobDemo-7263912075-2017-12-23 19:33:00-0 -[SimpleTaskProcessor]: process error, instanceId:7263912075 com.alibaba.dts.client.executor.simple.processor.SimpleTaskProcessor?
java.lang.NullPointerException: null
at org.apache.http.impl.cookie.PublicSuffixDomainFilter.match(PublicSuffixDomainFilter.java:93) ~[httpclient-4.5.3.jar:4.5.3]
at org.apache.http.impl.cookie.CookieSpecBase.match(CookieSpecBase.java:136) ~[httpclient-4.5.3.jar:4.5.3]
at org.apache.http.impl.cookie.DefaultCookieSpec.match(DefaultCookieSpec.java:178) ~[httpclient-4.5.3.jar:4.5.3]
at org.apache.http.client.protocol.RequestAddCookies.process(RequestAddCookies.java:166) ~[httpclient-4.5.3.jar:4.5.3]
at org.apache.http.protocol.ImmutableHttpProcessor.process(ImmutableHttpProcessor.java:133) ~[httpcore-4.4.8.jar:4.4.8]
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:183) ~[httpclient-4.5.3.jar:4.5.3]
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89) ~[httpclient-4.5.3.jar:4.5.3]
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:111) ~[httpclient-4.5.3.jar:4.5.3]
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185) ~[httpclient-4.5.3.jar:4.5.3]
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83) ~[httpclient-4.5.3.jar:4.5.3]
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:108) ~[httpclient-4.5.3.jar:4.5.3]
at com.amap.chexian.yunxiao.service.impl.HttpClientServiceImpl.doGetMethod(HttpClientServiceImpl.java:124) ~[classes/:na]
at com.amap.chexian.yunxiao.service.impl.HttpClientServiceImpl.downloadFromKpm(HttpClientServiceImpl.java:54) ~[classes/:na]
at com.amap.chexian.yunxiao.schedulerx.JobDemo.process(JobDemo.java:57) ~[classes/:na]
at com.alibaba.dts.client.executor.simple.processor.SimpleTaskProcessor.executeTask(SimpleTaskProcessor.java:133) ~[schedulerx-client-2.0.15.jar:0.0.2-SNAPSHOT]
at com.alibaba.dts.client.executor.simple.processor.SimpleTaskProcessor.run(SimpleTaskProcessor.java:90) ~[schedulerx-client-2.0.15.jar:0.0.2-SNAPSHOT]
经过查阅,是由于需要设置cookie的Domain及Attribute属性,这个问题是httpclient v4.5里面需要注意的。参考文末的链接。
cookie.setDomain(".my.domain.com");
cookie.setAttribute(ClientCookie.DOMAIN_ATTR, "true");
https://*.com/questions/29970409/cookies-getting-ignored-in-apache-httpclient-4-4