参考链接:
https://blog.csdn.net/LZHH_2008/article/details/127266312
https://www.cnblogs.com/liyuanhong/p/16007750.html
pom 文件引入 HttpClient5
<dependency> <groupId>org.apache.httpcomponents.client5</groupId> <artifactId>httpclient5</artifactId> <version>5.2</version> </dependency>
鉴权
package com.lv.controller.yzsba; import com.alibaba.fastjson.JSONObject; import org.apache.hc.client5.http.ContextBuilder; import org.apache.hc.client5.http.auth.UsernamePasswordCredentials; import org.apache.hc.client5.http.classic.methods.HttpPost; import org.apache.hc.client5.http.impl.auth.CredentialsProviderBuilder; import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; import org.apache.hc.client5.http.impl.classic.HttpClients; import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder; import org.apache.hc.client5.http.io.HttpClientConnectionManager; import org.apache.hc.client5.http.protocol.HttpClientContext; import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory; import org.apache.hc.core5.http.ClassicHttpResponse; import org.apache.hc.core5.http.HttpHost; import org.apache.hc.core5.http.io.entity.EntityUtils; import org.apache.hc.core5.http.message.StatusLine; import org.apache.http.conn.ssl.NoopHostnameVerifier; import org.apache.http.ssl.SSLContextBuilder; import org.apache.http.ssl.TrustStrategy; import javax.net.ssl.SSLContext; import java.security.KeyManagementException; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import static org.apache.hc.client5.http.impl.classic.HttpClients.createDefault; public class Test { public static void main(String[] args) throws Exception { getLoginRes(); } /** 解决ssl报错问题,信任所有证书 */ public static CloseableHttpClient createSSLClientDefault() { try { SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() { // 信任所有 @Override public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException { return true; } }).build(); SSLConnectionSocketFactory sslFactory = new SSLConnectionSocketFactory(sslContext, new NoopHostnameVerifier()); HttpClientConnectionManager connMgr = PoolingHttpClientConnectionManagerBuilder.create().setSSLSocketFactory(sslFactory).build(); return HttpClients.custom().setConnectionManager(connMgr).setUserAgent("MyService").build(); } catch (KeyManagementException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (KeyStoreException e) { e.printStackTrace(); } return createDefault(); } /** 获取登陆后的cookie和cookie */ public static JSONObject getLoginRes() throws Exception { try (final CloseableHttpClient httpclient = createSSLClientDefault()) { final HttpHost target = new HttpHost("https", "192.168.0.100", 443); final HttpClientContext localContext = ContextBuilder.create() .useCredentialsProvider(CredentialsProviderBuilder.create() .add(target, new UsernamePasswordCredentials("username", "password".toCharArray())) .build()) .build(); final HttpPost httpPost = new HttpPost("https://192.168.0.100/API/Web/Login"); System.out.println("Executing request " + httpPost.getMethod() + " " + httpPost.getUri()); ClassicHttpResponse res = httpclient.execute(httpPost, localContext, response -> { System.out.println("----------------------------------------"); System.out.println(httpPost + "->" + new StatusLine(response)); EntityUtils.consume(response.getEntity()); return response; }); if (res.getCode() == 200) { String cookie = res.getHeader("Set-Cookie").getValue(); System.out.println(cookie.substring(0, cookie.indexOf(";"))); System.out.println(res.getHeader("X-csrftoken").getValue()); } else { System.out.println(res.getCode() + " " + res.getReasonPhrase()); } } return null; } }
原文地址:http://www.cnblogs.com/lei01/p/16925539.html
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,请务用于商业用途!
3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员!
8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载
声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性