I have an application that requires connection to the Web. Does anyone know how I can check for a connection in my JAVA app so that I know whether or not to instruct the user to make a connection before continuing? Thanks
------------------ Happy Coding, Gregg Bolinger
aditya mahajan
Greenhorn
Joined: Jul 13, 2001
Posts: 8
posted
0
Why do you need to check whether you have a connection ar not. Let the program itsel connect to the web without the user having to instruct it. Or you can ask the user before connecting for his consent a OK - CANCEL type of alert Aditya Mahajan
Originally posted by Gregg Bolinger: I have an application that requires connection to the Web. Does anyone know how I can check for a connection in my JAVA app so that I know whether or not to instruct the user to make a connection before continuing? Thanks
Ravi Verma
Ranch Hand
Joined: Aug 30, 2001
Posts: 42
posted
0
It works in this way, It return code 200 if you are connected to Internet else return code is 401,404 etc., /*********************************************/ import java.net.*; import java.io.*; import javax.swing.*; import java.awt.*; public class SourceViewer { public static void main(String[] args){
try{ URL u = new URL("http://www.yahoo.com/"); System.out.println("Host...."+u.getHost()); HttpURLConnection uc = (HttpURLConnection) u.openConnection(); int code = uc.getResponseCode(); System.out.println("Return Code....."+code); } catch(IOException e){ e.printStackTrace(); System.err.println(e); } } } /********************************************************/
Ravi
David Weitzman
Ranch Hand
Joined: Jul 27, 2001
Posts: 1365
posted
0
Checking for an internet connection can cause at least one problem I know of. When I am offline and the computer set as the gateway is not turned on, it can take a good 20-30 seconds for a connection to fail. Just thought I'd bring up the topic. I don't really know what causes it though.