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 > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of "how to use Java native HttpClient". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to use Java native HttpClient" can help you solve the problem.
1. Trust certificate management class package cn.wja.component;import javax.net.ssl.TrustManager;import javax.net.ssl.X509TrustManager;import java.security.cert.X509Certificate;class MyX509TrustManager implements X509TrustManager {@ Override public void checkClientTrusted (X509Certificate [] x509Certificates, String s) {} @ Override public void checkServerTrusted (X509Certificate [] x509Certificates, String s) {} @ Override public X509Certificate [] getAcceptedIssuers () {return null } public static TrustManager [] getTrustManagers () {TrustManager [] trustAllCertificates = {new MyX509TrustManager ()}; return trustAllCertificates;}} 2.HttpClient class package cn.wja.component;import javax.net.ssl.SSLContext;import javax.net.ssl.SSLParameters;import javax.net.ssl.TrustManager;import java.net.http.HttpClient;import java.security.KeyManagementException;import java.security.NoSuchAlgorithmException;import java.security.SecureRandom;import java.time.Duration Public class MyHttpClient {static public HttpClient getClient () throws NoSuchAlgorithmException, KeyManagementException {TrustManager [] trustManagers = MyX509TrustManager.getTrustManagers (); var timeoutInSeconds = 60; SSLParameters sslParams = new SSLParameters (); sslParams.setEndpointIdentificationAlgorithm (""); SSLContext sc = SSLContext.getInstance ("SSL"); / / Unvalidate System.setProperty ("jdk.internal.httpclient.disableHostnameVerification", "true") Sc.init (null, trustManagers, new SecureRandom ()); return HttpClient.newBuilder () .connectTimeout (Duration.ofMillis (timeoutInSeconds * 1000)) .sslContext (sc) .sslParameters (sslParams) .followRedirects (HttpClient.Redirect.NEVER) .version (HttpClient.Version.HTTP_2) .build ();}} 3. Send request utility class package cn.wja.util;import cn.wja.component.MyHttpClient;import java.net.URI;import java.net.http.HttpClient;import java.net.http.HttpRequest;import java.net.http.HttpResponse;import static java.nio.charset.StandardCharsets.UTF_8;public class HttpUtils {public static HttpResponse sendGet (String urlStr, String cookieStr) throws Exception {HashMap hashMap = new HashMap (); hashMap.put ("Cookie", cookieStr); return sendGet (urlStr,hashMap) } public static HttpResponse sendGet (String urlStr, Map headers) throws KeyManagementException, NoSuchAlgorithmException, IOException, InterruptedException {HttpClient client = MyHttpClient.getClient (); HttpRequest.Builder reqBuilder = getReqBuilder (urlStr); reqBuilder.GET (); for (String key:headers.keySet ()) {reqBuilder.header (key, headers.get (key));} HttpRequest request = reqBuilder.build () HttpResponse result = client.send (request, HttpResponse.BodyHandlers.ofString (UTF_8)); return result;} private static HttpResponse sendPost (String contentType, String urlStr, String bodyStr, String cookieStr) throws Exception {HttpClient client = MyHttpClient.getClient (); HttpRequest.Builder reqBuilder = getReqBuilder (urlStr); HttpRequest.BodyPublisher bodyPublisher = HttpRequest.BodyPublishers.ofString (bodyStr) ReqBuilder.header ("Content-Type", contentType) .post (bodyPublisher) .header ("Cookie", cookieStr); HttpRequest request = reqBuilder.build (); HttpResponse result = client.send (request, HttpResponse.BodyHandlers.ofString (UTF_8)); return result } public static HttpResponse sendFormPost (String urlStr, String formStr, String cookieStr) throws Exception {return sendPost ("application/x-www-form-urlencoded;charset=utf-8", urlStr, formStr, cookieStr);} public static HttpResponse sendJsonPost (String urlStr, String jsonStr, String cookieStr) throws Exception {return sendPost ("application/json;charset=utf-8", urlStr, jsonStr, cookieStr) } public static HttpRequest.Builder getReqBuilder (String urlStr) {return HttpRequest.newBuilder () .uri (URI.create (urlStr)) .header ("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:96.0) Gecko/20100101 Firefox/96.0");}} 4. test
Tests show that GET requests and POST requests with Form forms / Json can be sent directly by calling tool class methods. If you want to send other forms of HTTP request, you can also refer to the above code to implement it yourself.
This is the end of the introduction to "how to use Java native HttpClient". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
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.