List of usage examples for java.net HttpURLConnection setReadTimeout
public void setReadTimeout(int timeout)
From source file:com.cloudbees.mtslaves.client.VirtualMachineRef.java
/** * Renews the lease for the virtual machine so that it won't * be terminated by the watch dog.// w w w . j a va 2s .com * * @throws IOException */ public void renew() throws IOException { HttpURLConnection con = open("renew"); con.setReadTimeout(RENEW_READ_TIMEOUT_MS); verifyResponseStatus(con); }
From source file:com.aperlambda.apercommon.connection.http.HttpRequestSender.java
private HttpURLConnection createConnection(HttpRequest request) throws IOException { URL url = new URL(request.getUrl()); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(timeout); connection.setReadTimeout(timeout); connection.setRequestMethod(sendMethod); return connection; }
From source file:URLMonitorPanel.java
public void run() { if (System.currentTimeMillis() - scheduledExecutionTime() > 5000) { // Let the next task do it return;/*from w w w . j a v a 2 s .c o m*/ } try { HttpURLConnection huc = (HttpURLConnection) url.openConnection(); huc.setConnectTimeout(1000); huc.setReadTimeout(1000); int code = huc.getResponseCode(); if (updater != null) updater.isAlive(true); } catch (Exception e) { if (updater != null) updater.isAlive(false); } }
From source file:de.fu_berlin.inf.dpp.netbeans.feedback.FileSubmitter.java
/** * Tries to upload the given file to the given HTTP server (via POST * method)./*from w ww . j a va 2 s .c o m*/ * * @param file * the file to upload * @param url * the URL of the server, that is supposed to handle the file * @param monitor * a monitor to report progress to * @throws IOException * if an I/O error occurs * */ public static void uploadFile(final File file, final String url, IProgressMonitor monitor) throws IOException { final String CRLF = "\r\n"; final String doubleDash = "--"; final String boundary = generateBoundary(); HttpURLConnection connection = null; OutputStream urlConnectionOut = null; FileInputStream fileIn = null; if (monitor == null) monitor = new NullProgressMonitor(); int contentLength = (int) file.length(); if (contentLength == 0) { log.warn("file size of file " + file.getAbsolutePath() + " is 0 or the file does not exist"); return; } monitor.beginTask("Uploading file " + file.getName(), contentLength); try { URL connectionURL = new URL(url); if (!"http".equals(connectionURL.getProtocol())) throw new IOException("only HTTP protocol is supported"); connection = (HttpURLConnection) connectionURL.openConnection(); connection.setUseCaches(false); connection.setDoInput(true); connection.setDoOutput(true); connection.setRequestMethod("POST"); connection.setReadTimeout(TIMEOUT); connection.setConnectTimeout(TIMEOUT); connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); String contentDispositionLine = "Content-Disposition: form-data; name=\"" + file.getName() + "\"; filename=\"" + file.getName() + "\"" + CRLF; String contentTypeLine = "Content-Type: application/octet-stream; charset=ISO-8859-1" + CRLF; String contentTransferEncoding = "Content-Transfer-Encoding: binary" + CRLF; contentLength += 2 * boundary.length() + contentDispositionLine.length() + contentTypeLine.length() + contentTransferEncoding.length() + 4 * CRLF.length() + 3 * doubleDash.length(); connection.setFixedLengthStreamingMode(contentLength); connection.connect(); urlConnectionOut = connection.getOutputStream(); PrintWriter writer = new PrintWriter(new OutputStreamWriter(urlConnectionOut, "US-ASCII"), true); writer.append(doubleDash).append(boundary).append(CRLF); writer.append(contentDispositionLine); writer.append(contentTypeLine); writer.append(contentTransferEncoding); writer.append(CRLF); writer.flush(); fileIn = new FileInputStream(file); byte[] buffer = new byte[8192]; for (int read = 0; (read = fileIn.read(buffer)) > 0;) { if (monitor.isCanceled()) return; urlConnectionOut.write(buffer, 0, read); monitor.worked(read); } urlConnectionOut.flush(); writer.append(CRLF); writer.append(doubleDash).append(boundary).append(doubleDash).append(CRLF); writer.close(); if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { log.debug("uploaded file " + file.getAbsolutePath() + " to " + connectionURL.getHost()); return; } throw new IOException("failed to upload file " + file.getAbsolutePath() + connectionURL.getHost() + " [" + connection.getResponseMessage() + "]"); } finally { IOUtils.closeQuietly(fileIn); IOUtils.closeQuietly(urlConnectionOut); if (connection != null) connection.disconnect(); monitor.done(); } }
From source file:integration.util.graylog.ServerHelper.java
public String getNodeId() throws MalformedURLException, URISyntaxException { final URI uri = IntegrationTestsConfig.getGlServerURL(); ObjectMapper mapper = new ObjectMapper(); try {/* w ww . ja v a2s . com*/ HttpURLConnection connection = (HttpURLConnection) new URL(uri.toURL(), "/system").openConnection(); connection.setConnectTimeout(HTTP_TIMEOUT); connection.setReadTimeout(HTTP_TIMEOUT); connection.setRequestMethod("GET"); if (uri.getUserInfo() != null) { String encodedUserInfo = Base64 .encodeBase64String(uri.getUserInfo().getBytes(StandardCharsets.UTF_8)); connection.setRequestProperty("Authorization", "Basic " + encodedUserInfo); } InputStream inputStream = connection.getInputStream(); JsonNode json = mapper.readTree(inputStream); return json.get("node_id").toString(); } catch (IOException e) { e.printStackTrace(); } return "00000000-0000-0000-0000-000000000000"; }
From source file:at.mukprojects.giphy4j.dao.HttpRequestSender.java
private HttpURLConnection createConnection(Request request) throws IOException { URL url = new URL(request.getUrl()); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(Giphy4JConstants.TIMEOUT); connection.setReadTimeout(Giphy4JConstants.TIMEOUT); connection.setRequestMethod(Giphy4JConstants.SEND_METHOD); return connection; }
From source file:dlauncher.dialogs.Login.java
private String login_getResponse() throws IOException { URL website = new URL("http://authserver.mojang.com/authenticate"); final HttpURLConnection connection = (HttpURLConnection) website.openConnection(); connection.setReadTimeout(3000); try (Closeable c = new Closeable() { @Override// w w w . ja v a 2 s. c o m public void close() throws IOException { connection.disconnect(); } }) { StringBuilder response; try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) { response = new StringBuilder(); String inputLine; while ((inputLine = in.readLine()) != null) { response.append(inputLine); } } return response.toString(); } }
From source file:it.polito.tellmefirst.apimanager.RestManager.java
public String getStringFromAPI(String urlStr) { LOG.debug("[getStringFromAPI] - BEGIN - url=" + urlStr); String result = ""; try {/*from w ww . j a va2 s . co m*/ LOG.debug("[getStringFromAPI] - http.proxyHost=" + System.getProperty("http.proxyHost")); LOG.debug("[getStringFromAPI] - http.proxyPort=" + System.getProperty("http.proxyPort")); LOG.debug("[getStringFromAPI] - https.proxyHost=" + System.getProperty("https.proxyHost")); LOG.debug("[getStringFromAPI] - https.proxyPort=" + System.getProperty("https.proxyPort")); LOG.debug("[getStringFromAPI] - http.nonProxyHosts=" + System.getProperty("http.nonProxyHosts")); URL url = new URL(urlStr); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(15000); //set timeout to 15 seconds conn.setReadTimeout(15000); //set timeout to 15 seconds if (conn.getResponseCode() != 200) { throw new IOException(conn.getResponseMessage()); } BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); StringBuilder sb = new StringBuilder(); String line; while ((line = rd.readLine()) != null) { sb.append(line); } rd.close(); conn.disconnect(); result = sb.toString(); } catch (Exception e) { LOG.error("[getStringFromAPI] - EXCEPTION for url=" + urlStr, e); } LOG.debug("[getStringFromAPI] - END"); return result; }
From source file:com.threewks.analytics.client.AnalyticsClient.java
/** * Post JSON a URL. Ignore (but log) any errors. * /*w w w . ja v a 2s .c om*/ * @param url the URL to post the data to. * @param json the JSON data to post. */ void postJson(String url, String json) { PrintWriter writer = null; try { HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); connection.setConnectTimeout(5000); connection.setReadTimeout(5000); connection.setDoOutput(true); connection.setRequestProperty(API_KEY_HEADER, apiKey); connection.setRequestProperty(CONTENT_TYPE_HEADER, JSON_CONTENT_TYPE); writer = new PrintWriter(connection.getOutputStream(), true); writer.append(json); writer.close(); int responseCode = connection.getResponseCode(); if (responseCode != 200) { logger.warning(String.format( "Analytics server returned HTTP response code: %s when posting data to url: %s", responseCode, url)); } } catch (Exception e) { logger.warning(String.format("Error sending data to url: %s, error: %s", url, e.getMessage())); } finally { IOUtils.closeQuietly(writer); } }
From source file:KV78Tester.java
public String[] getLines() throws Exception { String uri = "http://openov.nl:5078/line/"; URL url = new URL(uri); HttpURLConnection uc = (HttpURLConnection) url.openConnection(); uc.setRequestProperty("User-Agent", "KV78Turbo-Test"); uc.setConnectTimeout(60000);/*from w w w . ja v a 2s . com*/ uc.setReadTimeout(60000); BufferedReader in; in = new BufferedReader(new InputStreamReader(uc.getInputStream(), "UTF-8")); try { return parseLines(in); } finally { uc.disconnect(); } }