URL url = new URL(urlPath); URLConnection con = url.openConnection(); con.setConnectTimeout(connectTimeout); con.setReadTimeout(readTimeout); InputStream in = con.getInputStream();
public FileDownloadUrlConnection(URL url, Configuration configuration) throws IOException { if (configuration != null && configuration.proxy != null) { mConnection = url.openConnection(configuration.proxy); } else { mConnection = url.openConnection(); } if (configuration != null) { if (configuration.readTimeout != null) { mConnection.setReadTimeout(configuration.readTimeout); } if (configuration.connectTimeout != null) { mConnection.setConnectTimeout(configuration.connectTimeout); } } }
try { HttpURLConnection.setFollowRedirects(false); HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection(); con.setRequestMethod("HEAD"); con.setConnectTimeout(5000); //set timeout to 5 seconds return (con.getResponseCode() == HttpURLConnection.HTTP_OK); } catch (java.net.SocketTimeoutException e) { return false; } catch (java.io.IOException e) { return false; }
/** * This method should be used wherever {@link URL#openConnection()} to internet URLs is invoked directly. */ public static URLConnection open(URL url) throws IOException { final ProxyConfiguration p = get(); URLConnection con; if(p==null) { con = url.openConnection(); } else { Proxy proxy = p.createProxy(url.getHost()); con = url.openConnection(proxy); if(p.getUserName()!=null) { // Add an authenticator which provides the credentials for proxy authentication Authenticator.setDefault(p.authenticator); p.jenkins48775workaround(proxy, url); } } if(DEFAULT_CONNECT_TIMEOUT_MILLIS > 0) { con.setConnectTimeout(DEFAULT_CONNECT_TIMEOUT_MILLIS); } if (JenkinsJVM.isJenkinsJVM()) { // this code may run on a slave decorate(con); } return con; }
@Override public Result authenticate(HttpExchange pHttpExchange) { try { URLConnection connection = delegateURL.openConnection(); connection.addRequestProperty("Authorization", pHttpExchange.getRequestHeaders().getFirst("Authorization")); connection.setConnectTimeout(2000); connection.connect(); if (connection instanceof HttpURLConnection) { HttpURLConnection httpConnection = (HttpURLConnection) connection; return httpConnection.getResponseCode() == 200 ? new Success(principalExtractor.extract(connection)) : new Failure(401); } else { return new Failure(401); } } catch (final IOException e) { return prepareFailure(pHttpExchange, "Cannot call delegate url " + delegateURL + ": " + e, 503); } catch (final IllegalArgumentException e) { return prepareFailure(pHttpExchange, "Illegal Argument: " + e, 400); } catch (ParseException e) { return prepareFailure(pHttpExchange, "Invalid JSON response: " + e, 422); } }
System.setProperty("http.keepAlive", "false"); HttpURLConnection conn = (HttpURLConnection) mURL.openConnection(); conn.setUseCaches(false); conn.setRequestProperty("User-Agent", useragent); conn.setConnectTimeout(30000); conn.setDoOutput(true); conn.setDoInput(true); consumer.sign(conn); conn.connect(); InputSource is = new InputSource(conn.getInputStream());
if (request.referer != null) connection.setRequestProperty("Referer", request.referer); //$NON-NLS-1$ connection.setConnectTimeout(CONNECTION_TIMEOUT); connection.setReadTimeout(CONNECTION_TIMEOUT); BufferedInputStream inputStream = new BufferedInputStream(connection.getInputStream(), 8 * 1024); request.saveTile(inputStream); if (log.isDebugEnabled()) {
@Test public void construct_noConfiguration_noAssigned() throws IOException { FileDownloadUrlConnection.Creator creator = new FileDownloadUrlConnection.Creator(); creator.create("http://blog.dreamtobe.cn"); verify(mConnection, times(0)).setConnectTimeout(anyInt()); verify(mConnection, times(0)).setReadTimeout(anyInt()); }
/** * Copies bytes from the URL <code>source</code> to a file * <code>destination</code>. The directories up to <code>destination</code> * will be created if they don't already exist. <code>destination</code> * will be overwritten if it already exists. * * @param source the <code>URL</code> to copy bytes from, must not be {@code null} * @param destination the non-directory <code>File</code> to write bytes to * (possibly overwriting), must not be {@code null} * @param connectionTimeout the number of milliseconds until this method * will timeout if no connection could be established to the <code>source</code> * @param readTimeout the number of milliseconds until this method will * timeout if no data could be read from the <code>source</code> * @throws IOException if <code>source</code> URL cannot be opened * @throws IOException if <code>destination</code> is a directory * @throws IOException if <code>destination</code> cannot be written * @throws IOException if <code>destination</code> needs creating but can't be * @throws IOException if an IO error occurs during copying * @since 2.0 */ public static void copyURLToFile(final URL source, final File destination, final int connectionTimeout, final int readTimeout) throws IOException { final URLConnection connection = source.openConnection(); connection.setConnectTimeout(connectionTimeout); connection.setReadTimeout(readTimeout); copyInputStreamToFile(connection.getInputStream(), destination); }
private URLConnection getConnection(String urlString) throws IOException { URL connURL = new URL(urlString); URLConnection conn = null; // setup the proxy (if we are using one) and open the connect if (setProxyAuthentication()) { conn = connURL.openConnection(m_httpProxy); } else { conn = connURL.openConnection(); } // Set a timeout of 60 seconds for establishing the connection conn.setConnectTimeout(60000); return conn; }
void configUrlConnection() throws IOException { Util.d(TAG, "config connection for " + url); if (configuration != null && configuration.proxy != null) { connection = url.openConnection(configuration.proxy); } else { connection = url.openConnection(); } if (configuration != null) { if (configuration.readTimeout != null) { connection.setReadTimeout(configuration.readTimeout); } if (configuration.connectTimeout != null) { connection.setConnectTimeout(configuration.connectTimeout); } } }
URLConnection conn = url.openConnection(); conn.setConnectTimeout(timeoutMs); conn.setReadTimeout(timeoutMs); in = conn.getInputStream();
@Test public void construct_noConfiguration_noAssigned() throws IOException { DownloadUrlConnection.Factory factory = new DownloadUrlConnection.Factory(); factory.create("https://jacksgong.com"); verify(urlConnection, never()).setConnectTimeout(anyInt()); verify(urlConnection, never()).setReadTimeout(anyInt()); }
public static boolean hasActiveInternetConnection(Context context) { if (isNetworkAvailable(context)) { try { HttpURLConnection urlc = (HttpURLConnection) (new URL("http://www.google.com").openConnection()); urlc.setRequestProperty("User-Agent", "Test"); urlc.setRequestProperty("Connection", "close"); urlc.setConnectTimeout(1500); urlc.connect(); return (urlc.getResponseCode() == 200); } catch (IOException e) { Log.e(LOG_TAG, "Error checking internet connection", e); } } else { Log.d(LOG_TAG, "No network available!"); } return false; }
try { HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection(); con.setRequestMethod("HEAD"); con.setConnectTimeout(5000); //set timeout to 5 seconds return (con.getResponseCode() == HttpURLConnection.HTTP_OK); } catch (java.net.SocketTimeoutException e) { return false; } catch (java.io.IOException e) { return false; }
/** * Ouvre la connection http. * @return Object * @throws IOException Exception de communication */ private URLConnection openConnection() throws IOException { final URLConnection connection = url.openConnection(); connection.setUseCaches(false); if (CONNECTION_TIMEOUT > 0) { connection.setConnectTimeout(CONNECTION_TIMEOUT); } if (READ_TIMEOUT > 0) { connection.setReadTimeout(READ_TIMEOUT); } // grâce à cette propriété, l'application retournera un flux compressé si la taille // dépasse x Ko connection.setRequestProperty("Accept-Encoding", "gzip"); if (headers != null) { for (final Map.Entry<String, String> entry : headers.entrySet()) { connection.setRequestProperty(entry.getKey(), entry.getValue()); } } if (url.getUserInfo() != null) { final String authorization = Base64Coder.encodeString(url.getUserInfo()); connection.setRequestProperty("Authorization", "Basic " + authorization); } return connection; }
@Override public InputStream fetchStatus() throws IOException { final URLConnection con = url.openConnection(); con.setConnectTimeout(1000); con.setReadTimeout(2000); return con.getInputStream(); } }
@Test public void construct_validConfiguration_Assigned() throws IOException { FileDownloadUrlConnection.Creator creator = new FileDownloadUrlConnection.Creator( new FileDownloadUrlConnection.Configuration() .proxy(mProxy) .connectTimeout(1001) .readTimeout(1002) ); creator.create(mURL); verify(mURL).openConnection(mProxy); verify(mConnection).setConnectTimeout(1001); verify(mConnection).setReadTimeout(1002); }
public boolean isConnectedToServer(String url, int timeout) { try{ URL myUrl = new URL(url); URLConnection connection = myUrl.openConnection(); connection.setConnectTimeout(timeout); connection.connect(); return true; } catch (Exception e) { // Handle your exceptions return false; } }
@Test public void construct_validConfiguration_Assigned() throws IOException { DownloadUrlConnection.Factory factory = new DownloadUrlConnection.Factory( new DownloadUrlConnection.Configuration() .proxy(proxy) .connectTimeout(123) .readTimeout(456) ); factory.create(url); verify(url).openConnection(proxy); verify(urlConnection).setConnectTimeout(123); verify(urlConnection).setReadTimeout(456); }