In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.