import org.slf4j.Logger; import org.slf4j.LoggerFactory;
import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map;
public class HttpUtil {
private static final Logger log = LoggerFactory.getLogger(HttpUtil.class);
private static final int API_TIMEOUT = 180000;
private static final String POST = "POST";
private static final String GET = "GET";
private static String defaultEncoding = "utf-8";
public static String post(String url, String data, String charset) { return httpAccessJDK(url, data, charset, POST); }
public static String post(String url, Map<String, String> params, String charset) { String paramStr = getHttpRequestParams(url, params, charset); return httpAccessJDK(url, paramStr, charset, POST); }
public static String post(String url, Map<String, String> params) { String paramStr = getHttpRequestParams(url, params, defaultEncoding); return httpAccessJDK(url, paramStr, defaultEncoding, POST); }
public static String post(String url, String charset) { return httpAccessJDK(url, null, charset, POST); }
public static String post(String url) { return httpAccessJDK(url, null, defaultEncoding, POST); }
public static String get(String url, Map<String, String> params, String charset) { String paramStr = getHttpRequestParams(url, params, charset); return httpAccessJDK(url, paramStr, charset, GET); }
public static String get(String url, Map<String, String> params) { String paramStr = getHttpRequestParams(url, params, defaultEncoding); return httpAccessJDK(url, paramStr, defaultEncoding, GET); }
public static String get(String url, String charset) { return httpAccessJDK(url, null, charset, GET); }
public static String get(String url) { return httpAccessJDK(url, null, defaultEncoding, GET); }
public static String getHttpRequestParams(String url, Map<String, String> params, String charset) { if (params == null || params.size() == 0) { return null; } StringBuilder paramSb = new StringBuilder(); if (charset == null) { charset = defaultEncoding; } try { int i = 0, len = params != null ? params.size() : 0; for (Map.Entry<String, String> entry : params.entrySet()) { if (entry.getKey() == null || entry.getValue() == null) { i++; continue; } paramSb.append(URLEncoder.encode(entry.getKey(), charset)).append("=").append(URLEncoder.encode(entry.getValue(), charset)); if (i != len - 1) { paramSb.append("&"); } i++; } } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } return paramSb.toString(); }
private static String httpAccessJDK(String url, String data, String charset, String method) { if (method == null) { method = GET; } if (charset == null) { charset = defaultEncoding; }
StringBuilder content = new StringBuilder(); int sequence = -1;
try { if (data != null && method.equals(GET)) { if (url.indexOf("?") == -1) { url = new StringBuilder(url).append("?").append(data).toString(); } else { url = new StringBuilder(url).append("&").append(data).toString(); } }
if (log.isInfoEnabled()) { sequence = (int) (Math.random() * 500000); log.info("[http](" + sequence + ") " + method.toLowerCase() + ": " + url); if (data != null) { log.info("[http](" + sequence + ") param: " + (data.length() > 100 ? data.substring(0, 100) + "..." : data)); } else { log.info("[http](" + sequence + ") param: " + data); }
}
HttpURLConnection conn = null;
URL getUrl = new URL(url); conn = (HttpURLConnection) getUrl.openConnection(); conn.setConnectTimeout(API_TIMEOUT); conn.setReadTimeout(API_TIMEOUT);
conn.setDoOutput(true); conn.setDoInput(true); conn.setRequestMethod(method); conn.setUseCaches(false); conn.setInstanceFollowRedirects(true);
conn.connect(); if (data != null && method.equals(POST)) { byte[] bdata = data.getBytes(charset); DataOutputStream out = new DataOutputStream(conn.getOutputStream()); out.write(bdata); out.flush(); out.close(); }
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), charset)); String inputLine;
while ((inputLine = reader.readLine()) != null) { content.append(inputLine); } reader.close(); conn.disconnect(); } catch (Exception e) { throw new RuntimeException(e); }
String result = content.toString(); if (log.isInfoEnabled()) { log.info("[http](" + sequence + ") response: " + result); }
return result; }
}
|