Java test whether the URL address can be normally connected, javaurl

Source: Internet
Author: User

Java test whether the URL address can be normally connected, javaurl

 

 

1 public static int testWsdlConnection (String address) throws Exception {2 int status = 404; 3 try {4 URL urlObj = new URL (address); 5 HttpURLConnection oc = (HttpURLConnection) urlObj. openConnection (); 6 oc. setUseCaches (false); 7 oc. setConnectTimeout (3000); // set the timeout time to 8 status = oc. getResponseCode (); // Request status 9 if (200 = status) {10 // 200 indicates that the request address is successfully connected .. 11 return status; 12} 13} catch (Exception e) {14 e. printStackTrace (); 15 throw e; 16} 17 return status; 18}

 

An int type is defined. If 200 is returned, the address can be connected successfully. If 0 is returned or other types are returned, the connection fails.

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.