List of usage examples for java.net URLConnection setReadTimeout
public void setReadTimeout(int timeout)
From source file:com.iwgame.iwcloud.baidu.task.util.DownloadUtil.java
/** * url//from ww w. j a v a 2 s.c o m * @param strUrl * The Url to be downloaded. * @param connectTimeout * Connect timeout in milliseconds. * @param readTimeout * Read timeout in milliseconds. * @param maxFileSize * Max file size in BYTE. * @return The file content as byte array. */ public static byte[] downloadFile(String strUrl, int connectTimeout, int readTimeout, int maxFileSize) throws IOException { InputStream in = null; try { URL url = new URL(strUrl); // URL? URLConnection ucon = url.openConnection(); ucon.setConnectTimeout(connectTimeout); ucon.setReadTimeout(readTimeout); ucon.connect(); if (ucon.getContentLength() > maxFileSize) { String msg = "File " + strUrl + " size [" + ucon.getContentLength() + "] too large, download stoped."; logger.error(msg); throw new ClientInternalException(msg); } if (ucon instanceof HttpURLConnection) { HttpURLConnection httpCon = (HttpURLConnection) ucon; if (httpCon.getResponseCode() > 399) { String msg = "Failed to download file " + strUrl + " server response " + httpCon.getResponseMessage(); logger.error(msg); throw new ClientInternalException(msg); } } in = ucon.getInputStream(); // ? byte[] byteBuf = new byte[BUFFER_SIZE]; byte[] ret = null; int count, total = 0; // ?? while ((count = in.read(byteBuf, total, BUFFER_SIZE - total)) > 0) { total += count; if (total + 124 >= BUFFER_SIZE) break; } if (total < BUFFER_SIZE - 124) { ret = ArrayUtils.subarray(byteBuf, 0, total); } else { ByteArrayOutputStream bos = new ByteArrayOutputStream(MATERIAL_SIZE); count = total; total = 0; do { bos.write(byteBuf, 0, count); total += count; if (total > maxFileSize) { String msg = "File " + strUrl + " size exceed [" + maxFileSize + "] download stoped."; logger.error(msg); throw new ClientInternalException(msg); } } while ((count = in.read(byteBuf)) > 0); ret = bos.toByteArray(); } if (ret.length < MIN_SIZE) { String msg = "File " + strUrl + " size [" + maxFileSize + "] too small."; logger.error(msg); throw new ClientInternalException(msg); } return ret; } catch (IOException e) { String msg = "Failed to download file " + strUrl + " msg=" + e.getMessage(); logger.error(msg); throw e; } finally { try { if (in != null) { in.close(); } } catch (IOException e) { logger.error("Exception while close url - ", e); throw e; } } }
From source file:opennlp.tools.similarity.apps.WebSearchEngineResultsScraper.java
protected static String fetchPageSearchEngine(String url) { System.out.println("fetch url " + url); String pageContent = null;/* w w w . j a va 2 s . c o m*/ StringBuffer buf = new StringBuffer(); try { URLConnection connection = new URL(url).openConnection(); connection.setReadTimeout(50000); connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3"); String line; BufferedReader reader = null; try { reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); } catch (Exception e) { System.err.println("Unable to complete search engine request " + url); //e.printStackTrace(); } while ((line = reader.readLine()) != null) { buf.append(line); } } catch (Exception e) { // e.printStackTrace(); System.err.println("error fetching url " + url); } return buf.toString(); }
From source file:org.connectbot.util.UpdateHelper.java
/** * Read contents of a URL and return as a String. Handles any server * downtime with a 6-second timeout./*from ww w . j av a 2 s. co m*/ */ private static String getUrl(String tryUrl, String userAgent) throws Exception { URL url = new URL(tryUrl); URLConnection connection = url.openConnection(); connection.setConnectTimeout(6000); connection.setReadTimeout(6000); connection.setRequestProperty("User-Agent", userAgent); connection.connect(); InputStream is = connection.getInputStream(); ByteArrayOutputStream os = new ByteArrayOutputStream(); int bytesRead; byte[] buffer = new byte[1024]; while ((bytesRead = is.read(buffer)) != -1) { os.write(buffer, 0, bytesRead); } os.flush(); os.close(); is.close(); return new String(os.toByteArray()); }
From source file:org.opencastproject.rest.RestServiceTestEnv.java
/** * Return a localhost base URL with a random port between 8081 and 9000. * The method features a port usage detection to ensure it returns a free port. *///from ww w .ja v a2s . c om public static URL localhostRandomPort() { for (int tries = 100; tries > 0; tries--) { final URL url = UrlSupport.url("http", "localhost", 8081 + new Random(System.currentTimeMillis()).nextInt(919)); InputStream in = null; try { final URLConnection con = url.openConnection(); con.setConnectTimeout(1000); con.setReadTimeout(1000); con.getInputStream(); } catch (IOException e) { IoSupport.closeQuietly(in); return url; } } throw new RuntimeException("Cannot find free port. Giving up."); }
From source file:org.apache.atlas.security.SecureClientUtils.java
private static void setTimeouts(URLConnection connection, int socketTimeout) { connection.setConnectTimeout(socketTimeout); connection.setReadTimeout(socketTimeout); }
From source file:org.sakaiproject.compilatio.util.CompilatioAPIUtil.java
public static Document callCompilatioReturnDocument(String apiURL, Map<String, String> parameters, String secretKey, final int timeout) throws TransientSubmissionException, SubmissionException { SOAPConnectionFactory soapConnectionFactory; Document xmlDocument = null;/* w w w .ja va 2 s.c o m*/ try { soapConnectionFactory = SOAPConnectionFactory.newInstance(); SOAPConnection soapConnection = soapConnectionFactory.createConnection(); MessageFactory messageFactory = MessageFactory.newInstance(); SOAPMessage soapMessage = messageFactory.createMessage(); SOAPPart soapPart = soapMessage.getSOAPPart(); SOAPEnvelope envelope = soapPart.getEnvelope(); SOAPBody soapBody = envelope.getBody(); SOAPElement soapBodyAction = soapBody.addChildElement(parameters.get("action")); parameters.remove("action"); // api key SOAPElement soapBodyKey = soapBodyAction.addChildElement("key"); soapBodyKey.addTextNode(secretKey); Set<Entry<String, String>> ets = parameters.entrySet(); Iterator<Entry<String, String>> it = ets.iterator(); while (it.hasNext()) { Entry<String, String> param = it.next(); SOAPElement soapBodyElement = soapBodyAction.addChildElement(param.getKey()); soapBodyElement.addTextNode(param.getValue()); } URL endpoint = new URL(null, apiURL, new URLStreamHandler() { @Override protected URLConnection openConnection(URL url) throws IOException { URL target = new URL(url.toString()); URLConnection connection = target.openConnection(); // Connection settings connection.setConnectTimeout(timeout); connection.setReadTimeout(timeout); return (connection); } }); SOAPMessage soapResponse = soapConnection.call(soapMessage, endpoint); // loading the XML document ByteArrayOutputStream out = new ByteArrayOutputStream(); soapResponse.writeTo(out); DocumentBuilderFactory builderfactory = DocumentBuilderFactory.newInstance(); builderfactory.setNamespaceAware(true); DocumentBuilder builder = builderfactory.newDocumentBuilder(); xmlDocument = builder.parse(new InputSource(new StringReader(out.toString()))); soapConnection.close(); } catch (UnsupportedOperationException | SOAPException | IOException | ParserConfigurationException | SAXException e) { log.error(e); } return xmlDocument; }
From source file:org.sakaiproject.contentreview.compilatio.util.CompilatioAPIUtil.java
public static Document callCompilatioReturnDocument(String apiURL, Map<String, String> parameters, String secretKey, final int timeout, Proxy proxy, boolean isMultipart) throws TransientSubmissionException, SubmissionException { SOAPConnectionFactory soapConnectionFactory; Document xmlDocument = null;// w w w . j a v a 2 s.c om try { soapConnectionFactory = SOAPConnectionFactory.newInstance(); SOAPConnection soapConnection = soapConnectionFactory.createConnection(); MessageFactory messageFactory = MessageFactory.newInstance(); SOAPMessage soapMessage = messageFactory.createMessage(); SOAPPart soapPart = soapMessage.getSOAPPart(); SOAPEnvelope envelope = soapPart.getEnvelope(); SOAPBody soapBody = envelope.getBody(); SOAPElement soapBodyAction = soapBody.addChildElement(parameters.get("action")); parameters.remove("action"); // api key SOAPElement soapBodyKey = soapBodyAction.addChildElement("key"); soapBodyKey.addTextNode(secretKey); Set<Entry<String, String>> ets = parameters.entrySet(); Iterator<Entry<String, String>> it = ets.iterator(); while (it.hasNext()) { Entry<String, String> param = it.next(); SOAPElement soapBodyElement = soapBodyAction.addChildElement(param.getKey()); soapBodyElement.addTextNode(param.getValue()); } URL endpoint = new URL(null, apiURL, new URLStreamHandler() { @Override protected URLConnection openConnection(URL url) throws IOException { URL target = new URL(url.toString()); URLConnection connection = target.openConnection(); // Connection settings connection.setConnectTimeout(timeout); connection.setReadTimeout(timeout); return (connection); } }); SOAPMessage soapResponse = soapConnection.call(soapMessage, endpoint); // loading the XML document ByteArrayOutputStream out = new ByteArrayOutputStream(); soapResponse.writeTo(out); DocumentBuilderFactory builderfactory = DocumentBuilderFactory.newInstance(); builderfactory.setNamespaceAware(true); DocumentBuilder builder = builderfactory.newDocumentBuilder(); xmlDocument = builder.parse(new InputSource(new StringReader(out.toString()))); soapConnection.close(); } catch (UnsupportedOperationException | SOAPException | IOException | ParserConfigurationException | SAXException e) { log.error(e); } return xmlDocument; }
From source file:org.mule.tck.util.WebServiceOnlineCheck.java
public static boolean isWebServiceOnline() { logger.debug("Verifying that the web service is on-line..."); BufferedReader input = null;/* w w w .j a v a 2 s. c om*/ try { URLConnection conn = new URL(TEST_URL).openConnection(); // setting these timeouts ensures the client does not deadlock indefinitely // when the server has problems. conn.setConnectTimeout(AbstractMuleContextTestCase.RECEIVE_TIMEOUT); conn.setReadTimeout(AbstractMuleContextTestCase.RECEIVE_TIMEOUT); InputStream in = conn.getInputStream(); input = new BufferedReader(new InputStreamReader(in)); String response = ""; String line; while ((line = input.readLine()) != null) { response += line; } if (StringUtils.containsIgnoreCase(response, "Cisco")) { return true; } else { logger.warn("Unexpected response, web service does not seem to be on-line: \n" + response); return false; } } catch (Exception e) { logger.warn("Exception occurred, web service does not seem to be on-line: " + e); return false; } finally { IOUtils.closeQuietly(input); } }
From source file:ee.ria.xroad.common.conf.globalconf.ConfigurationDownloader.java
public static URLConnection getDownloadURLConnection(URL url) throws IOException { URLConnection connection = url.openConnection(); connection.setReadTimeout(READ_TIMEOUT); return connection; }
From source file:com.gallatinsystems.common.util.S3Util.java
public static URLConnection getConnection(String bucketName, String objectKey, String awsAccessKeyId, String awsAccessSecret) throws IOException { final String date = getDate(); final String payload = String.format(GET_PAYLOAD, date, bucketName, objectKey); final String signature = MD5Util.generateHMAC(payload, awsAccessSecret); final URL url = new URL(String.format(S3_URL, bucketName, objectKey)); final URLConnection conn = url.openConnection(); conn.setConnectTimeout(CONNECTION_TIMEOUT); conn.setReadTimeout(CONNECTION_TIMEOUT); conn.addRequestProperty("Cache-Control", "no-cache,max-age=0"); conn.setRequestProperty("Date", date); conn.setRequestProperty("Authorization", "AWS " + awsAccessKeyId + ":" + signature); return conn;/* ww w . j a v a2s . c o m*/ }