您好,登錄后才能下訂單哦!
這篇文章主要介紹“Java原生HttpClient怎么使用”的相關(guān)知識(shí),小編通過實(shí)際案例向大家展示操作過程,操作方法簡(jiǎn)單快捷,實(shí)用性強(qiáng),希望這篇“Java原生HttpClient怎么使用”文章能幫助大家解決問題。
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; } }
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"); //取消主機(jī)名驗(yàn)證 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(); } }
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<String> sendGet(String urlStr, String cookieStr) throws Exception { HashMap<String, String> hashMap = new HashMap<>(); hashMap.put("Cookie",cookieStr); return sendGet(urlStr,hashMap); } public static HttpResponse<String> sendGet(String urlStr, Map<String,String> 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<String> result = client.send(request, HttpResponse.BodyHandlers.ofString(UTF_8)); return result; } private static HttpResponse<String> 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<String> result = client.send(request, HttpResponse.BodyHandlers.ofString(UTF_8)); return result; } public static HttpResponse<String> 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<String> 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"); } }
測(cè)試表明,通過調(diào)用工具類方法,可以直接發(fā)送GET請(qǐng)求和帶Form表單/Json的POST請(qǐng)求。如果您要發(fā)送其他形式的HTTP請(qǐng)求,也可以參照上述代碼自行實(shí)現(xiàn)。
關(guān)于“Java原生HttpClient怎么使用”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí),可以關(guān)注億速云行業(yè)資訊頻道,小編每天都會(huì)為大家更新不同的知識(shí)點(diǎn)。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。