JSP怎么获取本机的公网IP
发布网友
发布时间:2022-04-20 22:43
我来回答
共4个回答
热心网友
时间:2023-07-11 14:41
jsp获得本机的公网ip可以使用jsp中的request对象中的方法获得IP地址,如下:
/ 获得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();//拿到本地IP地址
}
return ip;
}
热心网友
时间:2023-07-11 14:42
方法很多了。下面介绍两种:
1.使用httpclient在线查询ip138;
2.调用系统cmd命令!
热心网友
时间:2023-07-11 14:42
可以在jsp页面里面加上
<iframe src="http://www.ip138.com/ip2city.asp" frameborder="0" scrolling="no" width="100%" height="100%"></iframe>
如果调用cmd命令的话,你的机器是通过猫直接连网的话可以获取到*ip如果你是通过路由器再连到*的话ipconfig命令是获取不到*ip的
热心网友
时间:2023-07-11 14:43
C#版本的:你自己改改
就是访问http://www.ip138.com/ip2city.asp得到您的IP地址
WebRequest request = (WebRequest)WebRequest.Create("http://www.ip138.com/ip2city.asp");
WebResponse response = request.GetResponse();
Stream s = response.GetResponseStream();
byte[] bs = new byte[1024];
int i = s.Read(bs, 0, 1024);
string str = Encoding.Default.GetString(bs, 0, i);
Console.WriteLine(str);
int m = str.IndexOf("[");
int n = str.IndexOf("]");
string ip = str.Substring(m + 1, n - m - 1);
Console.Write(ip);
Console.Read();