Opens and returns a connection to the given URL.

@param url   the URL to open a connection to
@return the opened connection
@throws IOException in case of I/O errors
196931
private HttpURLConnection openConnection(URL url) throws IOException {
        URLConnection urlConnection = url.openConnection();
        if (!(urlConnection instanceof HttpURLConnection)) {
            throw new IllegalStateException("Connection should be an HttpURLConnection");
        }
        return (HttpURLConnection) urlConnection;
    }
928856
@SuppressWarnings("UnusedReturnValue")
    public static String webread(URL url) throws IOException {
        StringBuilder result = new StringBuilder();
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line;
        while ((line = rd.readLine()) != null) {
            result.append(line);
        }
        rd.close();
        return result.toString();
    }
449348
private InputStream connect(String url) throws IOException {
		URLConnection conn = new URL(URL_BASE + url).openConnection();
		conn.setConnectTimeout(CONNECT_TIMEOUT);
		conn.setReadTimeout(READ_TIMEOUT);
		conn.setRequestProperty("User-Agent", USER_AGENT);
		return conn.getInputStream();
	}
377195
public static synchronized boolean canConnect(String url) {
        boolean result = false;
        HttpURLConnection conn = null;
        try {
            conn = getNormalCon(formatUrl(url));
            conn.connect();

            int resultCode = conn.getResponseCode();
            result = (200 == resultCode);

                /*
            if (!result) {
                Log.d("canConnect", resultCode + "|" + url);
            }
            // */

        } catch (IOException e) {
            result = false;
//            Log.e("canConnect", url + "|error:" + e.toString());
        } finally {
            if (conn != null) {
                conn.disconnect();
            }
        }

        return result;
    }
906003
protected URL extractURL(Object url)
    throws IOException {
    if (!(url instanceof String)) {
      return null;
    }
    return (null == url || "".equals(url)) ? null : new URL( (String) url);
  }
1168590
public static InputStream inputStreamFromURL(URL url) throws IOException
    {
        if ("file".equals(url.getProtocol())) {
            /* As per [WSTX-82], can not do this if the path refers
             * to a network drive on windows. This fixes the problem;
             * might not be needed on all platforms (NFS?), but should not
             * matter a lot: performance penalty of extra wrapping is more
             * relevant when accessing local file system.
             */
            String host = url.getHost();
            if (host == null || host.length() == 0) {
                /* One more test: if there are quoted characters, need
                 * to decoded [WSTX-207]:
                 */
                String path = url.getPath();
                if (path.indexOf('%') >= 0) {
                    path = URLDecoder.decode(path, "UTF-8");
                }
                return new FileInputStream(path);
            }
        }
        return url.openStream();
    }
513168
@Override
	public URL getURL() throws IOException {
		URL url = resolveURL();
		if (url == null) {
			throw new FileNotFoundException(getDescription() + " cannot be resolved to URL because it does not exist");
		}
		return url;
	}
1018919
public static InputStream getInputStreamOfUrl(URL url) throws IOException {
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        return getInputStreamOfConnection(conn);
    }
1003629
public Reader create( URL url ) throws IOException {
        return new InputStreamReader( openStream( url ), StandardCharsets.UTF_8 );
    }
768728
public static JarFile getJarFile(URL url) {
		try {
			JarURLConnection urlConnection = (JarURLConnection) url.openConnection();
			return urlConnection.getJarFile();
		} catch (IOException e) {
			throw new IORuntimeException(e);
		}
	}
934072
static void openURL(String url) {
    try {
      openURL(new URL(url));
    } catch (MalformedURLException ex) {
      Tools.showError(ex);
    }
  }
733504
protected HttpURLConnection createHttpURLConnection(final String url) throws IOException {

    URL sendUrl = new URL(url);

    HttpURLConnection con;

    if (StringUtils.isNotBlank(proxyHost) && proxyPort != null) {

      Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));
      con = (HttpURLConnection) sendUrl.openConnection(proxy);
      if (StringUtils.isNotBlank(proxyUsername)) {
        String passwd = proxyPassword != null ? proxyPassword : "";
        String authValue = proxyUsername + ":" + passwd;
        String headerValue = Base64.encodeBase64String(authValue.getBytes(
            StandardCharsets.UTF_8));
        con.setRequestProperty("Proxy-Authorization", "Basic " + headerValue);
      }

    } else {

      con = (HttpURLConnection) sendUrl.openConnection();
    }

    try {
      if (url.toLowerCase().startsWith("https")) {
        HttpsURLConnection secCon = (HttpsURLConnection) con;
        secCon.setHostnameVerifier(createAllHostnamesVerifier());
        SSLContext sc = SSLContext.getInstance("TLS");
        sc.init(null, createTrustAllManagers(), new SecureRandom());
        secCon.setSSLSocketFactory(sc.getSocketFactory());
      }

    } catch (NoSuchAlgorithmException | KeyManagementException e) {
      throw new IOException(e);
    }

    return con;
  }
