I want to check ip as format of 'http://192.168.1.xxx',
if the response code='200', I can use javascript to redirect my web site to the ip.
(Exactly, my web site is public ip, and I hope I can check whether I can redirect the user to private ip or not)
I use the code:
var success=""; var error="";
<%
int resboseCode=0;
HttpURLConnection httpUrlConn;
try{
httpUrlConn = (HttpURLConnection) new URL("http://192.168.1.xx").openConnection();
httpUrlConn.setRequestMethod("HEAD");
httpUrlConn.setConnectTimeout(30000);
httpUrlConn.setReadTimeout(30000);
resboseCode=httpUrlConn.getResponseCode();
%>
sccess='<%= resboseCode %>';
<%
}catch(Exception e){
%>
error="<%= e.getMessage() %>";
<%
}
%>
When I use url="http://www.google.com", it works fine, but it will timeout whereas url="http://192.168.1.xxx".
Do I misunderstand what is url or it's different kind of method to get response code when check the format of 'http://192.168.1.xxx'?
I search internet and find another way is sending socket, like isReachable
, but the result seems don't like what I want?
Is there another direction for me to do the task?
Any suggestion will be appreciated.
As far as I understand, what you are asking for is not possible.
There is simply no way for a webserver on a public IP address to contact a private IP address. That's the point of private IP addresses: traffic cannot be routed to them over the public internet.
Private IP addresses are used in many networks such as office intranets and home networks. As these networks are all separated from one another, the same IP address can be used for different devices in different networks without any problems. There could be thousands of devices that have the 192.168.1.xxx address you specify: which one should the request be sent to?
Your office network may know how to route traffic from your desk computer to a 192.168.1.xxx address, but the webserver will not, unless it is also on the same network.