HttpURLConnection是JDK自身提供的網絡類,不須要引入額外的jar包。文中所使用到的軟件版本:Java 1.8.0_191。html
參見Java調用Http接口(1)--編寫服務端 java
public static void get() { try { String requestPath = "http://localhost:8080/demo/httptest/getUser?userId=1000&userName=" + URLEncoder.encode("李白", "utf-8"); URL url = new URL(requestPath); //設置代理 //InetSocketAddress addr = new InetSocketAddress("127.0.0.1", 8888); //Proxy proxy = new Proxy(Proxy.Type.HTTP, addr); //connection = (HttpURLConnection)url.openConnection(proxy); HttpURLConnection connection = (HttpURLConnection)url.openConnection(); connection.setRequestMethod("GET"); connection.connect(); if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { byte[] b = getBytesFromInputStream(connection.getInputStream()); String back = new String(b); System.out.println("GET返回結果:" + back); } else { System.out.println("GET請求狀態碼:" + connection.getResponseCode()); } } catch (Exception e) { e.printStackTrace(); } }
public static void post() { try { String requestPath = "http://localhost:8080/demo/httptest/getUser"; URL url = new URL(requestPath); HttpURLConnection connection = (HttpURLConnection)url.openConnection(); connection.setRequestMethod("POST"); connection.setDoOutput(true); connection.connect(); String param = "userId=1000&userName=李白"; bytesToOutputStream(param.getBytes(), connection.getOutputStream()); if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { byte[] b = getBytesFromInputStream(connection.getInputStream()); String back = new String(b); System.out.println("POST返回結果:" + back); } else { System.out.println("POST返回狀態碼:" + connection.getResponseCode()); } } catch (Exception e) { e.printStackTrace(); } }
public static void post2() { try { String requestPath = "http://localhost:8080/demo/httptest/addUser"; URL url = new URL(requestPath); HttpURLConnection connection = (HttpURLConnection)url.openConnection(); connection.setRequestMethod("POST"); connection.setDoOutput(true); connection.setRequestProperty("Content-type", "application/json"); connection.connect(); String param = "{\"userId\": \"1001\",\"userName\":\"杜甫\"}"; bytesToOutputStream(param.getBytes(), connection.getOutputStream()); if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { byte[] b = getBytesFromInputStream(connection.getInputStream()); String back = new String(b); System.out.println("POST2返回結果:" + back); } else { System.out.println("POST2返回狀態碼:" + connection.getResponseCode()); } } catch (Exception e) { e.printStackTrace(); } }
public static void upload() { try { String requestPath = "http://localhost:8080/demo/httptest/upload"; URL url = new URL(requestPath); HttpURLConnection connection = (HttpURLConnection)url.openConnection(); connection.setRequestMethod("POST"); connection.setDoOutput(true); connection.setRequestProperty("Content-type", "file/*"); connection.connect(); FileInputStream fileInputStream = new FileInputStream("d:/a.jpg"); inputStreamToOutputStream(fileInputStream, connection.getOutputStream()); if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { byte[] b = getBytesFromInputStream(connection.getInputStream()); String back = new String(b); System.out.println("upload返回結果:" + back); } else { System.out.println("upload返回狀態碼:" + connection.getResponseCode()); } } catch (Exception e) { e.printStackTrace(); } }
經過抓包工具分析頁面表單上傳文件的過程,能夠看出傳輸數據的結構:linux
public static void multi() { String BOUNDARY = java.util.UUID.randomUUID().toString(); String TWO_HYPHENS = "--"; String LINE_END = "\r\n"; FileInputStream in = null; try { String requestPath = "http://localhost:8080/demo/httptest/multi"; URL url = new URL(requestPath); HttpURLConnection connection = (HttpURLConnection)url.openConnection(); connection.setRequestMethod("POST"); connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestProperty("Content-type", "multipart/form-data; BOUNDARY=" + BOUNDARY); connection.connect(); StringBuffer sb = new StringBuffer(); //封裝鍵值對數據1 sb.append(TWO_HYPHENS).append(BOUNDARY).append(LINE_END); sb.append("Content-Disposition: form-data; name=\"param1\"").append(LINE_END); sb.append(LINE_END); sb.append("參數1").append(LINE_END); //封裝鍵值對數據2 sb.append(TWO_HYPHENS).append(BOUNDARY).append(LINE_END); sb.append("Content-Disposition: form-data; name=\"param2\"").append(LINE_END); sb.append(LINE_END); sb.append("參數2").append(LINE_END); //封裝文件數據 sb.append(TWO_HYPHENS).append(BOUNDARY).append(LINE_END); sb.append("Content-Disposition: form-data; name=\"file\"; filename=\"a.jpg\"").append(LINE_END); sb.append("Content-Type: file/*").append(LINE_END); sb.append(LINE_END); bytesToOutputStream(sb.toString().getBytes(), connection.getOutputStream()); in = new FileInputStream("d:/a.jpg"); inputStreamToOutputStream(in, connection.getOutputStream()); //寫入標記結束位 String end = (LINE_END + TWO_HYPHENS + BOUNDARY + TWO_HYPHENS + LINE_END); bytesToOutputStream(end.getBytes(), connection.getOutputStream()); if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { byte[] b = getBytesFromInputStream(connection.getInputStream()); String back = new String(b); System.out.println("multi返回結果:" + back); } else { System.out.println("multi返回狀態碼:" + connection.getResponseCode()); } } catch (Exception e) { e.printStackTrace(); } finally { FileUtil.close(in); } }
這種方式上傳文件徹底是模擬頁面的提交行爲來編碼的,比較繁瑣。能夠把文件轉成字符串,而後經過鍵值對傳給服務端,服務端執行相反的過程來存儲文件:json
客戶端:文件-> 字節數組->Base64字符串數組
服務端:Base64字符串-> 字節數組->文件安全
按照這種方式來實現應該比較容易,這裏就不演示了。服務器
public static void download() { FileOutputStream out = null; try { String requestPath = "http://localhost:8080/demo/httptest/download"; URL url = new URL(requestPath); HttpURLConnection connection = (HttpURLConnection)url.openConnection(); connection.setRequestMethod("POST"); connection.connect(); System.out.println("download返回狀態碼:" + connection.getResponseCode()); String contentDisposition = connection.getHeaderField("Content-Disposition");//attachment; filename="a.jpg" String fileName = "none"; if (contentDisposition != null && contentDisposition.indexOf("filename") > -1) { fileName = contentDisposition.substring(contentDisposition.indexOf("\"") + 1, contentDisposition.length() - 1); fileName = java.net.URLDecoder.decode(fileName, "utf-8"); } out = new FileOutputStream("d:/temp/dowload_" + System.currentTimeMillis() + "_" + fileName); inputStreamToOutputStream(connection.getInputStream(), out); } catch (Exception e) { e.printStackTrace(); } finally { FileUtil.close(out); } }
package com.inspur.demo.http.client; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; //import java.net.InetSocketAddress; //import java.net.Proxy; import java.net.URL; import java.net.URLEncoder; import com.inspur.demo.common.util.FileUtil; /** * * 經過HttpURLConnection調用Http接口 * */ public class HttpURLConnectionCase { /** * GET請求 */ public static void get() { try { String requestPath = "http://localhost:8080/demo/httptest/getUser?userId=1000&userName=" + URLEncoder.encode("李白", "utf-8"); URL url = new URL(requestPath); //設置代理 //InetSocketAddress addr = new InetSocketAddress("127.0.0.1", 8888); //Proxy proxy = new Proxy(Proxy.Type.HTTP, addr); //connection = (HttpURLConnection)url.openConnection(proxy); HttpURLConnection connection = (HttpURLConnection)url.openConnection(); connection.setRequestMethod("GET"); connection.connect(); if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { byte[] b = getBytesFromInputStream(connection.getInputStream()); String back = new String(b); System.out.println("GET返回結果:" + back); } else { System.out.println("GET請求狀態碼:" + connection.getResponseCode()); } } catch (Exception e) { e.printStackTrace(); } } /** * POST請求,發送鍵值對數據 */ public static void post() { try { String requestPath = "http://localhost:8080/demo/httptest/getUser"; URL url = new URL(requestPath); HttpURLConnection connection = (HttpURLConnection)url.openConnection(); connection.setRequestMethod("POST"); connection.setDoOutput(true); connection.connect(); String param = "userId=1000&userName=李白"; bytesToOutputStream(param.getBytes(), connection.getOutputStream()); if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { byte[] b = getBytesFromInputStream(connection.getInputStream()); String back = new String(b); System.out.println("POST返回結果:" + back); } else { System.out.println("POST返回狀態碼:" + connection.getResponseCode()); } } catch (Exception e) { e.printStackTrace(); } } /** * POST請求,發送json格式數據 */ public static void post2() { try { String requestPath = "http://localhost:8080/demo/httptest/addUser"; URL url = new URL(requestPath); HttpURLConnection connection = (HttpURLConnection)url.openConnection(); connection.setRequestMethod("POST"); connection.setDoOutput(true); connection.setRequestProperty("Content-type", "application/json"); connection.connect(); String param = "{\"userId\": \"1001\",\"userName\":\"杜甫\"}"; bytesToOutputStream(param.getBytes(), connection.getOutputStream()); if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { byte[] b = getBytesFromInputStream(connection.getInputStream()); String back = new String(b); System.out.println("POST2返回結果:" + back); } else { System.out.println("POST2返回狀態碼:" + connection.getResponseCode()); } } catch (Exception e) { e.printStackTrace(); } } /** * 上傳文件 */ public static void upload() { try { String requestPath = "http://localhost:8080/demo/httptest/upload"; URL url = new URL(requestPath); HttpURLConnection connection = (HttpURLConnection)url.openConnection(); connection.setRequestMethod("POST"); connection.setDoOutput(true); connection.setRequestProperty("Content-type", "file/*"); connection.connect(); FileInputStream fileInputStream = new FileInputStream("d:/a.jpg"); inputStreamToOutputStream(fileInputStream, connection.getOutputStream()); if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { byte[] b = getBytesFromInputStream(connection.getInputStream()); String back = new String(b); System.out.println("upload返回結果:" + back); } else { System.out.println("upload返回狀態碼:" + connection.getResponseCode()); } } catch (Exception e) { e.printStackTrace(); } } /** * 上傳文件及發送鍵值對數據 */ public static void multi() { String BOUNDARY = java.util.UUID.randomUUID().toString(); String TWO_HYPHENS = "--"; String LINE_END = "\r\n"; FileInputStream in = null; try { String requestPath = "http://localhost:8080/demo/httptest/multi"; URL url = new URL(requestPath); HttpURLConnection connection = (HttpURLConnection)url.openConnection(); connection.setRequestMethod("POST"); connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestProperty("Content-type", "multipart/form-data; BOUNDARY=" + BOUNDARY); connection.connect(); StringBuffer sb = new StringBuffer(); //封裝鍵值對數據1 sb.append(TWO_HYPHENS).append(BOUNDARY).append(LINE_END); sb.append("Content-Disposition: form-data; name=\"param1\"").append(LINE_END); sb.append(LINE_END); sb.append("參數1").append(LINE_END); //封裝鍵值對數據2 sb.append(TWO_HYPHENS).append(BOUNDARY).append(LINE_END); sb.append("Content-Disposition: form-data; name=\"param2\"").append(LINE_END); sb.append(LINE_END); sb.append("參數2").append(LINE_END); //封裝文件數據 sb.append(TWO_HYPHENS).append(BOUNDARY).append(LINE_END); sb.append("Content-Disposition: form-data; name=\"file\"; filename=\"a.jpg\"").append(LINE_END); sb.append("Content-Type: file/*").append(LINE_END); sb.append(LINE_END); bytesToOutputStream(sb.toString().getBytes(), connection.getOutputStream()); in = new FileInputStream("d:/a.jpg"); inputStreamToOutputStream(in, connection.getOutputStream()); //寫入標記結束位 String end = (LINE_END + TWO_HYPHENS + BOUNDARY + TWO_HYPHENS + LINE_END); bytesToOutputStream(end.getBytes(), connection.getOutputStream()); if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { byte[] b = getBytesFromInputStream(connection.getInputStream()); String back = new String(b); System.out.println("multi返回結果:" + back); } else { System.out.println("multi返回狀態碼:" + connection.getResponseCode()); } } catch (Exception e) { e.printStackTrace(); } finally { FileUtil.close(in); } } /** * 下載文件 */ public static void download() { FileOutputStream out = null; try { String requestPath = "http://localhost:8080/demo/httptest/download"; URL url = new URL(requestPath); HttpURLConnection connection = (HttpURLConnection)url.openConnection(); connection.setRequestMethod("POST"); connection.connect(); System.out.println("download返回狀態碼:" + connection.getResponseCode()); String contentDisposition = connection.getHeaderField("Content-Disposition");//attachment; filename="a.jpg" String fileName = "none"; if (contentDisposition != null && contentDisposition.indexOf("filename") > -1) { fileName = contentDisposition.substring(contentDisposition.indexOf("\"") + 1, contentDisposition.length() - 1); fileName = java.net.URLDecoder.decode(fileName, "utf-8"); } out = new FileOutputStream("d:/temp/dowload_" + System.currentTimeMillis() + "_" + fileName); inputStreamToOutputStream(connection.getInputStream(), out); } catch (Exception e) { e.printStackTrace(); } finally { FileUtil.close(out); } } /** * 從輸入流獲取數據 * @param in * @return * @throws IOException */ private static byte[] getBytesFromInputStream(InputStream in) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); byte[] b = new byte[1024]; int len; while ((len = in.read(b)) != -1) { out.write(b, 0, len); } byte[] bytes = out.toByteArray(); out.close(); return bytes; } /** * 把字節數組寫入輸出流 * @param bytes * @param out * @throws IOException */ private static void bytesToOutputStream(byte[] bytes, OutputStream out) throws IOException { ByteArrayInputStream in = new ByteArrayInputStream(bytes); byte[] b = new byte[1024]; int len; while ((len = in.read(b)) != -1) { out.write(b, 0, len); } out.flush(); in.close(); } /** * 從輸入流讀取數據並寫入輸出流 * @param out * @param bytes * @throws IOException */ private static void inputStreamToOutputStream(InputStream in, OutputStream out) throws IOException { byte[] b = new byte[1024]; int len; while ((len = in.read(b)) != -1) { out.write(b, 0, len); } out.flush(); } public static void main(String[] args) { get(); post(); post2(); upload(); multi(); download(); } }
調用Https接口須要用HttpsURLConnection,與調用Http接口不同的部分主要在設置ssl部分,下面用GET請求來演示ssl的設置,其餘調用方式相似。網絡
對於自定義證書的網站,需把證書用java自帶的工具keytool導入本地,如導入到d:/temp目錄下session
cd d:/temp
keytool -import -alias aa -keystore cacerts -file aa.cer -trustcacerts
若是當前目錄已有cacerts證書庫,則需輸入密碼;若是沒有則需設置密碼,會在當前目錄下生成cacerts證書庫。數據結構
也能夠不導入,使用默認實現的DefaultTrustManager,信任全部,將致使不安全。
package com.inspur.demo.http.client; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.security.KeyStore; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.KeyManager; import javax.net.ssl.KeyManagerFactory; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSession; import javax.net.ssl.SSLSocketFactory; import javax.net.ssl.TrustManager; import javax.net.ssl.TrustManagerFactory; import javax.net.ssl.X509TrustManager; import com.inspur.demo.common.util.FileUtil; /** * 經過HttpsURLConnection調用Https接口 */ public class HttpsURLConnectionCase { public static void main(String[] args) { try { /* * 請求有權威證書的地址 */ String requestPath = "https://www.baidu.com/"; HttpsURLConnection connection = getHttpsURLConnection(requestPath, "GET"); String result = new String(getBytesFromInputStream(connection.getInputStream())); System.out.println("GET1返回結果:" + result); /* * 請求自定義證書的地址 */ //獲取信任庫 KeyStore trustStore = getkeyStore("jks", "d:/temp/cacerts", "123456"); //不需客戶端證書 requestPath = "https://x.x.x.x:9010/zsywservice"; connection = getHttpsURLConnection(requestPath, "GET", null, null, trustStore); result = new String(getBytesFromInputStream(connection.getInputStream())); System.out.println("GET2返回結果:" + result); //需客戶端證書 requestPath = "https://x.x.x.x:9016/zsywservice"; KeyStore keyStore = getkeyStore("pkcs12", "d:/client.p12", "123456"); connection = getHttpsURLConnection(requestPath, "GET", keyStore, "123456", trustStore); result = new String(getBytesFromInputStream(connection.getInputStream())); System.out.println("GET3返回結果:" + result); } catch (Exception e) { e.printStackTrace(); } } /** * 獲取證書 * @return */ private static KeyStore getkeyStore(String type, String filePath, String password) { KeyStore keySotre = null; FileInputStream in = null; try { keySotre = KeyStore.getInstance(type); in = new FileInputStream(new File(filePath)); keySotre.load(in, password.toCharArray()); } catch (Exception e) { e.printStackTrace(); } finally { FileUtil.close(in); } return keySotre; } private static HttpsURLConnection getHttpsURLConnection(String uri, String method) throws Exception { URL url = new URL(uri); HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); connection.setRequestMethod(method); connection.setDoInput(true); connection.setDoOutput(true); return connection; } /** * 獲取鏈接 * @param uri * @param method * @param keyStore * @param trustStore * @return * @throws Exception */ private static HttpsURLConnection getHttpsURLConnection(String uri, String method, KeyStore keyStore, String keyStorePassword, KeyStore trustStore) throws Exception { KeyManager[] keyManagers = null; TrustManager[] trustManagers = null; if (keyStore != null) { KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("SunX509"); keyManagerFactory.init(keyStore, keyStorePassword.toCharArray()); keyManagers = keyManagerFactory.getKeyManagers(); } if (trustStore != null) { TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance("SunX509"); trustManagerFactory.init(trustStore); trustManagers = trustManagerFactory.getTrustManagers(); } else { trustManagers = new TrustManager[] { new DefaultTrustManager()}; } //設置服務端支持的協議 SSLContext context = SSLContext.getInstance("TLSv1.2"); context.init(keyManagers, trustManagers, null); SSLSocketFactory sslFactory = context.getSocketFactory(); URL url = new URL(uri); HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); connection.setSSLSocketFactory(sslFactory); //驗證URL的主機名和服務器的標識主機名是否匹配 connection.setHostnameVerifier(new HostnameVerifier() { @Override public boolean verify(String hostname, SSLSession session) { //if ("xxx".equals(hostname)) { // return true; //} else { // return false; //} return true; } }); connection.setRequestMethod(method); connection.setDoInput(true); connection.setDoOutput(true); return connection; } /** * 默認的TrustManager實現,不安全 */ private static final class DefaultTrustManager implements X509TrustManager { @Override public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { } @Override public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { } @Override public X509Certificate[] getAcceptedIssuers() { return null; } } /** * 從輸入流獲取數據 * @param in * @return * @throws IOException */ private static byte[] getBytesFromInputStream(InputStream in) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); byte[] b = new byte[1024]; int len; while ((len = in.read(b)) != -1) { out.write(b, 0, len); } byte[] bytes = out.toByteArray(); out.close(); return bytes; } }
出現這種這種問題的一種狀況是因爲客戶端使用的協議,服務端不支持形成的;修改成服務端所支持的協議便可。在getHttpsURLConnection方法中修改:
SSLContext context = SSLContext.getInstance("TLSv1.2");
可選的協議參數有:SSL、SSLv二、SSLv三、TLS、TLSv一、TLSv1.一、TLSv1.2;TLS是SSL標準化的產物,故如今基本只使用TLS相關的參數。
能夠用linux的curl命令來測試服務端支持哪些協議:
curl -k https://x.x.x.x:9016/zsywservice --tlsv1.2 -v
可選協議有:--tlsv一、--tlsv1.0、--tlsv1.一、--tlsv1.二、--tlsv1.3
若是出現以下信息,則表示不支持: