List of usage examples for java.net URLConnection setConnectTimeout
public void setConnectTimeout(int timeout)
From source file:org.xlrnet.metadict.engines.heinzelnisse.HeinzelnisseEngine.java
@NotNull private URLConnection buildUrlConnection(@NotNull URL url) throws IOException { URLConnection connection = url.openConnection(); connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 4 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19"); connection.setRequestProperty("Content-Type", "application/json"); connection.setRequestProperty("X-Requested-With", "XMLHttpRequest"); connection.setRequestProperty("Referrer", "http://www.heinzelnisse.info/app"); connection.setConnectTimeout(5000); connection.setReadTimeout(5000);/*from w w w .ja va 2s . co m*/ return connection; }
From source file:edu.usf.cutr.gtfs_realtime.bullrunner.GtfsRealtimeProviderImpl.java
private Pair downloadVehicleDetails() throws IOException, JSONException { URLConnection connection = null; try {//from w ww . java 2s .c o m connection = _url.openConnection(); } catch (Exception ex) { _log.error("Error in opening feeds url", ex); } connection.setConnectTimeout(10000); // connectTimeout is time out in miliseconds connection.setReadTimeout(10000); java.io.InputStream in = connection.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8")); StringBuilder builder = new StringBuilder(); String inputLine; JSONArray stopIDsArray; JSONArray vehicleArray; try { while ((inputLine = reader.readLine()) != null) builder.append(inputLine).append("\n"); JSONObject object = (JSONObject) new JSONTokener(builder.toString()).nextValue(); String data = object.getString("PredictionData"); JSONObject child2_obj = new JSONObject(data); responseTimeStamp = child2_obj.getString("TimeStamp"); stopIDsArray = child2_obj.getJSONArray("StopPredictions"); vehicleArray = child2_obj.getJSONArray("VehicleLocationData"); } catch (java.net.SocketTimeoutException ex) { _log.error("Error readline, server dosn't close the connection.", ex); stopIDsArray = null; vehicleArray = null; } return new Pair(stopIDsArray, vehicleArray); }
From source file:org.gudy.azureus2.pluginsimpl.local.utils.xml.simpleparser.SimpleXMLParserDocumentImpl.java
private void createSupport(InputStream input_stream) throws SimpleXMLParserDocumentException { try {/*from w w w . ja va 2 s. co m*/ DocumentBuilderFactory dbf = getDBF(); // Step 2: create a DocumentBuilder that satisfies the constraints // specified by the DocumentBuilderFactory DocumentBuilder db = dbf.newDocumentBuilder(); // Set an ErrorHandler before parsing OutputStreamWriter errorWriter = new OutputStreamWriter(System.err); MyErrorHandler error_handler = new MyErrorHandler(new PrintWriter(errorWriter, true)); db.setErrorHandler(error_handler); db.setEntityResolver(new EntityResolver() { public InputSource resolveEntity(String publicId, String systemId) { // System.out.println( publicId + ", " + systemId ); // handle bad DTD external refs try { URL url = new URL(systemId); if (source_url != null) { String net = AENetworkClassifier.categoriseAddress(source_url.getHost()); if (net != AENetworkClassifier.AT_PUBLIC) { if (AENetworkClassifier.categoriseAddress(url.getHost()) != net) { return new InputSource(new ByteArrayInputStream( "<?xml version='1.0' encoding='UTF-8'?>".getBytes())); } } } String host = url.getHost(); InetAddress.getByName(host); // try connecting too as connection-refused will also bork XML parsing InputStream is = null; try { URLConnection con = url.openConnection(); con.setConnectTimeout(15 * 1000); con.setReadTimeout(15 * 1000); is = con.getInputStream(); byte[] buffer = new byte[32]; int pos = 0; while (pos < buffer.length) { int len = is.read(buffer, pos, buffer.length - pos); if (len <= 0) { break; } pos += len; } String str = new String(buffer, "UTF-8").trim().toLowerCase(Locale.US); if (!str.contains("<?xml")) { // not straightforward to check for naked DTDs, could be lots of <!-- commentry preamble which of course can occur // in HTML too buffer = new byte[32000]; pos = 0; while (pos < buffer.length) { int len = is.read(buffer, pos, buffer.length - pos); if (len <= 0) { break; } pos += len; } str += new String(buffer, "UTF-8").trim().toLowerCase(Locale.US); if (str.contains("<html") && str.contains("<head")) { throw (new Exception("Bad DTD")); } } } catch (Throwable e) { return new InputSource( new ByteArrayInputStream("<?xml version='1.0' encoding='UTF-8'?>".getBytes())); } finally { if (is != null) { try { is.close(); } catch (Throwable e) { } } } return (null); } catch (UnknownHostException e) { return new InputSource( new ByteArrayInputStream("<?xml version='1.0' encoding='UTF-8'?>".getBytes())); } catch (Throwable e) { return (null); } } }); // Step 3: parse the input file document = db.parse(input_stream); SimpleXMLParserDocumentNodeImpl[] root_nodes = parseNode(document, false); int root_node_count = 0; // remove any processing instructions such as <?xml-stylesheet for (int i = 0; i < root_nodes.length; i++) { SimpleXMLParserDocumentNodeImpl node = root_nodes[i]; if (node.getNode().getNodeType() != Node.PROCESSING_INSTRUCTION_NODE) { root_node = node; root_node_count++; } } if (root_node_count != 1) { throw (new SimpleXMLParserDocumentException( "invalid document - " + root_nodes.length + " root elements")); } } catch (Throwable e) { throw (new SimpleXMLParserDocumentException(e)); } }
From source file:org.apache.hadoop.mapred.task.reduce.Fetcher.java
/** * The connection establishment is attempted multiple times and is given up * only on the last failure. Instead of connecting with a timeout of X, we * try connecting with a timeout of x < X but multiple times. *///from w ww .j a v a 2 s. c om private void connect(URLConnection connection, int connectionTimeout) throws IOException { int unit = 0; if (connectionTimeout < 0) { throw new IOException("Invalid timeout " + "[timeout = " + connectionTimeout + " ms]"); } else if (connectionTimeout > 0) { unit = Math.min(UNIT_CONNECT_TIMEOUT, connectionTimeout); } // set the connect timeout to the unit-connect-timeout connection.setConnectTimeout(unit); while (true) { try { connection.connect(); break; } catch (IOException ioe) { // update the total remaining connect-timeout connectionTimeout -= unit; // throw an exception if we have waited for timeout amount of // time // note that the updated value if timeout is used here if (connectionTimeout == 0) { throw ioe; } // reset the connect timeout for the last try if (connectionTimeout < unit) { unit = connectionTimeout; // reset the connect time out for the final connect connection.setConnectTimeout(unit); } } } }
From source file:com.servoy.extensions.plugins.http.HttpProvider.java
/** * Get media (binary data) such as images in a variable. It also supports gzip-ed content. * If this url is an https url that uses certificates unknown to Java * then you have to use the HttpClient so that smart client users will get the unknown certificate dialog that they then can accept * or you must make sure that those server certificates are stored in the cacerts of the java vm that is used (this is required for a web or headless client) * * @sample//w w w . j av a2 s . c o m * var image_byte_array = plugins.http.getMediaData('http://www.cnn.com/cnn.gif'); * * @param url */ public byte[] js_getMediaData(String url) { if (url == null) return null; ByteArrayOutputStream sb = new ByteArrayOutputStream(); try { URLConnection connection = createURLFromString(url).openConnection(); if (timeout >= 0) connection.setConnectTimeout(timeout); InputStream is = connection.getInputStream(); BufferedInputStream bis = new BufferedInputStream(is); Utils.streamCopy(bis, sb); bis.close(); is.close(); } catch (Exception e) { Debug.error(e); } return sb.toByteArray(); }
From source file:org.sakaiproject.lessonbuildertool.service.AjaxServer.java
public String getMimeType(String url) { Session s = SessionManager.getCurrentSession(); if (s == null || s.getUserId() == null) { // return ""; }/*from ww w .java2 s . co m*/ if (SimplePageBean.getYoutubeKeyFromUrl(url) != null) return "application/youtube"; String mimeType = ""; URLConnection conn = null; try { conn = new URL(new URL(ServerConfigurationService.getServerUrl()), url).openConnection(); conn.setConnectTimeout(10000); conn.setReadTimeout(10000); conn.connect(); String t = conn.getContentType(); if (t != null && !t.equals("")) { int i = t.indexOf(";"); if (i >= 0) t = t.substring(0, i); t = t.trim(); mimeType = t; } } catch (Exception e) { } finally { if (conn != null) { try { conn.getInputStream().close(); } catch (Exception e) { // log.error("getTypeOfUrl unable to close " + e); } } } if (mimeType == null || mimeType.equals("")) { String name = url; // starts after last / int i = name.lastIndexOf("/"); if (i >= 0) name = name.substring(i + 1); String extension = null; i = name.lastIndexOf("."); if (i > 0) extension = name.substring(i + 1); if (extension == null) return ""; if (SimplePageBean.imageTypes.contains(extension)) { return "image/unknown"; } if (extension.equals("html") || extension.equals("htm")) { return "text/html"; } else if (extension.equals("xhtml") || extension.equals("xht")) { return "application/xhtml+xml"; } else { return ""; } } return mimeType; }
From source file:org.wso2.emm.system.service.api.OTAServerManager.java
/** * Downloads the property list from remote site, and parse it to property list. * The caller can parse this list and get information. */// w ww. j ava 2 s . c o m public void getTargetPackagePropertyList(final URL url) { final String operation = Preference.getBoolean(context, context.getResources().getString(R.string.firmware_status_check_in_progress)) ? Constants.Operation.GET_FIRMWARE_UPGRADE_PACKAGE_STATUS : Constants.Operation.UPGRADE_FIRMWARE; if (asyncTask != null) { asyncTask.cancel(true); } asyncTask = new AsyncTask<Void, Void, Void>() { protected Void doInBackground(Void... param) { InputStream reader = null; ByteArrayOutputStream writer = null; BuildPropParser parser = null; final int bufSize = 1024; // First, trying to download the property list file. the build.prop of target image. try { URLConnection urlConnection; /* Use the URL configuration to open a connection to the OTA server */ urlConnection = url.openConnection(); urlConnection.setConnectTimeout(Constants.FIRMWARE_UPGRADE_CONNECTIVITY_TIMEOUT); urlConnection.setReadTimeout(Constants.FIRMWARE_UPGRADE_READ_TIMEOUT); /* Since you get a URLConnection, use it to get the InputStream */ reader = urlConnection.getInputStream(); /* Now that the InputStream is open, get the content length */ final int contentLength = urlConnection.getContentLength(); byte[] buffer = new byte[bufSize]; if (contentLength != -1) { writer = new ByteArrayOutputStream(contentLength); } else { writer = new ByteArrayOutputStream(DEFAULT_STREAM_LENGTH); } int totalBufRead = 0; int bytesRead; Timer timer = new Timer(); Log.d(TAG, "Start download: " + url.toString() + " to buffer"); while ((bytesRead = reader.read(buffer)) > 0) { // Write current segment into byte output stream writer.write(buffer, 0, bytesRead); Log.d(TAG, "wrote " + bytesRead + " into byte output stream"); totalBufRead += bytesRead; buffer = new byte[bufSize]; timer.cancel(); timer = new Timer(); timer.schedule(new Timeout(this), Constants.FIRMWARE_UPGRADE_READ_TIMEOUT); } Log.d(TAG, "Download finished: " + (Integer.toString(totalBufRead)) + " bytes downloaded"); parser = new BuildPropParser(writer, context); timer.cancel(); } catch (SocketTimeoutException e) { String message = "Connection failure (Socket timeout) when retrieving update package size."; Log.e(TAG, message + e); CommonUtils.sendBroadcast(context, operation, Constants.Code.FAILURE, Constants.Status.CONNECTION_FAILED, message); CommonUtils.callAgentApp(context, Constants.Operation.FAILED_FIRMWARE_UPGRADE_NOTIFICATION, 0, null); } catch (IOException e) { String message = "Property list (build.prop) not found in the server."; Log.e(TAG, message + e); CommonUtils.sendBroadcast(context, operation, Constants.Code.FAILURE, Constants.Status.FILE_NOT_FOUND, message); CommonUtils.callAgentApp(context, Constants.Operation.FAILED_FIRMWARE_UPGRADE_NOTIFICATION, 0, null); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { Log.e(TAG, "Failed to close buffer reader." + e); } } if (writer != null) { try { writer.close(); } catch (IOException e) { Log.e(TAG, "Failed to close buffer writer." + e); } } if (parser != null) { if (stateChangeListener != null) { stateChangeListener.onStateOrProgress(OTAStateChangeListener.STATE_IN_CHECKED, OTAStateChangeListener.NO_ERROR, parser, DEFAULT_STATE_INFO_CODE); } } else { reportCheckingError(OTAStateChangeListener.ERROR_CANNOT_FIND_SERVER); } } return null; } }.executeOnExecutor(threadPoolExecutor); }
From source file:org.wso2.iot.system.service.api.OTAServerManager.java
/** * Downloads the property list from remote site, and parse it to property list. * The caller can parse this list and get information. *///from ww w . j a va2s . com public void getTargetPackagePropertyList(final URL url) { final String operation = Preference.getBoolean(context, context.getResources().getString(R.string.firmware_status_check_in_progress)) ? Constants.Operation.GET_FIRMWARE_UPGRADE_PACKAGE_STATUS : Constants.Operation.UPGRADE_FIRMWARE; if (asyncTask != null) { asyncTask.cancel(true); } asyncTask = new AsyncTask<Void, Void, Void>() { protected Void doInBackground(Void... param) { InputStream reader = null; ByteArrayOutputStream writer = null; BuildPropParser parser = null; final int bufSize = 1024; // First, trying to download the property list file. the build.prop of target image. try { URLConnection urlConnection; /* Use the URL configuration to open a connection to the OTA server */ urlConnection = url.openConnection(); urlConnection.setConnectTimeout(Constants.FIRMWARE_UPGRADE_CONNECTIVITY_TIMEOUT); urlConnection.setReadTimeout(Constants.FIRMWARE_UPGRADE_READ_TIMEOUT); /* Since you get a URLConnection, use it to get the InputStream */ reader = urlConnection.getInputStream(); /* Now that the InputStream is open, get the content length */ final int contentLength = urlConnection.getContentLength(); byte[] buffer = new byte[bufSize]; if (contentLength != -1) { writer = new ByteArrayOutputStream(contentLength); } else { writer = new ByteArrayOutputStream(DEFAULT_STREAM_LENGTH); } int totalBufRead = 0; int bytesRead; Timer timer = new Timer(); Log.d(TAG, "Start download: " + url.toString() + " to buffer"); while ((bytesRead = reader.read(buffer)) > 0) { // Write current segment into byte output stream writer.write(buffer, 0, bytesRead); Log.d(TAG, "wrote " + bytesRead + " into byte output stream"); totalBufRead += bytesRead; buffer = new byte[bufSize]; timer.cancel(); timer = new Timer(); timer.schedule(new Timeout(this), Constants.FIRMWARE_UPGRADE_READ_TIMEOUT); } Log.d(TAG, "Download finished: " + (Integer.toString(totalBufRead)) + " bytes downloaded"); parser = new BuildPropParser(writer, context); timer.cancel(); } catch (SocketTimeoutException e) { String message = "Connection failure (Socket timeout) when retrieving update package size."; Log.e(TAG, message + e); CommonUtils.sendBroadcast(context, operation, Constants.Code.FAILURE, Constants.Status.CONNECTION_FAILED, message); CommonUtils.callAgentApp(context, Constants.Operation.FAILED_FIRMWARE_UPGRADE_NOTIFICATION, Preference.getInt(context, context.getResources().getString(R.string.operation_id)), message); } catch (IOException e) { String message = "Property list (build.prop) not found in the server."; Log.e(TAG, message + e); CommonUtils.sendBroadcast(context, operation, Constants.Code.FAILURE, Constants.Status.FILE_NOT_FOUND, message); CommonUtils.callAgentApp(context, Constants.Operation.FAILED_FIRMWARE_UPGRADE_NOTIFICATION, Preference.getInt(context, context.getResources().getString(R.string.operation_id)), message); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { Log.e(TAG, "Failed to close buffer reader." + e); } } if (writer != null) { try { writer.close(); } catch (IOException e) { Log.e(TAG, "Failed to close buffer writer." + e); } } if (parser != null) { if (stateChangeListener != null) { stateChangeListener.onStateOrProgress(OTAStateChangeListener.STATE_IN_CHECKED, OTAStateChangeListener.NO_ERROR, parser, DEFAULT_STATE_INFO_CODE); } } else { reportCheckingError(OTAStateChangeListener.ERROR_CANNOT_FIND_SERVER); } } return null; } }.executeOnExecutor(threadPoolExecutor); }
From source file:edu.ucuenca.authorsdisambiguation.Distance.java
public synchronized String Http(String s) throws SQLException, IOException { Statement stmt = conn.createStatement(); String sql;//from ww w . ja v a 2 s .co m sql = "SELECT * FROM cache where cache.key='" + getMD5(s) + "'"; java.sql.ResultSet rs = stmt.executeQuery(sql); String resp = ""; if (rs.next()) { resp = rs.getString("value"); rs.close(); stmt.close(); } else { rs.close(); stmt.close(); final URL url = new URL(s); final URLConnection connection = url.openConnection(); connection.setConnectTimeout(60000); connection.setReadTimeout(60000); connection.addRequestProperty("User-Agent", "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:44.0) Gecko/20100101 Firefox/44.0"); connection.addRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); final Scanner reader = new Scanner(connection.getInputStream(), "UTF-8"); while (reader.hasNextLine()) { final String line = reader.nextLine(); resp += line + "\n"; } reader.close(); try { JsonParser parser = new JsonParser(); parser.parse(resp); PreparedStatement stmt2 = conn.prepareStatement("INSERT INTO cache (key, value) values (?, ?)"); stmt2.setString(1, getMD5(s)); stmt2.setString(2, resp); stmt2.executeUpdate(); stmt2.close(); } catch (Exception e) { System.out.printf("Error al insertar en la DB: " + e); } } return resp; }
From source file:org.infoglue.calendar.actions.ViewEventListAction.java
public List getExternalFeedEntries(String externalRSSUrl) throws Exception { try {// w w w .jav a2 s . co m URL url = new URL(externalRSSUrl); URLConnection urlConn = url.openConnection(); urlConn.setConnectTimeout(5000); urlConn.setReadTimeout(10000); SyndFeedInput input = new SyndFeedInput(); SyndFeed inputFeed = input.build(new XmlReader(urlConn)); List entries = inputFeed.getEntries(); Iterator entriesIterator = entries.iterator(); while (entriesIterator.hasNext()) { SyndEntry entry = (SyndEntry) entriesIterator.next(); Iterator contentIterator = entry.getContents().iterator(); while (contentIterator.hasNext()) { SyndContent content = (SyndContent) contentIterator.next(); log.info("content:" + content.getValue()); if (content.getType().equalsIgnoreCase("text/xml")) content.setValue("<![CDATA[" + content.getValue() + "]]>"); } } return entries; } catch (Exception e) { throw new Exception( getParameterizedLabel("labels.internal.event.error.couldNotConnectToRSS", externalRSSUrl)); } }