Example usage for java.net URLConnection connect

List of usage examples for java.net URLConnection connect

Introduction

In this page you can find the example usage for java.net URLConnection connect.

Prototype

public abstract void connect() throws IOException;

Source Link

Document

Opens a communications link to the resource referenced by this URL, if such a connection has not already been established.

Usage

From source file:com.viettel.dms.download.DownloadFile.java

/**
 * Download file with urlConnection/*  www.ja v  a2  s .c o  m*/
 * @author : BangHN
 * since : 1.0
 */
public static void downloadWithURLConnection(String url, File output, File tmpDir) {
    BufferedOutputStream os = null;
    BufferedInputStream is = null;
    File tmp = null;
    try {
        VTLog.i("Download ZIPFile", "Downloading url :" + url);

        tmp = File.createTempFile("download", ".tmp", tmpDir);
        URL urlDownload = new URL(url);
        URLConnection cn = urlDownload.openConnection();
        cn.addRequestProperty("session", HTTPClient.sessionID);
        cn.setConnectTimeout(CONNECT_TIMEOUT);
        cn.setReadTimeout(READ_TIMEOUT);
        cn.connect();
        is = new BufferedInputStream(cn.getInputStream());
        os = new BufferedOutputStream(new FileOutputStream(tmp));
        //cp nht dung lng tp tin request
        fileSize = cn.getContentLength();
        //vn c tr?ng hp khng c ContentLength
        if (fileSize < 0) {
            //mc nh = 4 MB
            fileSize = 4 * 1024 * 1024;
        }
        copyStream(is, os);
        tmp.renameTo(output);
        tmp = null;
    } catch (IOException e) {
        ServerLogger.sendLog("Download ZIPFile", e.getMessage() + "\n" + e.toString() + "\n" + url, false,
                TabletActionLogDTO.LOG_EXCEPTION);
        VTLog.e("UnexceptionLog", VNMTraceUnexceptionLog.getReportFromThrowable(e));
        throw new RuntimeException(e);
    } catch (Exception e) {
        VTLog.e("UnexceptionLog", VNMTraceUnexceptionLog.getReportFromThrowable(e));
        ServerLogger.sendLog("Download ZIPFile", e.getMessage() + "\n" + e.toString() + "\n" + url, false,
                TabletActionLogDTO.LOG_EXCEPTION);
        throw new RuntimeException(e);
    } finally {
        if (tmp != null) {
            try {
                tmp.delete();
                tmp = null;
            } catch (Exception ignore) {
                ;
            }
        }
        if (is != null) {
            try {
                is.close();
                is = null;
            } catch (Exception ignore) {
                ;
            }
        }
        if (os != null) {
            try {
                os.close();
                os = null;
            } catch (Exception ignore) {
                ;
            }
        }
    }
}

From source file:org.owasp.jbrofuzz.core.Verifier.java

/**
 * <p>Return the contents of an internal file a String.</p>
 *  //w  w  w. jav  a 2s. c  om
 * @param fileName e.g. fuzzers.jbrf; headers.jbrf
 * @return the contents of the file as a String
 * 
 * @author subere@uncon.org
 * @version 2.0
 * @since 2.0
 */
private static String parseFile(String fileName) {

    final StringBuffer fileContents = new StringBuffer();

    // Attempt to read from the jar file
    final URL fileURL = ClassLoader.getSystemClassLoader().getResource(fileName);

    if (fileURL == null) {
        throw new RuntimeException(ERROR_MSG + "could not find " + fileName);
    }

    // Read the characters from the file
    BufferedReader in = null;
    try {
        final URLConnection connection = fileURL.openConnection();
        connection.connect();

        in = new BufferedReader(new InputStreamReader(connection.getInputStream()));

        int counter = 0;
        int c;
        while (((c = in.read()) > 0) && (counter < MAX_CHARS)) {
            // Allow the character only if its printable ascii or \n
            if ((CharUtils.isAsciiPrintable((char) c)) || (((char) c) == '\n')) {
                fileContents.append((char) c);
            }
            counter++;
        }
        in.close();

        if (counter == MAX_CHARS) {

            throw new RuntimeException(ERROR_MSG + "\n... stopped reading file :" + fileName + "\nafter "
                    + MAX_CHARS + " characters.\n\n");

        }

    } catch (final IOException e1) {
        throw new RuntimeException(ERROR_MSG + "could not read " + fileName);
    } finally {
        IOUtils.closeQuietly(in);
    }

    return fileContents.toString();
}

