Whether the Java test URL address can be properly connected
public static int testwsdlconnection (String address) throws Exception {
int status = 404;
try {
url urlobj = new URL (address);
HttpURLConnection OC = (httpurlconnection) urlobj.openconnection ();
Oc.setusecaches (false);
Oc.setconnecttimeout (3000); Set timeout
status = Oc.getresponsecode ()//Request state
if (= = = status) {
//200 is the request address smoothly connected. return
status;
}
catch (Exception e) {
e.printstacktrace ();
throw e;
}
return status;
}
An int is defined, and if 200 is returned, the address can be communicated successfully, if 0 is returned or otherwise it fails.
Let's look at a section of code that is available or open to Java detection URLs.
Package test;
Import java.net.HttpURLConnection;
Import Java.net.URL;
Import Org.apache.log4j.Logger;
public class Cheskurl {
private static Logger Logger = Logger.getlogger (cheskurl.class);
private static URL urlstr;
private static httpurlconnection connection;
private static int state =-1;
private static String succ;
Public synchronized string isconnect (string url) {
int counts = 0;
SUCC = null;
if (url = null | | url.length () <= 0) {return
succ;
}
while (counts < 5) {
try {
urlstr = new URL (URL);
Connection = (httpurlconnection) urlstr.openconnection ();
State = Connection.getresponsecode ();
if (state = =) {
succ = Connection.geturl (). toString ();
break;
catch (Exception ex) {
counts++; Logger.info ("Loop:" + counts);
Continue
}
}
return SUCC
}
}