Java Code Examples for java.net.URLConnection#setConnectTimeout()
The following examples show how to use
java.net.URLConnection#setConnectTimeout() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UpdateChecker.java From dragonite-java with Apache License 2.0 | 6 votes |
public String getVersionString(final String productName) { try { final URL url = new URL(URLString); final URLConnection conn = url.openConnection(); conn.setConnectTimeout(TIMEOUT); conn.setReadTimeout(TIMEOUT); try (final BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()))) { String line; while ((line = reader.readLine()) != null) { if (line.contains("-")) { final String[] kv = line.split("-"); if (kv[0].equalsIgnoreCase(productName)) return kv[1]; } } } return null; } catch (final IOException e) { return null; } }
Example 2
Source File: JSONParser.java From open-rmbt with Apache License 2.0 | 6 votes |
public static String readUrl(final URL url) throws IOException { final URLConnection urlConnection = url.openConnection(); try { urlConnection.setConnectTimeout(CONNECT_TIMEOUT); urlConnection.setReadTimeout(READ_TIMEOUT); final StringBuilder stringBuilder = new StringBuilder(); final BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); int read; final char[] chars = new char[1024]; while ((read = reader.read(chars)) != -1) stringBuilder.append(chars, 0, read); return stringBuilder.toString(); } finally { if (urlConnection instanceof HttpURLConnection) ((HttpURLConnection)urlConnection).disconnect(); } }
Example 3
Source File: UrlJwkProvider.java From jwks-rsa-java with MIT License | 6 votes |
private Map<String, Object> getJwks() throws SigningKeyNotFoundException { try { final URLConnection c = this.url.openConnection(); if (connectTimeout != null) { c.setConnectTimeout(connectTimeout); } if (readTimeout != null) { c.setReadTimeout(readTimeout); } c.setRequestProperty("Accept", "application/json"); try (InputStream inputStream = c.getInputStream()) { return reader.readValue(inputStream); } } catch (IOException e) { throw new NetworkException("Cannot obtain jwks from url " + url.toString(), e); } }
Example 4
Source File: NetworkUtil.java From nyzoVerifier with The Unlicense | 6 votes |
public static String stringForUrl(String urlString, int timeoutMilliseconds) { StringBuilder result = new StringBuilder(); try { URL url = new URL(urlString); URLConnection connection = url.openConnection(); connection.setConnectTimeout(timeoutMilliseconds); BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line; while ((line = reader.readLine()) != null) { result.append(line); } reader.close(); } catch (Exception ignored) { } return result.toString(); }
Example 5
Source File: InsecureStreamProvider.java From openshift-ping with Apache License 2.0 | 6 votes |
public URLConnection openConnection(String url, Map<String, String> headers, int connectTimeout, int readTimeout) throws IOException { if (LOGGER.isDebugEnabled()) { LOGGER.debug(String.format( "%s opening connection: url [%s], headers [%s], connectTimeout [%s], readTimeout [%s]", getClass() .getSimpleName(), url, headers, connectTimeout, readTimeout)); } URLConnection connection = new URL(url).openConnection(Proxy.NO_PROXY); if (headers != null) { for (Map.Entry<String, String> entry : headers.entrySet()) { connection.addRequestProperty(entry.getKey(), entry.getValue()); } } if (connectTimeout < 0 || readTimeout < 0) { throw new IllegalArgumentException(String.format( "Neither connectTimeout [%s] nor readTimeout [%s] can be less than 0 for URLConnection.", connectTimeout, readTimeout)); } connection.setConnectTimeout(connectTimeout); connection.setReadTimeout(readTimeout); return connection; }
Example 6
Source File: ExtensionManagerFrame.java From SikuliX1 with MIT License | 6 votes |
private static String html2txt(String urlstring) throws IOException { URL url = new URL(urlstring); URLConnection yc = url.openConnection(); yc.setConnectTimeout(5000); BufferedReader in = new BufferedReader( new InputStreamReader( yc.getInputStream())); String txt = ""; String inputLine; while ((inputLine = in.readLine()) != null) { txt = txt + inputLine; } in.close(); return txt; }
Example 7
Source File: IntentReceiver.java From aptoide-client with GNU General Public License v2.0 | 5 votes |
private void downloadMyappFile(String myappUri) throws Exception { try { URL url = new URL(myappUri); URLConnection connection; if (!myappUri.startsWith("file://")) { connection = url.openConnection(); connection.setReadTimeout(5000); connection.setConnectTimeout(5000); } else { connection = url.openConnection(); } BufferedInputStream getit = new BufferedInputStream(connection.getInputStream(), 1024); File file_teste = new File(TMP_MYAPP_FILE); if (file_teste.exists()) file_teste.delete(); FileOutputStream saveit = new FileOutputStream(TMP_MYAPP_FILE); BufferedOutputStream bout = new BufferedOutputStream(saveit, 1024); byte data[] = new byte[1024]; int readed = getit.read(data, 0, 1024); while (readed != -1) { bout.write(data, 0, readed); readed = getit.read(data, 0, 1024); } bout.close(); getit.close(); saveit.close(); } catch (Exception e) { Logger.printException(e); } }
Example 8
Source File: GetArchivesJob.java From sakai with Educational Community License v2.0 | 5 votes |
@Override public void execute(JobExecutionContext context) throws JobExecutionException { String source = serverConfigurationService.getString("archives.import.source", null); if (source == null) { return; } log.info("Attempting to import archives listed in: "+ source); try { URL url = new URL(source); URLConnection connection = url.openConnection(); connection.setRequestProperty("User-Agent", "Sakai Content Importer"); connection.setConnectTimeout(30000); connection.setReadTimeout(30000); // Now make the connection. connection.connect(); try (InputStream inputStream = connection.getInputStream()) { BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); String line; while ((line = reader.readLine()) != null) { if (!line.isEmpty() && !line.startsWith("#")) { String file = downloadArchive(line); if (file != null) { String siteId = extractSiteId(line); scheduleImport(file, siteId); } } } } } catch (IOException ioe) { log.warn("Problem with "+ source + " "+ ioe.getMessage()); } }
Example 9
Source File: InputStreamUtils.java From eagle with Apache License 2.0 | 5 votes |
private static InputStream openGZIPInputStream(URL url, String auth, int timeout) throws IOException { final URLConnection connection = url.openConnection(); connection.setConnectTimeout(CONNECTION_TIMEOUT); connection.setReadTimeout(timeout); connection.addRequestProperty(GZIP_HTTP_HEADER, GZIP_COMPRESSION); if (null != auth) { connection.setRequestProperty("Authorization", auth); } return new GZIPInputStream(connection.getInputStream()); }
Example 10
Source File: HttpClientUtil.java From charging_pile_cloud with MIT License | 5 votes |
/** * 封装HTTP GET方法 * * @param * @return * @throws Exception */ public static String get(String url) throws Exception { URL u = new URL(url); if("https".equalsIgnoreCase(u.getProtocol())){ SSLUtil.ignoreSsl(); } URLConnection conn = u.openConnection(); conn.setConnectTimeout(3000); conn.setReadTimeout(3000); return IOUtils.toString(conn.getInputStream()); }
Example 11
Source File: FileDownloader.java From HubTurbo with GNU Lesser General Public License v3.0 | 5 votes |
private URLConnection setupConnection(URI source) throws IOException { URLConnection connection = source.toURL().openConnection(); connection.setConnectTimeout(CONNECTION_TIMEOUT); connection.setReadTimeout(READ_CONNECTION_TIMEOUT); return connection; }
Example 12
Source File: SimpleUpdater.java From ChestCommands with GNU General Public License v3.0 | 5 votes |
private Object readJson(String url) throws MalformedURLException, IOException { URLConnection conn = new URL(url).openConnection(); conn.setConnectTimeout(5000); conn.setReadTimeout(8000); conn.addRequestProperty("User-Agent", "Updater (by filoghost)"); conn.setDoOutput(true); return JSONValue.parse(new BufferedReader(new InputStreamReader(conn.getInputStream()))); }
Example 13
Source File: NetUtil.java From MaxKey with Apache License 2.0 | 5 votes |
/** * Open an input stream to a GET(-like) operation on an URL. * * @param url The URL * @return Input stream to the URL connection * @throws IOException If an I/O error occurs */ public static InputStream openGetStream(URL url) throws IOException { URLConnection conn = url.openConnection(); conn.setConnectTimeout(CONNECT_TIMEOUT); conn.setReadTimeout(READ_TIMEOUT); // TODO: User-Agent? return conn.getInputStream(); }
Example 14
Source File: RemoteConfigurationType.java From eclipse-cs with GNU Lesser General Public License v2.1 | 5 votes |
@Override protected byte[] getBytesFromURLConnection(URLConnection connection) throws IOException { byte[] configurationFileData = null; // set timeouts - bug 2941010 connection.setConnectTimeout(10000); connection.setReadTimeout(10000); if (connection instanceof HttpURLConnection) { if (!sFailedWith401URLs.contains(connection.getURL().toString())) { HttpURLConnection httpConn = (HttpURLConnection) connection; httpConn.setInstanceFollowRedirects(true); httpConn.connect(); if (httpConn.getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED) { try { RemoteConfigAuthenticator.removeCachedAuthInfo(connection.getURL()); } catch (CheckstylePluginException e) { CheckstyleLog.log(e); } // add to 401ed URLs sFailedWith401URLs.add(connection.getURL().toString()); throw new IOException(Messages.RemoteConfigurationType_msgUnAuthorized); } } else { // don't retry since we just get another 401 throw new IOException(Messages.RemoteConfigurationType_msgUnAuthorized); } } try (InputStream in = connection.getInputStream()) { configurationFileData = ByteStreams.toByteArray(in); } return configurationFileData; }
Example 15
Source File: UpdateChecker.java From lams with GNU General Public License v2.0 | 5 votes |
private Properties getUpdateProperties(URL updateUrl) throws IOException { URLConnection connection = updateUrl.openConnection(); connection.setConnectTimeout(3 * 1000); InputStream in = connection.getInputStream(); try { Properties props = new Properties(); props.load(connection.getInputStream()); return props; } finally { if (in != null) { in.close(); } } }
Example 16
Source File: Updater.java From askyblock with GNU General Public License v2.0 | 4 votes |
/** * Make a connection to the BukkitDev API and request the newest file's details. * * @return true if successful. */ private boolean read() { try { final URLConnection conn = this.url.openConnection(); conn.setConnectTimeout(5000); if (this.apiKey != null) { conn.addRequestProperty("X-API-Key", this.apiKey); } conn.addRequestProperty("User-Agent", Updater.USER_AGENT); conn.setDoOutput(true); final BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); final String response = reader.readLine(); final JSONArray array = (JSONArray) JSONValue.parse(response); if (array.isEmpty()) { this.plugin.getLogger().warning("The updater could not find any files for the project id " + this.id); this.result = UpdateResult.FAIL_BADID; return false; } JSONObject latestUpdate = (JSONObject) array.get(array.size() - 1); this.versionName = (String) latestUpdate.get(Updater.TITLE_VALUE); this.versionLink = (String) latestUpdate.get(Updater.LINK_VALUE); this.versionType = (String) latestUpdate.get(Updater.TYPE_VALUE); this.versionGameVersion = (String) latestUpdate.get(Updater.VERSION_VALUE); return true; } catch (final IOException e) { if (e.getMessage().contains("HTTP response code: 403")) { this.plugin.getLogger().severe("dev.bukkit.org rejected the API key provided in plugins/Updater/config.yml"); this.plugin.getLogger().severe("Please double-check your configuration to ensure it is correct."); this.result = UpdateResult.FAIL_APIKEY; } else { this.plugin.getLogger().severe("The updater could not contact dev.bukkit.org for updating."); this.plugin.getLogger().severe("If you have not recently modified your configuration and this is the first time you are seeing this message, the site may be experiencing temporary downtime."); this.result = UpdateResult.FAIL_DBO; } this.plugin.getLogger().log(Level.SEVERE, null, e); return false; } }
Example 17
Source File: PluginUpdater.java From BedwarsRel with GNU General Public License v3.0 | 4 votes |
/** * Make a connection to the BukkitDev API and request the newest file's details. * * @return true if successful. */ private boolean read() { try { final URLConnection conn = this.url.openConnection(); conn.setConnectTimeout(5000); if (this.apiKey != null) { conn.addRequestProperty("X-API-Key", this.apiKey); } conn.addRequestProperty("User-Agent", PluginUpdater.USER_AGENT); conn.setDoOutput(true); final BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); final String response = reader.readLine(); final JSONArray array = (JSONArray) JSONValue.parse(response); if (array.isEmpty()) { this.plugin.getLogger() .warning("The updater could not find any files for the project id " + this.id); this.result = UpdateResult.FAIL_BADID; return false; } JSONObject latestUpdate = (JSONObject) array.get(array.size() - 1); this.versionName = (String) latestUpdate.get(PluginUpdater.TITLE_VALUE); this.versionLink = (String) latestUpdate.get(PluginUpdater.LINK_VALUE); this.versionType = (String) latestUpdate.get(PluginUpdater.TYPE_VALUE); this.versionGameVersion = (String) latestUpdate.get(PluginUpdater.VERSION_VALUE); this.versionCustom = this.getCustomVersion(); return true; } catch (final IOException e) { if (e.getMessage().contains("HTTP response code: 403")) { this.plugin.getLogger() .severe("dev.bukkit.org rejected the API key provided in plugins/updater/config.yml"); this.plugin.getLogger() .severe("Please double-check your configuration to ensure it is correct."); this.result = UpdateResult.FAIL_APIKEY; } else { this.plugin.getLogger() .severe("The updater could not contact dev.bukkit.org for updating."); this.plugin.getLogger().severe( "If you have not recently modified your configuration and this is the first time you are seeing this message, the site may be experiencing temporary downtime."); this.result = UpdateResult.FAIL_DBO; } this.plugin.getLogger().log(Level.SEVERE, null, e); return false; } }
Example 18
Source File: HessianURLConnectionFactory.java From sofa-hessian with Apache License 2.0 | 4 votes |
/** * Opens a new or recycled connection to the HTTP server. */ public HessianConnection open(URL url) throws IOException { if (log.isLoggable(Level.FINER)) log.finer(this + " open(" + url + ")"); URLConnection conn = url.openConnection(); // HttpURLConnection httpConn = (HttpURLConnection) conn; // httpConn.setRequestMethod("POST"); // conn.setDoInput(true); long connectTimeout = _proxyFactory.getConnectTimeout(); if (connectTimeout >= 0) conn.setConnectTimeout((int) connectTimeout); conn.setDoOutput(true); long readTimeout = _proxyFactory.getReadTimeout(); if (readTimeout > 0) { try { conn.setReadTimeout((int) readTimeout); } catch (Throwable e) { } } /* // Used chunked mode when available, i.e. JDK 1.5. if (_proxyFactory.isChunkedPost() && conn instanceof HttpURLConnection) { try { HttpURLConnection httpConn = (HttpURLConnection) conn; httpConn.setChunkedStreamingMode(8 * 1024); } catch (Throwable e) { } } */ return new HessianURLConnection(url, conn); }
Example 19
Source File: HttpPostGet.java From bidder with Apache License 2.0 | 4 votes |
/** * Send an HTTP Post * @param targetURL * String. The URL to transmit to. * @param data * byte[]. The payload bytes. * @param connTimeout * int. The connection timeout in ms * @param readTimeout * int. The read timeout in ms. * @return byte[]. The contents of the POST return. * @throws Exception * on network errors. */ public byte [] sendPost(String targetURL, byte [] data, int connTimeout, int readTimeout) throws Exception { URLConnection connection = new URL(targetURL).openConnection(); connection.setRequestProperty("Content-Type", "application/json"); connection.setDoInput(true); connection.setDoOutput(true); connection.setConnectTimeout(connTimeout); connection.setReadTimeout(readTimeout); OutputStream output = connection.getOutputStream(); try { output.write(data); } finally { try { output.close(); } catch (IOException logOrIgnore) { logOrIgnore.printStackTrace(); } } InputStream response = connection.getInputStream(); http = (HttpURLConnection) connection; code = http.getResponseCode(); String value = http.getHeaderField("Content-Encoding"); if (value != null && value.equals("gzip")) { byte bytes [] = new byte[4096]; ByteArrayOutputStream baos = new ByteArrayOutputStream(); GZIPInputStream gis = new GZIPInputStream(http.getInputStream()); while(true) { int n = gis.read(bytes); if (n < 0) break; baos.write(bytes,0,n); } return baos.toByteArray(); } byte [] b = getContents(response); if (b.length == 0) return null; return b; }
Example 20
Source File: HttpPostGet.java From XRTB with Apache License 2.0 | 4 votes |
/** * Send an HTTP Post * @param targetURL * String. The URL to transmit to. * @param data * String. The payload String. * @param connTimeout * int. The connection timeout in ms * @param readTimeout * int. The read timeout in ms. * @return String. The contents of the POST return. * @throws Exception * on network errors. */ public String sendPost(String targetURL, String data, int connTimeout, int readTimeout) throws Exception { URLConnection connection = new URL(targetURL).openConnection(); connection.setRequestProperty("Content-Type", "application/json"); connection.setDoInput(true); connection.setDoOutput(true); connection.setConnectTimeout(connTimeout); connection.setReadTimeout(readTimeout); OutputStream output = connection.getOutputStream(); try { output.write(data.getBytes()); } finally { try { output.close(); } catch (IOException logOrIgnore) { logOrIgnore.printStackTrace(); } } InputStream response = connection.getInputStream(); http = (HttpURLConnection) connection; code = http.getResponseCode(); String value = http.getHeaderField("Content-Encoding"); if (value != null && value.equals("gzip")) { byte bytes [] = new byte[4096]; ByteArrayOutputStream baos = new ByteArrayOutputStream(); GZIPInputStream gis = new GZIPInputStream(http.getInputStream()); while(true) { int n = gis.read(bytes); if (n < 0) break; baos.write(bytes,0,n); } return new String(baos.toByteArray()); } byte [] b = getContents(response); if (b.length == 0) return null; return new String(b, 0, b.length); }