From source file:com.snaplogic.snaps.checkfree.SoapExecuteTest.java

static String getContentFrom(URL fileUrl) {
    String templateText;//from  w  w  w  . j av a2  s . c o m
    try {
        URLConnection urlConnection = fileUrl.openConnection();
        if (urlConnection == null) {
            throw new ExecutionException(ERR_URL_CONNECT).formatWith(fileUrl.toString())
                    .withReason(REASON_URL_CONNECT).withResolution(RESOLUTION_URL_CONNECT);
        }
        urlConnection.connect();
        try (final InputStream in = urlConnection.getInputStream()) {
            templateText = IOUtils.toString(in);
            if (StringUtils.isBlank(templateText)) {
                throw new ExecutionException(TEMPLATE_READ_FAIL).formatWith(fileUrl.toString())
                        .withReason(EMPTY_TEMPLATE).withResolution(CHECK_TEMPLATE_CONTENTS);
            }
        }
    } catch (IOException e) {
        throw new ExecutionException(TEMPLATE_READ_FAIL).formatWith(fileUrl.toString())
                .withReason(ERROR_READING_TEMPLATE).withResolution(CHECK_TEMPLATE_CONTENTS);
    }
    return templateText;
}

From source file:com.vmware.thinapp.common.util.AfUtil.java

/**
 * Attempt to extract a filename from the HTTP headers when accessing the
 * given URL./*w w  w.jav a  2  s .c om*/
 *
 * @param url URL to access
 * @return filename extracted from the Content-Disposition HTTP header, null
 *         if extraction fails.
 */
public static String getFilenameFromUrl(URL url) {
    String filename = null;

    if (url == null) {
        return null;
    }

    try {
        URLConnection connection = url.openConnection();
        connection.connect();

        // Pull out the Content-Disposition header if there is one
        String contentDisp = connection.getHeaderField(AfUtil.CONTENT_DISPOSITION);

        // Attempt to close the associated stream as we don't need it
        Closeables.closeQuietly(connection.getInputStream());

        // Attempt to extract the filename from the HTTP header
        filename = AfUtil.getFilenameFromContentDisposition(contentDisp);
    } catch (IOException ex) {
        // Unable to make the HTTP request to get the filename from the
        // message headers.
        // Do nothing, null will be returned.
    }
    return filename;
}

From source file:dk.clarin.tools.workflow.java

/**
* Sends an HTTP GET request to a url// w  ww  . j  av a 2 s .co m
*
* @param endpoint - The URL of the server. (Example: " http://www.yahoo.com/search")
* @param requestString - all the request parameters (Example: "param1=val1&param2=val2"). Note: This method will add the question mark (?) to the request - DO NOT add it yourself
* @return - The response from the end point
*/

