From 73c8388ba2608fabe8030c1f153c5e1d1d1198a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=9C=B0=E5=B9=B3=E7=BA=BF?= <22250530@qq.com> Date: Fri, 11 Sep 2015 12:47:24 +0800 Subject: [PATCH] --- src/com/wentch/redkale/util/Utility.java | 57 +++++++++++++++++------- 1 file changed, 41 insertions(+), 16 deletions(-) diff --git a/src/com/wentch/redkale/util/Utility.java b/src/com/wentch/redkale/util/Utility.java index 82dc11fed..a00844a3b 100644 --- a/src/com/wentch/redkale/util/Utility.java +++ b/src/com/wentch/redkale/util/Utility.java @@ -387,43 +387,68 @@ public final class Utility { } public static String postHttpContent(String url) throws IOException { - return remoteHttpContent(null, "POST", url, null).toString("UTF-8"); + return remoteHttpContent(null, "POST", url, null, null).toString("UTF-8"); } public static String postHttpContent(String url, String body) throws IOException { - return remoteHttpContent(null, "POST", url, body).toString("UTF-8"); - } - - public static String getHttpContent(String url) throws IOException { - return remoteHttpContent(null, "GET", url, null).toString("UTF-8"); - } - - public static byte[] getHttpBytesContent(String url) throws IOException { - return remoteHttpContent(null, "GET", url, null).toByteArray(); + return remoteHttpContent(null, "POST", url, null, body).toString("UTF-8"); } public static String postHttpContent(SSLContext ctx, String url) throws IOException { - return remoteHttpContent(ctx, "POST", url, null).toString("UTF-8"); + return remoteHttpContent(ctx, "POST", url, null, null).toString("UTF-8"); } public static String postHttpContent(SSLContext ctx, String url, String body) throws IOException { - return remoteHttpContent(ctx, "POST", url, body).toString("UTF-8"); + return remoteHttpContent(ctx, "POST", url, null, body).toString("UTF-8"); + } + + public static String postHttpContent(SSLContext ctx, String url, Map headers, String body) throws IOException { + return remoteHttpContent(ctx, "POST", url, headers, body).toString("UTF-8"); + } + + public static byte[] postHttpBytesContent(String url) throws IOException { + return remoteHttpContent(null, "POST", url, null, null).toByteArray(); + } + + public static byte[] postHttpBytesContent(SSLContext ctx, String url) throws IOException { + return remoteHttpContent(ctx, "POST", url, null, null).toByteArray(); + } + + public static byte[] postHttpBytesContent(SSLContext ctx, String url, Map headers, String body) throws IOException { + return remoteHttpContent(ctx, "POST", url, headers, body).toByteArray(); + } + + public static String getHttpContent(String url) throws IOException { + return remoteHttpContent(null, "GET", url, null, null).toString("UTF-8"); } public static String getHttpContent(SSLContext ctx, String url) throws IOException { - return remoteHttpContent(ctx, "GET", url, null).toString("UTF-8"); + return remoteHttpContent(ctx, "GET", url, null, null).toString("UTF-8"); + } + + public static String getHttpContent(SSLContext ctx, String url, Map headers, String body) throws IOException { + return remoteHttpContent(ctx, "GET", url, headers, body).toString("UTF-8"); + } + + public static byte[] getHttpBytesContent(String url) throws IOException { + return remoteHttpContent(null, "GET", url, null, null).toByteArray(); } public static byte[] getHttpBytesContent(SSLContext ctx, String url) throws IOException { - return remoteHttpContent(ctx, "GET", url, null).toByteArray(); + return remoteHttpContent(ctx, "GET", url, null, null).toByteArray(); } - protected static ByteArrayOutputStream remoteHttpContent(SSLContext ctx, String method, String url, String body) throws IOException { + public static byte[] getHttpBytesContent(SSLContext ctx, String url, Map headers, String body) throws IOException { + return remoteHttpContent(ctx, "GET", url, headers, body).toByteArray(); + } + + protected static ByteArrayOutputStream remoteHttpContent(SSLContext ctx, String method, String url, Map headers, String body) throws IOException { HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); conn.setConnectTimeout(3000); conn.setReadTimeout(3000); if (conn instanceof HttpsURLConnection) ((HttpsURLConnection) conn).setSSLSocketFactory((ctx == null ? DEFAULTSSL_CONTEXT : ctx).getSocketFactory()); conn.setRequestMethod(method); + if (headers != null) headers.forEach((x, y) -> conn.setRequestProperty(x, y)); if (body != null) { conn.setDoOutput(true); conn.getOutputStream().write(body.getBytes(UTF_8)); @@ -433,7 +458,7 @@ public final class Utility { if (rs == 301 || rs == 302) { String newurl = conn.getHeaderField("Location"); conn.disconnect(); - return remoteHttpContent(ctx, method, newurl, body); + return remoteHttpContent(ctx, method, newurl, headers, body); } InputStream in = conn.getInputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream(1024);