Code _java whether the Java test URL address can be properly connected

Source: Internet
Author: User

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
  }
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.