public static int sendRequest(String result, String endpoint, String requestString, bracmat BracMat,
        String filename, String jobID, boolean postmethod) {
    int code = 0;
    String message = "";
    //String filelist;
    if (endpoint.startsWith("http://") || endpoint.startsWith("https://")) {
        // Send a GET or POST request to the servlet
        try {
            // Construct data
            String requestResult = "";
            String urlStr = endpoint;
            if (postmethod) // HTTP POST
            {
                logger.debug("HTTP POST");
                StringReader input = new StringReader(requestString);
                StringWriter output = new StringWriter();
                URL endp = new URL(endpoint);

                HttpURLConnection urlc = null;
                try {
                    urlc = (HttpURLConnection) endp.openConnection();
                    try {
                        urlc.setRequestMethod("POST");
                    } catch (ProtocolException e) {
                        throw new Exception("Shouldn't happen: HttpURLConnection doesn't support POST??", e);
                    }
                    urlc.setDoOutput(true);
                    urlc.setDoInput(true);
                    urlc.setUseCaches(false);
                    urlc.setAllowUserInteraction(false);
                    urlc.setRequestProperty("Content-type", "text/xml; charset=" + "UTF-8");

                    OutputStream out = urlc.getOutputStream();

                    try {
                        Writer writer = new OutputStreamWriter(out, "UTF-8");
                        pipe(input, writer);
                        writer.close();
                    } catch (IOException e) {
                        throw new Exception("IOException while posting data", e);
                    } finally {
                        if (out != null)
                            out.close();
                    }
                } catch (IOException e) {
                    throw new Exception("Connection error (is server running at " + endp + " ?): " + e);
                } finally {
                    if (urlc != null) {
                        code = urlc.getResponseCode();

                        if (code == 200) {
                            got200(result, BracMat, filename, jobID, urlc.getInputStream());
                        } else {
                            InputStream in = urlc.getInputStream();
                            try {
                                Reader reader = new InputStreamReader(in);
                                pipe(reader, output);
                                reader.close();
                                requestResult = output.toString();
                            } catch (IOException e) {
                                throw new Exception("IOException while reading response", e);
                            } finally {
                                if (in != null)
                                    in.close();
                            }
                            logger.debug("urlc.getResponseCode() == {}", code);
                            message = urlc.getResponseMessage();
                            didnotget200(code, result, endpoint, requestString, BracMat, filename, jobID,
                                    postmethod, urlStr, message, requestResult);
                        }
                        urlc.disconnect();
                    } else {
                        code = 0;
                        didnotget200(code, result, endpoint, requestString, BracMat, filename, jobID,
                                postmethod, urlStr, message, requestResult);
                    }
                }
                //requestResult = output.toString();
                logger.debug("postData returns " + requestResult);
            } else // HTTP GET
            {
                logger.debug("HTTP GET");
                // Send data

                if (requestString != null && requestString.length() > 0) {
                    urlStr += "?" + requestString;
                }
                URL url = new URL(urlStr);
                URLConnection conn = url.openConnection();
                conn.connect();

                // Cast to a HttpURLConnection
                if (conn instanceof HttpURLConnection) {
                    HttpURLConnection httpConnection = (HttpURLConnection) conn;
                    code = httpConnection.getResponseCode();
                    logger.debug("httpConnection.getResponseCode() == {}", code);
                    message = httpConnection.getResponseMessage();
                    BufferedReader rd;
                    StringBuilder sb = new StringBuilder();
                    ;
                    //String line;
                    if (code == 200) {
                        got200(result, BracMat, filename, jobID, httpConnection.getInputStream());
                    } else {
                        // Get the error response
                        rd = new BufferedReader(new InputStreamReader(httpConnection.getErrorStream()));
                        int nextChar;
                        while ((nextChar = rd.read()) != -1) {
                            sb.append((char) nextChar);
                        }
                        rd.close();
                        requestResult = sb.toString();
                        didnotget200(code, result, endpoint, requestString, BracMat, filename, jobID,
                                postmethod, urlStr, message, requestResult);
                    }
                } else {
                    code = 0;
                    didnotget200(code, result, endpoint, requestString, BracMat, filename, jobID, postmethod,
                            urlStr, message, requestResult);
                }
            }
            logger.debug("Job " + jobID + " receives status code [" + code + "] from tool.");
        } catch (Exception e) {
            //jobs = 0; // No more jobs to process now, probably the tool is not reachable
            logger.warn("Job " + jobID + " aborted. Reason:" + e.getMessage());
            /*filelist =*/ BracMat.Eval("abortJob$(" + result + "." + jobID + ")");
        }
    } else {
        //jobs = 0; // No more jobs to process now, probably the tool is not integrated at all
        logger.warn("Job " + jobID + " aborted. Endpoint must start with 'http://' or 'https://'. (" + endpoint
                + ")");
        /*filelist =*/ BracMat.Eval("abortJob$(" + result + "." + jobID + ")");
    }
    return code;
}

