package com.chinacreator.videoalliance.net.util; import javax.servlet.http.HttpServletRequest; import org.apache.commons.lang.math.NumberUtils; import org.apache.log4j.Logger; public class WebUtil { private static final String PORT = ":port="; private static final String SCHEME = ":scheme="; private static Logger logger = Logger.getLogger("WebUtil.class"); public static String getBasePath(HttpServletRequest request) { String path = request.getContextPath(); String useragent = request.getHeader("user-agent"); int port = -1; String scheme = null; if(useragent != null) { int index = useragent.indexOf(PORT); if(index != -1) { port = NumberUtils.toInt(useragent.substring(index + PORT.length()), -1); } int index1 = useragent.indexOf(SCHEME); if(index1 != -1) { scheme = useragent.substring(index1 + SCHEME.length(),index); } } if (port == -1) { port = request.getServerPort(); } if(scheme == null){ scheme = request.getScheme(); } System.out.println(request.getHeader("User-Agent")); if(request.getServerName().equals("ua1.v.wo.cn")){ port = 443; } String proxy_port = request.getHeader("X-Proxy-Port"); String proxy_host = request.getHeader("X-Proxy-Host"); String proxy_scheme = request.getHeader("X-Proxy-Scheme"); logger.info("X-Proxy-Port ===> " + proxy_port + ", X-Proxy-Host ==> " + proxy_host + ", X-Proxy-Scheme ==>" + proxy_scheme); if(proxy_port != null){ port = NumberUtils.toInt(proxy_port, -1); } if(proxy_scheme != null){ scheme = proxy_scheme; } if(port == 80) { return scheme+"://" + request.getServerName() + path; } return scheme+"://" + request.getServerName() + ":" + port + path; } public static void main(String[] args) { String str = "Mozilla/5.0 (iPhone; CPU iPhone OS 10_0_2 like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) Version/10.0 Mobile/14A456 Safari/602.1:scheme=https://ua1.v.wo.cn:443:port=86"; int index1 = str.indexOf(SCHEME); int index = str.indexOf(PORT); String scheme = str.substring(index1 + SCHEME.length(),index); System.out.println(scheme); } }