Reputation: 607
Is it possible with Java code to check if any URL or website is blocked by Network Admin ? Suppose I am logging in by my Gmail credentials to my website. Now if I open the website in a network where Gmail is blocked, how would I tell the user that this website is blocked in your network and so you cannot login from Gmail.
Upvotes: 3
Views: 868
Reputation: 9946
In order to find out if a site is blocked by your network administrator, you need to check the content received :
URL url = new URL("http://www.gmail.com");
URLConnection connection = url.openConnection();
connection.connect();
InputStream is = connection.getInputStream();
byte[] b = new byte[1024];
while(is.available()>0) {
is.read(b);
System.out.println(new String(b));
}
You should analyze the content from input stream and based on that you can decide. I tried it and it gave me the HTML page set by network admin.
If your company using any proxy than you need to set following as well:
System.setProperty("http.proxyHost", "<proxy address>");
System.setProperty("http.proxyPort", "<proxy port>");
Hope this helps
Upvotes: 2
Reputation: 1765
The following piece of code will do what you want , but its a BEST Guess
try {
URL myURL = new URL("http://example.com/");
URLConnection myURLConnection = myURL.openConnection();
myURLConnection.connect();
}
catch (MalformedURLException e) {
// new URL() failed
// ...
}
catch (IOException e) {
// openConnection() failed
// ...
}
Upvotes: 2