From source file:de.schildbach.wallet.goldcoin.ExchangeRatesProvider.java

private static Map<String, ExchangeRate> getBlockchainInfo() {
    try {/*w  ww.j a  va 2  s.  co m*/
        //double btcRate = getGoldCoinValueBTC();

        Double btcRate = 0.0;

        Object result = getGoldCoinValueBTC();

        if (result == null)
            return null;

        else
            btcRate = (Double) result;

        final URL URL = new URL("https://blockchain.info/ticker");
        final URLConnection connection = URL.openConnection();
        connection.setConnectTimeout(TIMEOUT_MS);
        connection.setReadTimeout(TIMEOUT_MS);
        connection.connect();
        final StringBuilder content = new StringBuilder();

        Reader reader = null;
        try {
            reader = new InputStreamReader(new BufferedInputStream(connection.getInputStream(), 1024));
            IOUtils.copy(reader, content);

            final Map<String, ExchangeRate> rates = new TreeMap<String, ExchangeRate>();

            //Add Bitcoin information
            rates.put("BTC", new ExchangeRate("BTC",
                    Utils.toNanoCoins(String.format("%.8f", btcRate).replace(",", ".")), "pubapi.cryptsy.com"));

            final JSONObject head = new JSONObject(content.toString());
            for (final Iterator<String> i = head.keys(); i.hasNext();) {
                final String currencyCode = i.next();
                final JSONObject o = head.getJSONObject(currencyCode);
                double gldInCurrency = o.getDouble("15m") * btcRate;
                final String rate = String.format("%.8f", gldInCurrency); //o.optString("15m", null);

                if (rate != null)
                    rates.put(currencyCode, new ExchangeRate(currencyCode,
                            Utils.toNanoCoins(rate.replace(",", ".")), URL.getHost()));
            }

            return rates;
        } finally {
            if (reader != null)
                reader.close();
        }
    } catch (final IOException x) {
        x.printStackTrace();
    } catch (final JSONException x) {
        x.printStackTrace();
    }

    return null;
}

From source file:org.arasthel.almeribus.utils.LoadFromWeb.java

