Skip to content

Instantly share code, notes, and snippets.

@c0rp-aubakirov
Last active March 29, 2024 06:50
Show Gist options
  • Save c0rp-aubakirov/a4349cbd187b33138969 to your computer and use it in GitHub Desktop.
Save c0rp-aubakirov/a4349cbd187b33138969 to your computer and use it in GitHub Desktop.
How to get full client info using servlet request, including client IP, browser, os, use-agent and referer
public void printClientInfo(HttpServletRequest request) {
final String referer = getReferer(request);
final String fullURL = getFullURL(request);
final String clientIpAddr = getClientIpAddr(request);
final String clientOS = getClientOS(request);
final String clientBrowser = getClientBrowser(request);
final String userAgent = getUserAgent(request);
logger.info("\n" +
"User Agent \t" + userAgent + "\n" +
"Operating System\t" + clientOS + "\n" +
"Browser Name\t" + clientBrowser + "\n" +
"IP Address\t" + clientIpAddr + "\n" +
"Full URL\t" + fullURL + "\n" +
"Referrer\t" + referer);
}
public String getReferer(HttpServletRequest request) {
final String referer = request.getHeader("referer");
return referer;
}
public String getFullURL(HttpServletRequest request) {
final StringBuffer requestURL = request.getRequestURL();
final String queryString = request.getQueryString();
final String result = queryString == null ? requestURL.toString() : requestURL.append('?')
.append(queryString)
.toString();
return result;
}
//http://stackoverflow.com/a/18030465/1845894
public String getClientIpAddr(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.getHeader("HTTP_CLIENT_IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
//http://stackoverflow.com/a/18030465/1845894
public String getClientOS(HttpServletRequest request) {
final String browserDetails = request.getHeader("User-Agent");
//=================OS=======================
final String lowerCaseBrowser = browserDetails.toLowerCase();
if (lowerCaseBrowser.contains("windows")) {
return "Windows";
} else if (lowerCaseBrowser.contains("mac")) {
return "Mac";
} else if (lowerCaseBrowser.contains("x11")) {
return "Unix";
} else if (lowerCaseBrowser.contains("android")) {
return "Android";
} else if (lowerCaseBrowser.contains("iphone")) {
return "IPhone";
} else {
return "UnKnown, More-Info: " + browserDetails;
}
}
//http://stackoverflow.com/a/18030465/1845894
public String getClientBrowser(HttpServletRequest request) {
final String browserDetails = request.getHeader("User-Agent");
final String user = browserDetails.toLowerCase();
String browser = "";
//===============Browser===========================
if (user.contains("msie")) {
String substring = browserDetails.substring(browserDetails.indexOf("MSIE")).split(";")[0];
browser = substring.split(" ")[0].replace("MSIE", "IE") + "-" + substring.split(" ")[1];
} else if (user.contains("safari") && user.contains("version")) {
browser = (browserDetails.substring(browserDetails.indexOf("Safari")).split(" ")[0]).split(
"/")[0] + "-" + (browserDetails.substring(
browserDetails.indexOf("Version")).split(" ")[0]).split("/")[1];
} else if (user.contains("opr") || user.contains("opera")) {
if (user.contains("opera"))
browser = (browserDetails.substring(browserDetails.indexOf("Opera")).split(" ")[0]).split(
"/")[0] + "-" + (browserDetails.substring(
browserDetails.indexOf("Version")).split(" ")[0]).split("/")[1];
else if (user.contains("opr"))
browser = ((browserDetails.substring(browserDetails.indexOf("OPR")).split(" ")[0]).replace("/",
"-")).replace(
"OPR", "Opera");
} else if (user.contains("chrome")) {
browser = (browserDetails.substring(browserDetails.indexOf("Chrome")).split(" ")[0]).replace("/", "-");
} else if ((user.indexOf("mozilla/7.0") > -1) || (user.indexOf("netscape6") != -1) || (user.indexOf(
"mozilla/4.7") != -1) || (user.indexOf("mozilla/4.78") != -1) || (user.indexOf(
"mozilla/4.08") != -1) || (user.indexOf("mozilla/3") != -1)) {
//browser=(userAgent.substring(userAgent.indexOf("MSIE")).split(" ")[0]).replace("/", "-");
browser = "Netscape-?";
} else if (user.contains("firefox")) {
browser = (browserDetails.substring(browserDetails.indexOf("Firefox")).split(" ")[0]).replace("/", "-");
} else if (user.contains("rv")) {
browser = "IE";
} else {
browser = "UnKnown, More-Info: " + browserDetails;
}
return browser;
}
public String getUserAgent(HttpServletRequest request) {
return request.getHeader("User-Agent");
}
@ghemulyanto
Copy link

Nice

@zikozee
Copy link

zikozee commented Sep 27, 2022

Thank you so much

@omarabdennour
Copy link

Thank you so much

@KERROUFIRDAOUS
Copy link

Nice work. Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment