21ec56f2a156f0a2a2d0641e830e761fb8604a43.svn-base 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package com.chinacreator.videoalliance.net.util;
  2. import java.io.UnsupportedEncodingException;
  3. import java.net.InetAddress;
  4. import java.net.NetworkInterface;
  5. import java.net.SocketException;
  6. import java.net.URLEncoder;
  7. import java.util.Enumeration;
  8. import javax.servlet.http.HttpServletRequest;
  9. import org.apache.commons.lang.math.NumberUtils;
  10. import org.apache.log4j.Logger;
  11. public class RequestUtil {
  12. private static final String ANDROID = ".*(?i)Android.*";
  13. private static final String IPHONE = ".*(?i)iPhone.*";
  14. private static final String PORT = ":port=";
  15. public static String getQueryString(HttpServletRequest request) {
  16. if("GET".equalsIgnoreCase(request.getMethod())) {
  17. return request.getQueryString();
  18. } else {
  19. Enumeration<String> enumeration = request.getParameterNames();
  20. StringBuffer buffer = new StringBuffer();
  21. while(enumeration.hasMoreElements()) {
  22. String key = enumeration.nextElement();
  23. String value = request.getParameter(key);
  24. if("userid".equals(key)) {
  25. try {
  26. value = URLEncoder.encode(value, "UTF-8");
  27. } catch (UnsupportedEncodingException e) {
  28. e.printStackTrace();
  29. }
  30. }
  31. buffer.append(key).append("=").append(value);
  32. if(enumeration.hasMoreElements()) {
  33. buffer.append("&");
  34. }
  35. }
  36. return buffer.toString();
  37. }
  38. }
  39. public static String getIpAddr(HttpServletRequest request) {
  40. String ip = request.getHeader("X-Real-IP");
  41. if (inValidateIpAddr(ip)) {
  42. ip = request.getHeader("X-Forwarded-For");
  43. }
  44. if (inValidateIpAddr(ip)) {
  45. ip = request.getHeader("Proxy-Client-IP");
  46. }
  47. if (inValidateIpAddr(ip)) {
  48. ip = request.getHeader("WL-Proxy-Client-IP");
  49. }
  50. if (inValidateIpAddr(ip)) {
  51. ip = request.getHeader("clientip");
  52. }
  53. if (inValidateIpAddr(ip)) {
  54. ip = request.getRemoteAddr();
  55. }
  56. int index = ip.indexOf(",");
  57. if (index != -1) {
  58. ip = ip.substring(0, index);
  59. }
  60. return ip;
  61. }
  62. private static boolean inValidateIpAddr(String ip) {
  63. return ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip) || ip.startsWith("10.") || ip.startsWith("172.") || ip.startsWith("192.") || ip.startsWith("111.206.133.34");
  64. }
  65. public static String getHostIp() {
  66. try {
  67. Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
  68. while (en.hasMoreElements()) {
  69. NetworkInterface intf = (NetworkInterface) en.nextElement();
  70. Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses();
  71. while (enumIpAddr.hasMoreElements()) {
  72. InetAddress inetAddress = (InetAddress) enumIpAddr.nextElement();
  73. if (!inetAddress.isLoopbackAddress() && !inetAddress.isLinkLocalAddress() && inetAddress.isSiteLocalAddress()) {
  74. return inetAddress.getHostAddress().toString();
  75. }
  76. }
  77. }
  78. } catch (SocketException e) {
  79. e.printStackTrace();
  80. }
  81. return null;
  82. }
  83. public static String getUserAgent(HttpServletRequest request) {
  84. return request.getHeader("user-agent");
  85. }
  86. public static String getAppType(HttpServletRequest request) {
  87. String userAgent = getUserAgent(request);
  88. String appType = null;
  89. if(userAgent.matches(ANDROID)) {
  90. appType = "Android";
  91. } else if(userAgent.matches(IPHONE)) {
  92. appType = "iPhone";
  93. }
  94. return appType;
  95. }
  96. public static String getBasePath(HttpServletRequest request) {
  97. String path = request.getContextPath();
  98. return getDomain(request) + path;
  99. }
  100. public static String getDomain(HttpServletRequest request) {
  101. String useragent = request.getHeader("user-agent");
  102. int port = -1;
  103. if(useragent != null) {
  104. int index = useragent.indexOf(PORT);
  105. if(index != -1) {
  106. port = NumberUtils.toInt(useragent.substring(index + PORT.length()), -1);
  107. }
  108. }
  109. if (port == -1) {
  110. port = request.getServerPort();
  111. }
  112. if(port == 80) {
  113. return request.getScheme() + "://" + request.getServerName();
  114. }
  115. return request.getScheme() + "://" + request.getServerName() + ":" + port;
  116. }
  117. }