public static int cargarParadasLinea(Context context, int numeroLinea) {
    if (!isConnectionEnabled(context)) {
        return SIN_CONEXION;
    }/* w  w  w . j a va 2 s .  c o  m*/
    try {
        if (cookie == null) {
            if (loadCookie() == MANTENIMIENTO) {
                return MANTENIMIENTO;
            }
        }
        URL url = new URL(QUERY_ADDRESS_PARADAS_LINEA + numeroLinea);
        URLConnection connection = url.openConnection();
        connection.setConnectTimeout(15000);
        connection.setReadTimeout(30000);
        connection.setRequestProperty("Cookie", "ASP.NET_SessionId=" + cookie);
        connection.setRequestProperty("REFERER", "http://m.surbus.com/tiempo-espera");
        connection.setRequestProperty("Content-Type", "application/json;charset=utf-8");
        connection.setRequestProperty("X-Requested-With", "XMLHttpRequest");
        connection.connect();
        Scanner scan = new Scanner(connection.getInputStream());
        StringBuilder strBuilder = new StringBuilder();
        while (scan.hasNextLine()) {
            strBuilder.append(scan.nextLine());
        }
        scan.close();
        JSONObject json = new JSONObject(strBuilder.toString());
        Log.d("Almeribus", strBuilder.toString());
        boolean isSuccessful = json.getBoolean("success");
        if (isSuccessful) {
            DataStorage.DBHelper.eliminarParadasLinea(numeroLinea);
            Linea l = new Linea(numeroLinea);
            JSONArray list = json.getJSONArray("list");
            Parada primeraParada = null;
            Parada paradaAnterior = null;
            for (int i = 0; i < list.length(); i++) {
                JSONObject paradaJSON = list.getJSONObject(i);
                int numeroParada = paradaJSON.getInt("IdBusStop");
                String nombreParada = paradaJSON.getString("Name");
                Parada p = null;
                if (DataStorage.paradas.containsKey(numeroParada)) {
                    p = DataStorage.paradas.get(numeroParada);
                    p.setNombre(nombreParada);
                } else {
                    p = new Parada(numeroParada, nombreParada);
                }
                synchronized (DataStorage.DBHelper) {
                    DataStorage.DBHelper.addInfoParada(numeroParada, nombreParada);
                }
                p.addLinea(l.getNumero());
                if (paradaAnterior != null) {
                    p.setAnterior(paradaAnterior.getId(), numeroLinea);
                }

                if (i == 0) {
                    primeraParada = p;
                } else if (i == list.length() - 1) {
                    primeraParada.setAnterior(p.getId(), numeroLinea);
                    p.setSiguiente(primeraParada.getId(), numeroLinea);
                }

                if (paradaAnterior != null) {
                    paradaAnterior.setSiguiente(p.getId(), numeroLinea);
                }

                paradaAnterior = p;
                synchronized (DataStorage.paradas) {
                    if (DataStorage.paradas.containsKey(numeroParada)) {
                        DataStorage.paradas.remove(numeroParada);
                    }
                    DataStorage.paradas.put(numeroParada, p);
                }

                l.addParada(p);
            }
            DataStorage.lineas.put(numeroLinea, l);
            for (Parada parada : l.getParadas()) {
                synchronized (DataStorage.DBHelper) {
                    try {
                        DataStorage.DBHelper.addParadaLinea(parada.getId(), numeroLinea,
                                parada.getSiguiente(numeroLinea));
                    } catch (ParadaNotFoundException e) {
                        DataStorage.DBHelper.addParadaLinea(parada.getId(), numeroLinea, 0);
                    }
                }
            }
            return TODO_OK;
        } else {
            return ERROR_IO;
        }
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        return ERROR_IO;
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return ERROR_IO;
}

From source file:de.dal33t.powerfolder.util.ConfigurationLoader.java

/**
 * Loads a pre-configuration from the URL
 *
 * @param from//  w w  w .  j  a v  a 2s.  c om
 *            the URL to load from
 * @return the loaded properties WITHOUT those in config.
 * @throws IOException
 */
private static Properties loadPreConfiguration(URL from, String un, char[] pw) throws IOException {
    Reject.ifNull(from, "URL is null");
    URLConnection con = from.openConnection();
    if (StringUtils.isNotBlank(un)) {
        String s = un + ":" + Util.toString(pw);
        LoginUtil.clear(pw);
        String base64 = "Basic " + Base64.encodeBytes(s.getBytes("UTF-8"));
        con.setRequestProperty("Authorization", base64);
    }
    con.setConnectTimeout(1000 * URL_CONNECT_TIMEOUT_SECONDS);
    con.setReadTimeout(1000 * URL_CONNECT_TIMEOUT_SECONDS);
    con.connect();
    InputStream in = con.getInputStream();
    try {
        return loadPreConfiguration(in);
    } finally {
        try {
            in.close();
        } catch (Exception e) {
        }
    }
}

From source file:ubic.gemma.core.loader.expression.geo.DatasetCombiner.java

/**
 * Given a GDS, find the corresponding GSEs (there can be more than one in rare cases).
 *
 * @param datasetAccession dataset accession
 * @return Collection of series this data set is derived from (this is almost always just a single item).
 *///  w w  w. j  a va  2 s .c om
public static Collection<String> findGSEforGDS(String datasetAccession) {
    /*
     * go from GDS to GSE, using screen scraping.
     */
    // http://www.ncbi.nlm.nih.gov/entrez/query.fcgi?db=gds&term=GSE674[Accession]&cmd=search
    // grep on "GDS[[digits]] record"
    URL url = null;

    Pattern pat = Pattern.compile(DatasetCombiner.GSE_RECORD_REGEXP);

    Collection<String> associatedSeriesAccession = new HashSet<>();

    try {
        url = new URL(DatasetCombiner.ENTREZ_GEO_QUERY_URL_BASE + datasetAccession
                + DatasetCombiner.ENTREZ_GEO_QUERY_URL_SUFFIX);
        URLConnection conn = url.openConnection();
        conn.connect();

        try (InputStream is = conn.getInputStream();
                BufferedReader br = new BufferedReader(new InputStreamReader(is))) {

            String line;
            while ((line = br.readLine()) != null) {
                Matcher mat = pat.matcher(line);
                if (mat.find()) {
                    String capturedAccession = mat.group(1);
                    associatedSeriesAccession.add(capturedAccession);
                }
            }
        }
    } catch (MalformedURLException e) {
        DatasetCombiner.log.error(e, e);
        throw new RuntimeException("Invalid URL " + url, e);
    } catch (IOException e) {
        DatasetCombiner.log.error(e, e);
        throw new RuntimeException("Could not get data from remote server", e);
    }

    if (associatedSeriesAccession.size() == 0) {
        throw new IllegalStateException("No GSE found for " + datasetAccession);
    }

    return associatedSeriesAccession;

}

From source file:de.schildbach.wallet.goldcoin.ExchangeRatesProvider.java

private static Double getGoldCoinValueLTC() {
    Date date = new Date();
    long now = date.getTime();

    if ((now < (lastLitecoinValueCheck + 10 * 60)) && lastLitecoinValue > 0.0)
        return lastLitecoinValue;

    //final Map<String, ExchangeRate> rates = new TreeMap<String, ExchangeRate>();
    // Keep the LTC rate around for a bit
    Double ltcRate = 0.0;//from w ww.  j  a  va 2 s. co  m
    String currencyCryptsy = "LTC";
    String urlCryptsy = "http://pubapi.cryptsy.com/api.php?method=marketdata";

    try {
        // final String currencyCode = currencies[i];
        final URL URLCryptsy = new URL(urlCryptsy);
        final URLConnection connectionCryptsy = URLCryptsy.openConnection();
        connectionCryptsy.setConnectTimeout(TIMEOUT_MS);
        connectionCryptsy.setReadTimeout(TIMEOUT_MS);
        connectionCryptsy.connect();

        final StringBuilder contentCryptsy = new StringBuilder();

        Reader reader = null;
        try {
            reader = new InputStreamReader(new BufferedInputStream(connectionCryptsy.getInputStream(), 1024));
            IOUtils.copy(reader, contentCryptsy);
            final JSONObject head = new JSONObject(contentCryptsy.toString());
            JSONObject returnObject = head.getJSONObject("return");
            JSONObject markets = returnObject.getJSONObject("markets");
            JSONObject GLD = markets.getJSONObject("GLD");

            Double lastTrade = GLD.getDouble("lasttradeprice");

            //String euros = String.format("%.7f", lastTrade);
            // Fix things like 3,1250
            //euros = euros.replace(",", ".");
            //rates.put(currencyCryptsy, new ExchangeRate(currencyCryptsy, Utils.toNanoCoins(euros), URLCryptsy.getHost()));
            if (currencyCryptsy.equalsIgnoreCase("LTC"))
                ltcRate = lastTrade;

            lastLitecoinValue = ltcRate;
            lastLitecoinValueCheck = now;
        } finally {
            if (reader != null)
                reader.close();
        }
        return ltcRate;
    } catch (final IOException x) {
        x.printStackTrace();
    } catch (final JSONException x) {
        x.printStackTrace();
    }

    return null;
}