123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- package com.chinacreator.videoalliance.net.util;
- import java.io.UnsupportedEncodingException;
- import java.net.InetAddress;
- import java.net.NetworkInterface;
- import java.net.SocketException;
- import java.net.URLEncoder;
- import java.util.Enumeration;
- import javax.servlet.http.HttpServletRequest;
- import org.apache.commons.lang.math.NumberUtils;
- import org.apache.log4j.Logger;
- public class RequestUtil {
-
- private static final String ANDROID = ".*(?i)Android.*";
-
- private static final String IPHONE = ".*(?i)iPhone.*";
-
- private static final String PORT = ":port=";
-
- public static String getQueryString(HttpServletRequest request) {
- if("GET".equalsIgnoreCase(request.getMethod())) {
- return request.getQueryString();
- } else {
- Enumeration<String> enumeration = request.getParameterNames();
- StringBuffer buffer = new StringBuffer();
- while(enumeration.hasMoreElements()) {
- String key = enumeration.nextElement();
- String value = request.getParameter(key);
- if("userid".equals(key)) {
- try {
- value = URLEncoder.encode(value, "UTF-8");
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
- }
- buffer.append(key).append("=").append(value);
- if(enumeration.hasMoreElements()) {
- buffer.append("&");
- }
- }
- return buffer.toString();
- }
- }
-
- public static String getIpAddr(HttpServletRequest request) {
- String ip = request.getHeader("X-Real-IP");
- if (inValidateIpAddr(ip)) {
- ip = request.getHeader("X-Forwarded-For");
- }
- if (inValidateIpAddr(ip)) {
- ip = request.getHeader("Proxy-Client-IP");
- }
- if (inValidateIpAddr(ip)) {
- ip = request.getHeader("WL-Proxy-Client-IP");
- }
- if (inValidateIpAddr(ip)) {
- ip = request.getHeader("clientip");
- }
- if (inValidateIpAddr(ip)) {
- ip = request.getRemoteAddr();
- }
- int index = ip.indexOf(",");
- if (index != -1) {
- ip = ip.substring(0, index);
- }
- return ip;
- }
-
- private static boolean inValidateIpAddr(String ip) {
- return ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip) || ip.startsWith("10.") || ip.startsWith("172.") || ip.startsWith("192.") || ip.startsWith("111.206.133.34");
- }
-
- public static String getHostIp() {
- try {
- Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
- while (en.hasMoreElements()) {
- NetworkInterface intf = (NetworkInterface) en.nextElement();
- Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses();
- while (enumIpAddr.hasMoreElements()) {
- InetAddress inetAddress = (InetAddress) enumIpAddr.nextElement();
- if (!inetAddress.isLoopbackAddress() && !inetAddress.isLinkLocalAddress() && inetAddress.isSiteLocalAddress()) {
- return inetAddress.getHostAddress().toString();
- }
- }
- }
- } catch (SocketException e) {
- e.printStackTrace();
- }
- return null;
- }
-
- public static String getUserAgent(HttpServletRequest request) {
- return request.getHeader("user-agent");
- }
-
- public static String getAppType(HttpServletRequest request) {
- String userAgent = getUserAgent(request);
- String appType = null;
- if(userAgent.matches(ANDROID)) {
- appType = "Android";
- } else if(userAgent.matches(IPHONE)) {
- appType = "iPhone";
- }
- return appType;
- }
-
- public static String getBasePath(HttpServletRequest request) {
- String path = request.getContextPath();
- return getDomain(request) + path;
- }
-
- public static String getDomain(HttpServletRequest request) {
- String useragent = request.getHeader("user-agent");
- int port = -1;
- if(useragent != null) {
- int index = useragent.indexOf(PORT);
- if(index != -1) {
- port = NumberUtils.toInt(useragent.substring(index + PORT.length()), -1);
- }
- }
-
- if (port == -1) {
- port = request.getServerPort();
- }
- if(port == 80) {
- return request.getScheme() + "://" + request.getServerName();
- }
- return request.getScheme() + "://" + request.getServerName() + ":" + port;
- }
-
-
- }
|