Java發(fā)送https請求代碼實(shí)例
更新時間:2019年08月27日 11:28:57 作者:隱語者
這篇文章主要介紹了Java發(fā)送https請求代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
1、前文:通過webService發(fā)送https請求,有兩種版本,一種是攜帶證書驗(yàn)證(比較麻煩),另外一種就是直接忽略證書,本文提供的就是第二種(本人已測試過)
2、最簡易代碼:
import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.Reader; import java.net.MalformedURLException; import java.net.URL; import java.text.SimpleDateFormat; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLSession; @SuppressWarnings("all") public class TestAPI_https { public static void main(String args[]) throws Exception { new TestAPI_https().TestRiQingAPI_SaleOrder(); } public static void TestRiQingAPI_SaleOrder() throws Exception { String postData = getJson(); //String url = "https://*****"; String url = "https://*****"; HttpsURLConnection conn = null; OutputStream out = null; String rsp = null; byte[] byteArray = postData.getBytes("utf-8"); try { URL uri = new URL(url); conn = (HttpsURLConnection) uri.openConnection(); //忽略證書驗(yàn)證--Begin conn.setHostnameVerifier(new TrustAnyHostnameVerifier()); //忽略證書驗(yàn)證--End conn.setRequestMethod("POST"); conn.setDoInput(true); conn.setDoOutput(true); conn.setRequestProperty("Host", uri.getHost()); conn.setRequestProperty("Content-Type", "application/json"); out = conn.getOutputStream(); out.write(byteArray); out.close(); if(conn.getResponseCode()==200) { rsp = getStreamAsString(conn.getInputStream(), "utf-8"); }else { rsp = getStreamAsString(conn.getErrorStream(), "utf-8"); } System.out.println(rsp); } catch (Exception e) { if(null!=out) out.close(); e.printStackTrace(); } } /** * getJson * */ private static String getJson() { return "{" + "\"name\"" + ":" + "\"robo_blogs_zh123\"" + "}"; } private static String getStreamAsString(InputStream stream, String charset) throws IOException { try { Reader reader = new InputStreamReader(stream, charset); StringBuilder response = new StringBuilder(); final char[] buff = new char[1024]; int read = 0; while ((read = reader.read(buff)) > 0) { response.append(buff, 0, read); } return response.toString(); } finally { if (stream != null) { stream.close(); } } } } //定制Verifier class TrustAnyHostnameVerifier implements HostnameVerifier { public boolean verify(String hostname, SSLSession session) { return true; } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
IDEA Maven Mybatis generator 自動生成代碼(實(shí)例講解)
下面小編就為大家分享一篇IDEA Maven Mybatis generator 自動生成代碼的實(shí)例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-12-12shiro與spring?security用自定義異常處理401錯誤
這篇文章主要介紹了shiro與spring?security用自定義異常處理401錯誤,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11若依 MyBatis改為MyBatis-Plus的實(shí)現(xiàn)步驟
本文主要介紹了若依 MyBatis改為MyBatis-Plus的實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-08-08Java使用Maven BOM統(tǒng)一管理版本號的實(shí)現(xiàn)
這篇文章主要介紹了Java使用Maven BOM統(tǒng)一管理版本號的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04GateWay路由規(guī)則與動態(tài)路由詳細(xì)介紹
這篇文章主要介紹了GateWay路由規(guī)則與GateWay動態(tài)路由,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09