|
@@ -0,0 +1,307 @@
|
|
|
|
+package com.vnpay.demo.utils;
|
|
|
|
+
|
|
|
|
+import java.io.BufferedOutputStream;
|
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.io.InputStream;
|
|
|
|
+import java.io.OutputStream;
|
|
|
|
+import java.io.OutputStreamWriter;
|
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
|
|
+import java.net.HttpURLConnection;
|
|
|
|
+import java.net.URL;
|
|
|
|
+import java.net.URLConnection;
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
+import java.util.Enumeration;
|
|
|
|
+import java.util.HashMap;
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.Map;
|
|
|
|
+
|
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
|
+
|
|
|
|
+import org.apache.http.HttpEntity;
|
|
|
|
+import org.apache.http.HttpResponse;
|
|
|
|
+import org.apache.http.client.entity.UrlEncodedFormEntity;
|
|
|
|
+import org.apache.http.client.methods.HttpPost;
|
|
|
|
+import org.apache.http.entity.StringEntity;
|
|
|
|
+import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
|
+import org.apache.http.impl.client.HttpClients;
|
|
|
|
+import org.apache.http.message.BasicNameValuePair;
|
|
|
|
+import org.apache.http.util.EntityUtils;
|
|
|
|
+import org.apache.http.util.TextUtils;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * HTTP工具类
|
|
|
|
+ * 用于发送HTTP请求
|
|
|
|
+ *
|
|
|
|
+ * @author VnPay Demo
|
|
|
|
+ */
|
|
|
|
+public class HttpUtil {
|
|
|
|
+
|
|
|
|
+ public static final int ACITON_POST = 0;
|
|
|
|
+ public static final int ACITON_GET = 1;
|
|
|
|
+
|
|
|
|
+ private static final int IO_BUFFER_SIZE = 4 * 1024;
|
|
|
|
+ public static final String NET_TYPE_WIFI = "WIFI";
|
|
|
|
+ public static final String NET_TYPE_MOBILE = "MOBILE";
|
|
|
|
+ public static final String NET_TYPE_NO_NETWORK = "no_network";
|
|
|
|
+ public static final int HTTP_REQUEST_TIMEOUT_MS = 60 * 1000;
|
|
|
|
+ public static final String DEFAULT_CHARSET = "UTF-8";
|
|
|
|
+ public static final String DEFAULT_CONTENTYPE = "text/html";
|
|
|
|
+
|
|
|
|
+ public static String doPostRequest(String url) {
|
|
|
|
+ return doPostRequest(url, null, null, null);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static String doPostRequest(String url, String parameter) {
|
|
|
|
+ return doPostRequest(url, parameter, null, null);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * POST JSON请求
|
|
|
|
+ */
|
|
|
|
+ public static String doPostRequest(String url, String parameter, Map<String, String> header, String contentType) {
|
|
|
|
+ if (TextUtils.isEmpty(url)) {
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ byte[] data = doRequest(url, parameter, null, null, "POST", contentType, header);
|
|
|
|
+ try {
|
|
|
|
+ if (data != null)
|
|
|
|
+ return new String(data, DEFAULT_CHARSET);
|
|
|
|
+ } catch (UnsupportedEncodingException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static String doGetRequest(String url) {
|
|
|
|
+ return doGetRequest(url, null, null, null);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static String doGetRequest(String url, String parameter, Map<String, String> header, String contentType) {
|
|
|
|
+ if (TextUtils.isEmpty(url)) {
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ byte[] data = doRequest(url, parameter, null, null, "GET", contentType, header);
|
|
|
|
+ try {
|
|
|
|
+ if (data != null)
|
|
|
|
+ return new String(data, DEFAULT_CHARSET);
|
|
|
|
+ } catch (UnsupportedEncodingException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * HTTP请求
|
|
|
|
+ */
|
|
|
|
+ public static byte[] doRequest(String url, String parameter, String userAgent, String charset, String requestMethod,
|
|
|
|
+ String contentType, Map<String, String> header) {
|
|
|
|
+ if (TextUtils.isEmpty(url)) {
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ HttpURLConnection connection = null;
|
|
|
|
+ byte[] exceptionByte = null;
|
|
|
|
+ try {
|
|
|
|
+ connection = (HttpURLConnection) getURLConnection(url);
|
|
|
|
+ connection.setDoInput(true);
|
|
|
|
+ connection.setDoOutput(true);
|
|
|
|
+ connection.setRequestMethod(requestMethod);
|
|
|
|
+ if (header != null) {
|
|
|
|
+ for (Map.Entry<String, String> entry : header.entrySet()) {
|
|
|
|
+ connection.setRequestProperty(entry.getKey(), entry.getValue());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ connection.setRequestProperty("Connection", "Keep-Alive");
|
|
|
|
+ connection.setRequestProperty("Charset", TextUtils.isEmpty(charset) ? DEFAULT_CHARSET : charset);
|
|
|
|
+ connection.setRequestProperty("content-type",
|
|
|
|
+ TextUtils.isEmpty(contentType) ? DEFAULT_CONTENTYPE : contentType);
|
|
|
|
+ if (!TextUtils.isEmpty(userAgent))
|
|
|
|
+ connection.setRequestProperty("User-Agent", userAgent);
|
|
|
|
+ connection.setConnectTimeout(HTTP_REQUEST_TIMEOUT_MS);
|
|
|
|
+ connection.setReadTimeout(HTTP_REQUEST_TIMEOUT_MS);
|
|
|
|
+ connection.connect();
|
|
|
|
+ InputStream inputStream = null;
|
|
|
|
+ OutputStream outputStream = null;
|
|
|
|
+ try {
|
|
|
|
+ String exception = "exception";
|
|
|
|
+ exceptionByte = exception.getBytes(DEFAULT_CHARSET);
|
|
|
|
+ if (!TextUtils.isEmpty(parameter)) {
|
|
|
|
+ OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
|
|
|
|
+ out.write(parameter);
|
|
|
|
+ out.close();
|
|
|
|
+ }
|
|
|
|
+ inputStream = connection.getInputStream();
|
|
|
|
+ ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
|
|
|
|
+ outputStream = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE);
|
|
|
|
+ copy(inputStream, outputStream);
|
|
|
|
+ outputStream.flush();
|
|
|
|
+ return dataStream.toByteArray();
|
|
|
|
+ } finally {
|
|
|
|
+ if (inputStream != null) {
|
|
|
|
+ inputStream.close();
|
|
|
|
+ }
|
|
|
|
+ if (outputStream != null) {
|
|
|
|
+ outputStream.close();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ return exceptionByte;
|
|
|
|
+ } finally {
|
|
|
|
+ if (connection != null) {
|
|
|
|
+ connection.disconnect();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ protected static URLConnection getURLConnection(String url) {
|
|
|
|
+ URLConnection connection = null;
|
|
|
|
+ try {
|
|
|
|
+ connection = new URL(url).openConnection();
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ return connection;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ protected static void copy(InputStream in, OutputStream out) throws IOException {
|
|
|
|
+ byte[] b = new byte[IO_BUFFER_SIZE];
|
|
|
|
+ int read;
|
|
|
|
+ while ((read = in.read(b)) != -1) {
|
|
|
|
+ out.write(b, 0, read);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public static String getQueryString(HttpServletRequest request) {
|
|
|
|
+ Map<String, String[]> params = request.getParameterMap();
|
|
|
|
+ String queryString = "";
|
|
|
|
+ for (String key : params.keySet()) {
|
|
|
|
+ String[] values = params.get(key);
|
|
|
|
+ for (int i = 0; i < values.length; i++) {
|
|
|
|
+ String value = values[i];
|
|
|
|
+ queryString += key + "=" + value + "&";
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ // 去掉最后一个字符
|
|
|
|
+ if (!StringUtil.isEmpty(queryString)) {
|
|
|
|
+ queryString = queryString.substring(0, queryString.length() - 1);
|
|
|
|
+ }
|
|
|
|
+ return queryString;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取完整请求路径
|
|
|
|
+ */
|
|
|
|
+ public static String getAddr(HttpServletRequest request) {
|
|
|
|
+ return request.getRequestURL() + "?" + getQueryString(request);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取相对请求路径
|
|
|
|
+ */
|
|
|
|
+ public static String getRelatedAddr(HttpServletRequest request) {
|
|
|
|
+ return request.getRequestURI() + "?" + getQueryString(request);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取客户端IP地址
|
|
|
|
+ */
|
|
|
|
+ public static String getIpAddr(HttpServletRequest request) {
|
|
|
|
+ String ip = request.getHeader("x-forwarded-for");
|
|
|
|
+ if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
|
|
|
+ ip = request.getHeader("Proxy-Client-IP");
|
|
|
|
+ }
|
|
|
|
+ if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
|
|
|
+ ip = request.getHeader("WL-Proxy-Client-IP");
|
|
|
|
+ }
|
|
|
|
+ if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
|
|
|
+ ip = request.getRemoteAddr();
|
|
|
|
+ }
|
|
|
|
+ if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
|
|
|
+ ip = request.getHeader("http_client_ip");
|
|
|
|
+ }
|
|
|
|
+ if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
|
|
|
|
+ ip = request.getHeader("HTTP_X_FORWARDED_FOR");
|
|
|
|
+ }
|
|
|
|
+ // 如果是多级代理,那么取第一个ip为客户ip
|
|
|
|
+ if (ip != null && ip.indexOf(",") != -1) {
|
|
|
|
+ ip = ip.substring(ip.lastIndexOf(",") + 1, ip.length()).trim();
|
|
|
|
+ }
|
|
|
|
+ return ip;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 使用HttpClient发送POST请求(表单方式)
|
|
|
|
+ */
|
|
|
|
+ public static String postByHttpClient(String url, Map<String, String> params) {
|
|
|
|
+ try {
|
|
|
|
+ HttpPost httpPost = new HttpPost(url);
|
|
|
|
+ CloseableHttpClient client = HttpClients.createDefault();
|
|
|
|
+ String respContent = null;
|
|
|
|
+
|
|
|
|
+ // 表单方式
|
|
|
|
+ List<BasicNameValuePair> pairList = new ArrayList<BasicNameValuePair>();
|
|
|
|
+ if(params != null && params.size() > 0) {
|
|
|
|
+ for(String key: params.keySet()) {
|
|
|
|
+ pairList.add(new BasicNameValuePair(key, params.get(key)));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ httpPost.setEntity(new UrlEncodedFormEntity(pairList, "utf-8"));
|
|
|
|
+
|
|
|
|
+ HttpResponse resp = client.execute(httpPost);
|
|
|
|
+ HttpEntity he = resp.getEntity();
|
|
|
|
+ respContent = EntityUtils.toString(he, "UTF-8");
|
|
|
|
+
|
|
|
|
+ return respContent;
|
|
|
|
+ } catch(Exception ex) {
|
|
|
|
+ ex.printStackTrace();
|
|
|
|
+ return "";
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 使用HttpClient发送POST请求(字符串方式)
|
|
|
|
+ */
|
|
|
|
+ public static String postByHttpClient(String url, String params) {
|
|
|
|
+ try {
|
|
|
|
+ HttpPost httpPost = new HttpPost(url);
|
|
|
|
+ CloseableHttpClient client = HttpClients.createDefault();
|
|
|
|
+ String respContent = null;
|
|
|
|
+
|
|
|
|
+ StringEntity entityParams = new StringEntity(params,"utf-8");
|
|
|
|
+ httpPost.setEntity(entityParams);
|
|
|
|
+ httpPost.setHeader("Content-Type", "text/xml;charset=ISO-8859-1");
|
|
|
|
+
|
|
|
|
+ HttpResponse resp = client.execute(httpPost);
|
|
|
|
+ if (resp.getStatusLine().getStatusCode() == 200) {
|
|
|
|
+ HttpEntity he = resp.getEntity();
|
|
|
|
+ respContent = EntityUtils.toString(he, "UTF-8");
|
|
|
|
+ }
|
|
|
|
+ return respContent;
|
|
|
|
+ } catch(Exception ex) {
|
|
|
|
+ ex.printStackTrace();
|
|
|
|
+ return "";
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取所有请求参数
|
|
|
|
+ */
|
|
|
|
+ public static Map<String, String> getAllRequestParam(final HttpServletRequest request) {
|
|
|
|
+ Map<String, String> res = new HashMap<String, String>();
|
|
|
|
+ Enumeration<?> temp = request.getParameterNames();
|
|
|
|
+ if (null != temp) {
|
|
|
|
+ while (temp.hasMoreElements()) {
|
|
|
|
+ String en = (String) temp.nextElement();
|
|
|
|
+ String value = request.getParameter(en);
|
|
|
|
+ res.put(en, value);
|
|
|
|
+ // 在报文上送时,如果字段的值为空,则不上送
|
|
|
|
+ if (null == res.get(en) || "".equals(res.get(en))) {
|
|
|
|
+ res.remove(en);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return res;
|
|
|
|
+ }
|
|
|
|
+}
|