java访问https,获取页面或者数据时,需要证书和账号密码的验证。
一 获取CRT证书
获取网站的证书,拿到证书后可能是crt格式,可以使用下面的命令转为p12格式
openssl pkcs12 -export -in Mycert.crt -inkey Mykey.key -out Mycert.p12 -name "Mycert"
Mycert.crt是证书,Mykey.key是生成证书同时生成的key,最终转出Mycert.p12
二 访问https,不需要账号密码
比如访问如下地址
https://hf.anjuke.com/esf-ajax/community/pc/autocomplete?city_id=33&kw=%E9%9D%92%E9%98%B3%E6%96%B0%E6%9D%91&type=3
public static void GetSSLJson4() throws Exception {
final String certFile = "C:/Users/682556/wux-presto.seagate.com.p12";//证书
final String storePass = "presto-seagate";//证书的密码
final String keyPass = "presto-seagate";//key的密码,两者相同
KeyStore keyStore;
try (FileInputStream fis = new FileInputStream(certFile)) {
keyStore = KeyStore.getInstance("PKCS12"); // or "JKS"
keyStore.load(fis, storePass.toCharArray());//装载证书
}
SSLContextBuilder sslContextBuilder = SSLContexts.custom();
sslContextBuilder.loadTrustMaterial(TrustAllStrategy.INSTANCE);
sslContextBuilder.loadKeyMaterial(keyStore, keyPass.toCharArray());
SSLContext sslContext = sslContextBuilder.build();
HttpClientBuilder httpClientBuilder = HttpClients.custom();
httpClientBuilder.setSSLContext(sslContext);
httpClientBuilder.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE);
CloseableHttpClient httpClient = httpClientBuilder.build();
HttpGet get = new HttpGet("https://hf.anjuke.com/esf-ajax/community/pc/autocomplete?city_id=33&kw=%E9%9D%92%E9%98%B3%E6%96%B0%E6%9D%91&type=3");
CloseableHttpResponse response = null;
response = httpClient.execute(get);
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity);
System.out.println(result);
}
运行上面的方法,获得json返回值
{"status":"ok","serverTime":{"year":2023,"month":"JUNE","dayOfWeek":"FRIDAY","dayOfYear":153,"era":"CE","chronology":{"calendarType":"iso8601","id":"ISO"},"leapYear":false,"monthValue":6,"dayOfMonth":2},"data":[{"id":337953,"name":"青阳新村","address":"蜀山-青阳北路-青阳北路31号","type":4,"areaId":3882,"areaName":"蜀山","price":"13818","listName":"qingyangxincun","flag":{"isCommission":"0"}}]}
三 访问https,需要账号密码
以下地址是我内部地址,需要登录输入账号密码
https://wux-presto.seagate.com:31265/v1/query/20230602_070334_01069_r2s92?pretty
https内容json
Java代码
public static void GetSSLJson4() throws Exception {
final String certFile = "C:/Users/682556/wux-presto.seagate.com.p12";//证书
final String storePass = "presto-seagate";//证书的密码
final String keyPass = "presto-seagate";//key的密码,两者相同
KeyStore keyStore;
try (FileInputStream fis = new FileInputStream(certFile)) {
keyStore = KeyStore.getInstance("PKCS12"); // or "JKS"
keyStore.load(fis, storePass.toCharArray());//装载证书
}
SSLContextBuilder sslContextBuilder = SSLContexts.custom();
sslContextBuilder.loadTrustMaterial(TrustAllStrategy.INSTANCE);
sslContextBuilder.loadKeyMaterial(keyStore, keyPass.toCharArray());
SSLContext sslContext = sslContextBuilder.build();
HttpClientBuilder httpClientBuilder = HttpClients.custom();
httpClientBuilder.setSSLContext(sslContext);
httpClientBuilder.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE);
//设置https网页账号密码
CredentialsProvider provider = new BasicCredentialsProvider();
provider.setCredentials(new AuthScope("wux-presto.seagate.com",31265), new UsernamePasswordCredentials("账号", "密码"));
CloseableHttpClient httpClient = httpClientBuilder.setDefaultCredentialsProvider(provider).build();
HttpGet get = new HttpGet("https://wux-presto.seagate.com:31265/v1/query/20230602_070334_01069_r2s92?pretty");
CloseableHttpResponse response = null;
response = httpClient.execute(get);
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity,"UTF-8");
System.out.println(result);
}
运行以上程序,返回结果
文章来源:https://www.toymoban.com/news/detail-468806.html
文章来源地址https://www.toymoban.com/news/detail-468806.html
到了这里,关于【java】java访问https并验证账号密码的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!