How to implement https with httpclient
How to use httpclient to achieve https, I believe that many inexperienced people do not know what to do. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
Package com.neusoft.sample.opscopereq
Import java.io.InputStream
Import java.nio.charset.Charset
Import java.security.KeyStore
Import java.security.SecureRandom
Import java.security.cert.CertificateFactory
Import java.util.Base64
Import javax.net.ssl.HostnameVerifier
Import javax.net.ssl.SSLContext
Import javax.net.ssl.SSLSession
Import javax.net.ssl.TrustManagerFactory
Import org.apache.http.HttpEntity
Import org.apache.http.client.methods.CloseableHttpResponse
Import org.apache.http.client.methods.HttpPost
Import org.apache.http.entity.StringEntity
Import org.apache.http.impl.client.CloseableHttpClient
Import org.apache.http.impl.client.HttpClients
Import org.apache.http.util.EntityUtils
Import com.alibaba.fastjson.JSONObject
Public class M {
Public static void main (String [] args) throws Exception {
InputStream inputStream = M.class.getClassLoader () .getResourceAsStream ("sssssss.crt")
CertificateFactory certificateFactory = CertificateFactory.getInstance ("X.509")
KeyStore keyStore = KeyStore.getInstance (KeyStore.getDefaultType ())
KeyStore.load (null)
String certificateAlias = Integer.toString (0)
KeyStore.setCertificateEntry (certificateAlias, certificateFactory.generateCertificate (inputStream))
InputStream.close ()
SSLContext sslContext = SSLContext.getInstance ("TLS")
TrustManagerFactory trustManagerFactory = TrustManagerFactory
.getInstance (TrustManagerFactory.getDefaultAlgorithm ())
TrustManagerFactory.init (keyStore)
SslContext.init (null, trustManagerFactory.getTrustManagers (), new SecureRandom ())
CloseableHttpClient client = HttpClients.custom () .setSSLContext (sslContext) .setSSLHostnameVerifier (new HostnameVerifier () {
Public boolean verify (String hostname, SSLSession session) {
/ / TODO Auto-generated method stub
Return true
}
}) .build ()
HttpPost http = new HttpPost ("www.url.com")
String USERNAME = "user"
String PASSWORD = "psw"
String auth = USERNAME + ":" + PASSWORD
Byte [] encodedAuth = Base64.getEncoder () .encode (auth.getBytes (Charset.forName ("US-ASCII")
String AUTH_HEADER_VAL = "Basic" + new String (encodedAuth)
Http.setHeader ("Authorization", AUTH_HEADER_VAL)
JSONObject paramObj = new JSONObject ()
ParamObj.put ("industryCode", "517")
ParamObj.put ("keyWord", "wholesale")
ParamObj.put ("pageNum", 1)
HttpEntity entity=new StringEntity (paramObj.toJSONString (), "application/json;", "utf-8")
Http.setEntity (entity)
CloseableHttpResponse res = client.execute (http)
String aa = EntityUtils.toString (res.getEntity ())
System.out.println (aa)
}
}
Org.apache.httpcomponents
Httpcore
4.4.10
Org.apache.httpcomponents
Httpclient
4.5.6
After reading the above, have you mastered how to implement https using httpclient? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!