272502
private HttpURLConnection getRequest(String method, URL url) throws IOException {
		HttpURLConnection connection;
		if (proxy == null)
			connection = (HttpURLConnection) url.openConnection();
		else
			connection = (HttpURLConnection) url.openConnection(proxy);
		connection.setRequestMethod(method);
		connection.setReadTimeout(config.getReadTimeout());
		connection.setConnectTimeout(config.getConnectTimeout());
		connection.setRequestProperty("Content-Type", "application/json");
		connection.setRequestProperty("X-Ovh-Application", config.getApplicationKey());
		return connection;
	}
220669
public static void openLink(String url) throws IOException, URISyntaxException {
        Desktop.getDesktop().browse(new URI(url));
    }
507418
private HttpURLConnection connect(URL url) throws IOException {
        LOGGER.debug("Searching Artifactory url {}", url);

        // Determine if we need to use a proxy. The rules:
        // 1) If the proxy is set, AND the setting is set to true, use the proxy
        // 2) Otherwise, don't use the proxy (either the proxy isn't configured,
        // or proxy is specifically set to false)
        final URLConnectionFactory factory = new URLConnectionFactory(settings);
        final HttpURLConnection conn = factory.createHttpURLConnection(url, useProxy);
        conn.setDoOutput(true);

        conn.addRequestProperty("X-Result-Detail", "info");

        final String username = settings.getString(Settings.KEYS.ANALYZER_ARTIFACTORY_API_USERNAME);
        final String apiToken = settings.getString(Settings.KEYS.ANALYZER_ARTIFACTORY_API_TOKEN);
        if (username != null && apiToken != null) {
            final String userpassword = username + ":" + apiToken;
            final String encodedAuthorization = Base64.getEncoder().encodeToString(userpassword.getBytes(StandardCharsets.UTF_8));
            conn.addRequestProperty("Authorization", "Basic " + encodedAuthorization);
        } else {
            final String bearerToken = settings.getString(Settings.KEYS.ANALYZER_ARTIFACTORY_BEARER_TOKEN);
            if (bearerToken != null) {
                conn.addRequestProperty("Authorization", "Bearer " + bearerToken);
            }
        }

        conn.connect();
        return conn;
    }
1097662
protected HttpURLConnection openConnection(String url, Map<String, String> header) throws IOException {
		// Initialize connection
		URL urlObj = null;
		HttpURLConnection conn = null;

		int redirectCount = 0;
		boolean redirect = true;
		while (redirect) {
			if (redirectCount > 10) {
				throw new ProtocolException("Too many redirects: " + redirectCount);
			}

			if (conn == null) {
				urlObj = new URL(url);
			} else {
				// get redirect url from "location" header field
				urlObj = new URL(conn.getHeaderField("Location"));
			}

			// open the new connection again
			conn = (HttpURLConnection) urlObj.openConnection();
			conn.setRequestMethod(header.get("method"));
			conn.setInstanceFollowRedirects(true);

			// Set output if PUT or POST
			if (conn.getRequestMethod() == "POST" || conn.getRequestMethod() == "PUT") {
				conn.setDoOutput(true);
			}

			// Set request header
			for (Map.Entry<String, String> headerAttr : header.entrySet()) {
				conn.addRequestProperty(headerAttr.getKey(), headerAttr.getValue());
			}

			// Check if redirect
			redirect = false;
			if (conn.getRequestMethod() == "GET") {
				int status = conn.getResponseCode();
				if (status != HttpURLConnection.HTTP_OK) {
					if (status == HttpURLConnection.HTTP_MOVED_TEMP || status == HttpURLConnection.HTTP_MOVED_PERM || status == HttpURLConnection.HTTP_SEE_OTHER)
						redirect = true;
					redirectCount++;
				}
			}
		}

		return conn;
	}
347179
public static InputStream getStream(URL url) {
		Assert.notNull(url);
		try {
			return url.openStream();
		} catch (IOException e) {
			throw new IORuntimeException(e);
		}
	}