In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you how java determines whether the http address is connected. The content is concise and easy to understand. It will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
The following code looks at java to determine whether the http address is connected
Private boolean isOk (String url) {if (StrUtil.isEmpty (url)) return false; try {URL netUrl = new URL (url); HttpURLConnection connection = (HttpURLConnection) netUrl.openConnection (); connection.setConnectTimeout (3000); / / connection host timeout ms connection.setReadTimeout (3000) / / timeout for reading data from the host ms if (HttpURLConnection.HTTP_OK = = connection.getResponseCode ()) {System.out.println ("Network Unicom!") ; return true;}} catch (IOException e) {log.error ("disconnected", e.getMessage ()); return false;} return false;}
Add: let's take a look at the url.openconnection () setting timeout
System.setProperty ("sun.net.client.defaultConnectTimeout", "30000"); System.setProperty ("sun.net.client.defaultReadTimeout", "30000")
Where: sun.net.client.defaultConnectTimeout: the timeout for connecting to the host (in milliseconds)
Sun.net.client.defaultReadTimeout: timeout for reading data from the host (in milliseconds)
In versions prior to JDK 1.5, network timeouts could only be controlled by setting these two system properties. In 1.5, you can also use the following two methods of URLConnection, the parent class of HttpURLConnection:
SetConnectTimeout: sets the connection host timeout (in milliseconds)
SetReadTimeout: sets the timeout for reading data from the host (in milliseconds)
For example:
HttpURLConnection urlCon = (HttpURLConnection) url.openConnection (); urlCon.setConnectTimeout (30000); urlCon.setReadTimeout (30000); the above is how java determines whether the http address is connected. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.