List of usage examples for java.net URLConnection connect
public abstract void connect() throws IOException;
From source file:io.github.bonigarcia.wdm.BrowserManager.java
private boolean isNetAvailable() { try {/*from ww w . j a v a 2s. co m*/ URL url = getDriverUrl(); Proxy proxy = createProxy(); URLConnection conn = proxy != null ? url.openConnection(proxy) : url.openConnection(); conn.connect(); return true; } catch (IOException e) { log.warn("Network not available. Forcing the use of cache"); return false; } }
From source file:org.jab.docsearch.utils.NetUtils.java
/** * Downloads URL to file/*from w ww . jav a 2s . c o m*/ * * Content type, content length, last modified and md5 will be set in SpiderUrl * * @param spiderUrl URL to download * @param file URL content downloads to this file * @return true if URL successfully downloads to a file */ public boolean downloadURLToFile(final SpiderUrl spiderUrl, final String file) { boolean downloadOk = false; BufferedInputStream inputStream = null; BufferedOutputStream outputStream = null; try { // if the file downloads - save it and return true URL url = new URL(spiderUrl.getUrl()); URLConnection conn = url.openConnection(); // set connection parameter conn.setDoInput(true); conn.setDoOutput(false); conn.setUseCaches(false); conn.setRequestProperty("User-Agent", USER_AGENT); // connect conn.connect(); // content type spiderUrl.setContentType(getContentType(conn)); // open streams inputStream = new BufferedInputStream(conn.getInputStream()); outputStream = new BufferedOutputStream(new FileOutputStream(file)); // copy data from URL to file long size = 0; int readed = 0; while ((readed = inputStream.read()) != -1) { size++; outputStream.write(readed); } // set values spiderUrl.setContentType(getContentType(conn)); spiderUrl.setSize(size); downloadOk = true; } catch (IOException ioe) { logger.fatal("downloadURLToFile() failed for URL='" + spiderUrl.getUrl() + "'", ioe); downloadOk = false; } finally { IOUtils.closeQuietly(outputStream); IOUtils.closeQuietly(inputStream); } // set values if (downloadOk) { spiderUrl.setMd5(FileUtils.getMD5Sum(file)); } return downloadOk; }
From source file:org.jab.docsearch.utils.NetUtils.java
/** * Get SpiderUrl status/* w w w . j av a 2 s .c om*/ * * Content type, content length, last modified and md5 will be set in SpiderUrl if url is changed * * @param spiderUrl URL to check * @param file URL content downloads to to this file * @return -1 if link is broken, 0 if the file is unchanged or 1 if the file * is different... part of caching algoritm. */ public int getURLStatus(final SpiderUrl spiderUrl, final String file) { // -1 means broken link // 0 means same file // 1 means changed int status; try { // attempt to obtain status from date URL url = new URL(spiderUrl.getUrl()); URLConnection conn = url.openConnection(); // set connection parameter conn.setDoInput(true); conn.setDoOutput(false); conn.setUseCaches(false); conn.setRequestProperty("User-Agent", USER_AGENT); // connect conn.connect(); // content type spiderUrl.setContentType(getContentType(conn)); // check date long spiDate = spiderUrl.getLastModified(); long urlDate = conn.getLastModified(); // same if date is equal and not zero if (spiDate == urlDate && urlDate != 0) { // the file is not changed status = 0; } else { // download the URL and compare hashes boolean downloaded = downloadURLToFile(conn, file); if (downloaded) { // download ok // compare file hashes String fileHash = FileUtils.getMD5Sum(file); if (fileHash.equals(spiderUrl.getMd5())) { // same status = 0; } else { // changed status = 1; // set changed values spiderUrl.setSize(FileUtils.getFileSize(file)); spiderUrl.setLastModified(urlDate); spiderUrl.setMd5(fileHash); } } else { // download failed // broken link status = -1; } } } catch (IOException ioe) { logger.error("getURLStatus() failed for URL='" + spiderUrl.getUrl() + "'", ioe); status = -1; } return status; }
From source file:org.y20k.transistor.helpers.MetadataHelper.java
private void shoutcastProxyReaderLoop(Socket proxy, URLConnection connection) throws IOException { connection.setConnectTimeout(5000);// ww w . j ava 2 s. co m connection.setReadTimeout(5000); connection.setRequestProperty("Icy-MetaData", "1"); connection.connect(); InputStream in = connection.getInputStream(); OutputStream out = proxy.getOutputStream(); out.write(("HTTP/1.0 200 OK\r\n" + "Pragma: no-cache\r\n" + "Content-Type: " + connection.getContentType() + "\r\n\r\n").getBytes(StandardCharsets.UTF_8)); byte buf[] = new byte[16384]; // one second of 128kbit stream int count = 0; int total = 0; int metadataSize = 0; final int metadataOffset = connection.getHeaderFieldInt("icy-metaint", 0); int bitRate = Math.max(connection.getHeaderFieldInt("icy-br", 128), 32); LogHelper.v(LOG_TAG, "createProxyConnection: connected, icy-metaint " + metadataOffset + " icy-br " + bitRate); while (true) { count = Math.min(in.available(), buf.length); if (count <= 0) { count = Math.min(bitRate * 64, buf.length); // buffer half-second of stream data } if (metadataOffset > 0) { count = Math.min(count, metadataOffset - total); } count = in.read(buf, 0, count); if (count == 0) { continue; } if (count < 0) { break; } out.write(buf, 0, count); total += count; if (metadataOffset > 0 && total >= metadataOffset) { // read metadata total = 0; count = in.read(); if (count < 0) { break; } count *= 16; metadataSize = count; if (metadataSize == 0) { continue; } // maximum metadata length is 4080 bytes total = 0; while (total < metadataSize) { count = in.read(buf, total, count); if (count < 0) { break; } if (count == 0) { continue; } total += count; count = metadataSize - total; } total = 0; String[] metadata = new String(buf, 0, metadataSize, StandardCharsets.UTF_8).split(";"); for (String s : metadata) { if (s.indexOf(TransistorKeys.SHOUTCAST_STREAM_TITLE_HEADER) == 0 && s.length() >= TransistorKeys.SHOUTCAST_STREAM_TITLE_HEADER.length() + 1) { handleMetadataString( s.substring(TransistorKeys.SHOUTCAST_STREAM_TITLE_HEADER.length(), s.length() - 1)); // break; } } } } }
From source file:com.photon.phresco.framework.rest.api.BuildInfoService.java
private boolean isConnectionAlive(String protocol, String host, int port) { boolean isAlive = true; try {//from w ww . jav a 2 s.c o m Thread.sleep(3000); URL url = new URL(protocol, host, port, ""); URLConnection connection = url.openConnection(); connection.connect(); } catch (Exception e) { isAlive = false; } return isAlive; }
From source file:io.s4.example.twittertopiccount.TwitterFeedListener.java
public void connectAndRead() throws Exception { URL url = new URL(urlString); URLConnection connection = url.openConnection(); String userPassword = userid + ":" + password; String encoded = EncodingUtil.getAsciiString(Base64.encodeBase64(EncodingUtil.getAsciiBytes(userPassword))); connection.setRequestProperty("Authorization", "Basic " + encoded); connection.connect(); InputStream is = connection.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String inputLine = null;//from w ww .j a va 2 s . c om while ((inputLine = br.readLine()) != null) { if (inputLine.trim().length() == 0) { blankCount++; continue; } messageCount++; messageQueue.add(inputLine); } }
From source file:org.wso2.carbon.core.CarbonAxisConfigurator.java
/** * Load an AxisConfiguration from the repository directory specified * * @param repoLocation repoLocation/*from w ww.j a v a 2s . com*/ * @param weblocation weblocation * @throws org.wso2.carbon.utils.ServerException ServerException */ public void init(String repoLocation, String weblocation) throws ServerException { if (repoLocation == null) { throw new ServerException("Axis2 repository not specified!"); } this.webLocation = weblocation; // Check whether this is a URL isUrlRepo = CarbonUtils.isURL(repoLocation); if (isUrlRepo) { // Is repoLocation a URL Repo? try { new URL(repoLocation).openConnection().connect(); } catch (IOException e) { throw new ServerException("Cannot connect to URL repository " + repoLocation, e); } this.repoLocation = repoLocation; } else { // Is repoLocation a file repo? File repo = new File(repoLocation); if (repo.exists()) { this.repoLocation = repo.getAbsolutePath(); } else { this.repoLocation = System.getProperty("wso2wsas.home") + File.separator + repoLocation; repo = new File(this.repoLocation); if (!repo.exists()) { this.repoLocation = null; throw new ServerException("Repository location '" + repoLocation + "' not found!"); } } } axis2xml = CarbonUtils.getAxis2Xml(); isUrlAxis2Xml = CarbonUtils.isURL(axis2xml); if (!isUrlAxis2Xml) { // Is axis2xml a URL to the axis2.xml file? File configFile = new File(axis2xml); if (!configFile.exists()) { //This will fallback to default axis2.xml this.axis2xml = null; //Thus default } } else { try { URLConnection urlConnection = new URL(axis2xml).openConnection(); urlConnection.connect(); } catch (IOException e) { throw new ServerException("Cannot connect to axis2.xml URL " + repoLocation, e); } isInitialized = true; } }
From source file:com.protheos.graphstream.JSONSender.java
/** * Send JSONObject message to Gephi server * //from w w w .j a v a 2 s. c o m * @param obj * , the JSON message content * @param operation * , the operation sending to the server, like "updateGraph", * "getGraph" */ private void doSend(JSONObject obj, String operation) { try { URL url = new URL("http", host, port, "/" + workspace + "?operation=" + operation + "&format=JSON"); URLConnection connection = url.openConnection(); connection.setDoOutput(true); connection.connect(); OutputStream outputStream = null; PrintStream out = null; try { outputStream = connection.getOutputStream(); out = new PrintStream(outputStream, true); out.print(obj.toString() + EOL); out.flush(); out.close(); // send event message to the server and read the result from the // server InputStream inputStream = connection.getInputStream(); BufferedReader bf = new BufferedReader(new InputStreamReader(inputStream)); String line; while ((line = bf.readLine()) != null) { // if (debug) debug(line); } inputStream.close(); } catch (UnknownServiceException e) { // protocol doesn't support output e.printStackTrace(); return; } } catch (IOException ex) { ex.printStackTrace(); } }
From source file:android.webkit.cts.TestWebServer.java
/** * Terminate the http server.//from www. j ava2s . c om */ public void shutdown() { try { // Avoid a deadlock between two threads where one is trying to call // close() and the other one is calling accept() by sending a GET // request for shutdown and having the server's one thread // sequentially call accept() and close(). URL url = new URL(mServerUri + SHUTDOWN_PREFIX); URLConnection connection = openConnection(url); connection.connect(); // Read the input from the stream to send the request. InputStream is = connection.getInputStream(); is.close(); // Block until the server thread is done shutting down. mServerThread.join(); } catch (MalformedURLException e) { throw new IllegalStateException(e); } catch (InterruptedException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } catch (NoSuchAlgorithmException e) { throw new IllegalStateException(e); } catch (KeyManagementException e) { throw new IllegalStateException(e); } setInstance(null, mSsl); }
From source file:com.bigdata.gom.TestRemoteGOM.java
public void testSimpleJSON() throws RepositoryException, IOException { final NanoSparqlObjectManager om = new NanoSparqlObjectManager(m_repo, m_namespace); final ValueFactory vf = om.getValueFactory(); final URI keyname = vf.createURI("attr:/test#name"); final Resource id = vf.createURI("gpo:test#1"); // om.checkValue(id); final int transCounter = om.beginNativeTransaction(); try {/*w w w .j a v a 2 s .co m*/ IGPO gpo = om.getGPO(id); gpo.setValue(keyname, vf.createLiteral("Martyn")); om.commitNativeTransaction(transCounter); } catch (Throwable t) { om.rollbackNativeTransaction(); throw new RuntimeException(t); } // Now let's read the data as JSON by connecting directly with the serviceurl URL url = new URL(m_serviceURL); URLConnection server = url.openConnection(); try { // server.setRequestProperty("Accept", TupleQueryResultFormat.JSON.toString()); server.setDoOutput(true); server.connect(); PrintWriter out = new PrintWriter(server.getOutputStream()); out.print("query=SELECT ?p ?v WHERE {<" + id.stringValue() + "> ?p ?v}"); out.close(); InputStream inst = server.getInputStream(); byte[] buf = new byte[2048]; int curs = 0; while (true) { int len = inst.read(buf, curs, buf.length - curs); if (len == -1) { break; } curs += len; } if (log.isInfoEnabled()) log.info("Read in " + curs + " - " + new String(buf, 0, curs)); } catch (Exception e) { e.printStackTrace(); } // clear cached data ((ObjectMgrModel) om).clearCache(); IGPO gpo = om.getGPO(id); // reads from backing journal assertTrue("Martyn".equals(gpo.getValue(keyname).stringValue())); }