HttpURLConnection是JDK自身提供的网络类,不需要引入额外的jar包。文中所使用到的软件版本:Java 1.8.0_191。
1、服务端
参见Java调用Http接口(1)--编写服务端
2、调用Http接口
2.1、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(); } }
2.2、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(); } }
2.3、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(); } }
2.4、上传文件
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(); } }
2.5、上传文件及发送键值对数据
2.5.1、分析数据结构
通过抓包工具分析页面表单上传文件的过程,可以看出传输数据的结构:
2.5.2、根据分析出的数据结构编写代码
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); } }
2.5.3、上传文件方法变通
这种方式上传文件完全是模拟页面的提交行为来编码的,比较繁琐。可以把文件转成字符串,然后通过键值对传给服务端,服务端执行相反的过程来存储文件:
客户端:文件-> 字节数组->Base64字符串
服务端:Base64字符串-> 字节数组->文件
按照这种方式来实现应该比较容易,这里就不演示了。
2.6、下载文件
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); } }
2.7、完整例子
![](http://img.e-com-net.com/image/info8/2b246dbcfe82488397e9aa432bbc4e31.gif)
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(); } }
3、调用Https接口
调用Https接口需要用HttpsURLConnection,与调用Http接口不一样的部分主要在设置ssl部分,下面用GET请求来演示ssl的设置,其他调用方式类似。
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.X509TrustManager; import com.inspur.demo.common.util.FileUtil; /** * 通过HttpsURLConnection调用Https接口 */ public class HttpsURLConnectionCase { public static void main(String[] args) { try { String requestPath = "https://x.x.x.x:9010/x"; //不需客户端证书 HttpsURLConnection connection = getHttpsURLConnection(requestPath, "GET"); String result = new String(getBytesFromInputStream(connection.getInputStream())); System.out.println("GET(不需客户端证书)返回结果:" + result); requestPath = "https://x.x.x.x:9016/zsywservice"; //需客户端证书 connection = getHttpsURLConnection(requestPath, "GET", getkeyStore("d:/client.p12", "123"), "123"); result = new String(getBytesFromInputStream(connection.getInputStream())); System.out.println("GET(需客户端证书)返回结果:" + result); } catch (Exception e) { e.printStackTrace(); } } 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; } } /** * 获取证书 * @return */ private static KeyStore getkeyStore(String filePath, String password) { KeyStore keySotre = null; FileInputStream in = null; try { keySotre = KeyStore.getInstance("PKCS12"); in = new FileInputStream(new File(filePath)); keySotre.load(in, password.toCharArray()); } catch (Exception e) { e.printStackTrace(); } finally { FileUtil.close(in); } return keySotre; } /** * 获取连接 * @param uri * @param method * @param keyStore * @return * @throws Exception */ private static HttpsURLConnection getHttpsURLConnection(String uri, String method, KeyStore keyStore, String password) throws Exception { KeyManager[] keyManagers = null; if (keyStore != null) { KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("SunX509"); keyManagerFactory.init(keyStore, password.toCharArray()); keyManagers = keyManagerFactory.getKeyManagers(); } SSLContext context = SSLContext.getInstance("TLS"); context.init(keyManagers, new TrustManager[] { new DefaultTrustManager() }, null); SSLSocketFactory sslFactory = context.getSocketFactory(); URL url = new URL(uri); HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); connection.setSSLSocketFactory(sslFactory); connection.setHostnameVerifier(new HostnameVerifier() { @Override public boolean verify(String arg0, SSLSession arg1) { return true; } }); connection.setRequestMethod(method); connection.setDoInput(true); connection.setDoOutput(true); return connection; } private static HttpsURLConnection getHttpsURLConnection(String uri, String method) throws Exception { return getHttpsURLConnection(uri, method, null, 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; } }