Here you can find the source of isUrlAvailable(URL url, Proxy proxy)
Parameter | Description |
---|---|
url | The URL to check. |
proxy | An optional proxy definition. Can be null. |
Parameter | Description |
---|---|
IOException | an exception |
IllegalArgumentException | an exception |
UnsupportedOperationException | an exception |
ProtocolException | an exception |
public static boolean isUrlAvailable(URL url, Proxy proxy) throws IOException, IllegalArgumentException, UnsupportedOperationException, ProtocolException
//package com.java2s; // See LICENSE.txt for license information import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.ProtocolException; import java.net.Proxy; import java.net.URL; import java.net.URLConnection; public class Main { /**//from w ww. j a va2 s . co m * Checks whether the specified URL points to a valid resource. * @param url The URL to check. * @param proxy An optional proxy definition. Can be null. * @return true if the URL is available, false otherwise. * @throws IOException * @throws IllegalArgumentException * @throws UnsupportedOperationException * @throws ProtocolException */ public static boolean isUrlAvailable(URL url, Proxy proxy) throws IOException, IllegalArgumentException, UnsupportedOperationException, ProtocolException { if (url != null) { if (url.getProtocol().equalsIgnoreCase("http") || url.getProtocol().equalsIgnoreCase("https")) { // We only need to check header for HTTP protocol HttpURLConnection.setFollowRedirects(true); HttpURLConnection conn = null; if (proxy != null) { conn = (HttpURLConnection) url.openConnection(proxy); } else { conn = (HttpURLConnection) url.openConnection(); } if (conn != null) { int timeout = conn.getConnectTimeout(); conn.setConnectTimeout(6000); conn.setRequestMethod("HEAD"); int responseCode; try { responseCode = conn.getResponseCode(); conn.setConnectTimeout(timeout); return (responseCode == HttpURLConnection.HTTP_OK); } catch (IOException e) { conn.setConnectTimeout(timeout); } } } else { // more generic method InputStream is = null; URLConnection conn = null; if (proxy != null) { conn = url.openConnection(proxy); } else { conn = url.openConnection(); } if (conn != null) { is = url.openStream(); is.close(); return true; } } } return false; } }