Java中的原生post請(qǐng)求方式
1.post請(qǐng)求方式(有參數(shù))
因?yàn)檎?qǐng)求地址為https 需要配置不驗(yàn)證證書(shū)
/** * post請(qǐng)求方式(有參數(shù)) 因?yàn)檎?qǐng)求地址為https 需要配置不驗(yàn)證證書(shū) */ public static String post(String strUrl, Map<String, Object> params) { try { // 添加信任主機(jī) trustAllHosts(); // 創(chuàng)建連接 URL url = new URL(strUrl); HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); // 不驗(yàn)證配置 connection.setHostnameVerifier(DO_NOT_VERIFY); // 設(shè)置請(qǐng)求方式 connection.setRequestMethod("POST"); connection.setRequestProperty("accept","*/*"); connection.setRequestProperty("connection","Keep-Alive"); connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); // 設(shè)置發(fā)送數(shù)據(jù)的格式j(luò)son connection.setRequestProperty("Content-Type","application/json"); // 設(shè)置token appkey connection.setRequestProperty("apikey","Z31jdnojkYFg3z3mXourHjP5gmpg9Ms9"); // 設(shè)置接收數(shù)據(jù)的格式j(luò)son connection.setRequestProperty("Accept", "application/json"); connection.setDoOutput(true); connection.setDoInput(true); connection.setUseCaches(false); connection.setInstanceFollowRedirects(true); OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), StandardCharsets.UTF_8); // 阿里巴巴的fastjson out.append(JSON.toJSONString(params)); out.flush(); out.close(); // 請(qǐng)求成功 int responseCode = connection.getResponseCode(); if (responseCode == 200) { // 讀取響應(yīng) StringBuffer respResult = new StringBuffer(); BufferedReader reader = new BufferedReader( new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8)); String line; while ((line = reader.readLine()) != null) { respResult.append(line); } reader.close(); log.info("Req Success{}" + respResult.toString()); return respResult.toString(); } } catch (Exception e) { log.info("Req Error{}" + e.getMessage()); return e.getMessage(); } // 請(qǐng)求失敗 return null; }
/** * 不驗(yàn)證配置 */ private final static HostnameVerifier DO_NOT_VERIFY = (hostname, session) -> true; /** * 添加信任主機(jī) */ private static void trustAllHosts() { // 創(chuàng)建不驗(yàn)證證書(shū)鏈的信任管理器 這里使用的是x509證書(shū) 此處的MyX509TrustManager()為實(shí)現(xiàn)重寫(xiě)方法 TrustManager[] trustAllCerts = new TrustManager[]{new MyX509TrustManager() { @Override public java.security.cert.X509Certificate[] getAcceptedIssuers() { return new java.security.cert.X509Certificate[]{}; } @Override public void checkClientTrusted(X509Certificate[] chain, String authType) {} @Override public void checkServerTrusted(X509Certificate[] chain, String authType) {} }}; // 安裝所有信任的信任管理器 try { SSLContext sc = SSLContext.getInstance("TLS"); sc.init(null, trustAllCerts, new java.security.SecureRandom()); //HttpsURLConnection通過(guò)SSLSocket來(lái)建立與HTTPS的安全連接,SSLSocket對(duì)象是由SSLSocketFactory生成的。 HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); } catch (Exception e) { e.printStackTrace(); } }
2.post請(qǐng)求方式(無(wú)參數(shù))
/** * post請(qǐng)求方式(無(wú)參數(shù)) */ public static String post(String strUrl) { try { // 創(chuàng)建連接 URL url = new URL(strUrl); HttpURLConnection connection = (HttpsURLConnection) url.openConnection(); connection.setDoOutput(true); connection.setDoInput(true); connection.setUseCaches(false); connection.setInstanceFollowRedirects(true); // 設(shè)置請(qǐng)求方式 connection.setRequestMethod("POST"); connection.setRequestProperty("Connection", "keep-alive"); // 設(shè)置接收數(shù)據(jù)的格式j(luò)son connection.setRequestProperty("Accept", "application/json"); // connection.setRequestProperty("Content-Type", "application/json"); // 設(shè)置發(fā)送數(shù)據(jù)的格式j(luò)son connection.connect(); // 獲取響應(yīng)碼 int code = connection.getResponseCode(); // 請(qǐng)求成功 if (code == 200) { // 讀取響應(yīng) StringBuffer respResult = new StringBuffer(); BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8)); String line; while ((line = reader.readLine()) != null) { respResult.append(line); } reader.close(); log.info("Req Success{}" + respResult.toString()); return respResult.toString(); } } catch (Exception e) { log.info("Req Error{}" + e.getMessage()); return e.getMessage(); } // 請(qǐng)求失敗 return null; }
import java.security.cert.X509Certificate; import javax.net.ssl.X509TrustManager; /** * 重寫(xiě)的X509TrustManager類 */ public class MyX509TrustManager implements X509TrustManager { @Override public void checkClientTrusted(X509Certificate[] chain, String authType) {} @Override public void checkServerTrusted(X509Certificate[] chain, String authType) {} @Override public X509Certificate[] getAcceptedIssuers() { return null; } }
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
教你如何編寫(xiě)簡(jiǎn)單的網(wǎng)絡(luò)爬蟲(chóng)
實(shí)際的爬蟲(chóng)是從一系列的種子鏈接開(kāi)始。種子鏈接是起始節(jié)點(diǎn),種子頁(yè)面的超鏈接指向的頁(yè)面是子節(jié)點(diǎn)(中間節(jié)點(diǎn)),對(duì)于非html文檔,如excel等,不能從中提取超鏈接,看做圖的終端節(jié)點(diǎn)2013-10-10SpringMVC使用hibernate-validator進(jìn)行參數(shù)校驗(yàn)最佳實(shí)踐記錄
這篇文章主要介紹了SpringMVC使用hibernate-validator進(jìn)行參數(shù)校驗(yàn)最佳實(shí)踐,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-05-05高級(jí)數(shù)據(jù)結(jié)構(gòu)及應(yīng)用之使用bitmap進(jìn)行字符串去重的方法實(shí)例
今天小編就為大家分享一篇關(guān)于高級(jí)數(shù)據(jù)結(jié)構(gòu)及應(yīng)用之使用bitmap進(jìn)行字符串去重的方法實(shí)例,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-02-02Java實(shí)現(xiàn)九宮格的簡(jiǎn)單實(shí)例
這篇文章主要介紹了 Java實(shí)現(xiàn)九宮格的簡(jiǎn)單實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-06-06SpringBoot與SpringCloud的版本對(duì)應(yīng)關(guān)系解讀
本文介紹了SpringBoot與SpringCloud的版本對(duì)應(yīng)關(guān)系,提供了一個(gè)官方的版本對(duì)應(yīng)表,并給出了個(gè)人的一些經(jīng)驗(yàn)總結(jié)2024-12-12Java虛擬機(jī)如何運(yùn)行Java字節(jié)碼
這篇文章主要介紹了Java虛擬機(jī)如何運(yùn)行Java字節(jié)碼的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06java高級(jí)用法之JNA中的回調(diào)問(wèn)題
這篇文章主要介紹了java高級(jí)用法之:JNA中的回調(diào),為了方便和native方法進(jìn)行交互,JNA中同樣提供了Callback用來(lái)進(jìn)行回調(diào),JNA中回調(diào)的本質(zhì)是一個(gè)指向native函數(shù)的指針,通過(guò)這個(gè)指針可以調(diào)用native函數(shù)中的方法,一起來(lái)看看吧2022-05-05Java多線程并發(fā)編程 Volatile關(guān)鍵字
volatile 關(guān)鍵字是一個(gè)神秘的關(guān)鍵字,也許在 J2EE 上的 JAVA 程序員會(huì)了解多一點(diǎn),但在 Android 上的 JAVA 程序員大多不了解這個(gè)關(guān)鍵字。只要稍了解不當(dāng)就好容易導(dǎo)致一些并發(fā)上的錯(cuò)誤發(fā)生,例如好多人把 volatile 理解成變量的鎖2017-05-05