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:org.apache.synapse.registry.url.SimpleURLRegistry.java

public OMNode lookup(String key) {

    if (log.isDebugEnabled()) {
        log.debug("==> Repository fetch of resource with key : " + key);

    }/* w  w  w.j a  va  2  s .  com*/
    URL url = SynapseConfigUtils.getURLFromPath(root + key,
            properties.get(SynapseConstants.SYNAPSE_HOME) != null
                    ? properties.get(SynapseConstants.SYNAPSE_HOME).toString()
                    : "");
    if (url == null) {
        return null;
    }

    BufferedInputStream inputStream;
    try {
        URLConnection connection = SynapseConfigUtils.getURLConnection(url);
        if (connection == null) {
            if (log.isDebugEnabled()) {
                log.debug("Cannot create a URLConnection for given URL : " + url);
            }
            return null;
        }
        connection.connect();
        inputStream = new BufferedInputStream(connection.getInputStream());
    } catch (IOException e) {
        return null;
    }

    OMNode result = null;

    if (inputStream != null) {

        try {

            XMLStreamReader parser = XMLInputFactory.newInstance().createXMLStreamReader(inputStream);
            StAXOMBuilder builder = new StAXOMBuilder(parser);
            result = builder.getDocumentElement();

        } catch (OMException ignored) {

            if (log.isDebugEnabled()) {
                log.debug("The resource at the provided URL isn't " + "well-formed XML,So,takes it as a text");
            }

            try {
                inputStream.close();
            } catch (IOException e) {
                log.error("Error in closing the input stream. ", e);
            }
            result = SynapseConfigUtils.readNonXML(url);

        } catch (XMLStreamException ignored) {

            if (log.isDebugEnabled()) {
                log.debug("The resource at the provided URL isn't " + "well-formed XML,So,takes it as a text");
            }

            try {
                inputStream.close();
            } catch (IOException e) {
                log.error("Error in closing the input stream. ", e);
            }
            result = SynapseConfigUtils.readNonXML(url);

        } finally {
            try {
                if (result != null && result.getParent() != null) {
                    //TODO Replace following code with the correct code when synapse is moving to AXIOM 1.2.9
                    result.detach();
                    OMDocument omDocument = omFactory.createOMDocument();
                    omDocument.addChild(result);
                }
                inputStream.close();
            } catch (IOException e) {
                log.error("Error in closing the input stream.", e);
            }

        }

    }
    return result;
}

From source file:com.comcast.cdn.traffic_control.traffic_monitor.util.Fetcher.java

private static HttpCookie getTmCookie(final String url, final String username, final String password,
        final int timeout) throws IOException {
    if (tmCookie != null && !tmCookie.hasExpired()) {
        return tmCookie;
    }/* w w w.  jav  a  2  s . c  o  m*/

    final String charset = UTF8_STR;
    final String query = String.format("u=%s&p=%s", URLEncoder.encode(username, charset),
            URLEncoder.encode(password, charset));
    final URLConnection connection = new URL(url).openConnection();

    if (!(connection instanceof HttpsURLConnection)) {
        return null;
    }

    final HttpsURLConnection http = (HttpsURLConnection) connection;

    http.setInstanceFollowRedirects(false);

    http.setHostnameVerifier(new HostnameVerifier() {
        @Override
        public boolean verify(final String arg0, final SSLSession arg1) {
            return true;
        }
    });

    http.setRequestMethod("POST");
    http.setAllowUserInteraction(true);

    if (timeout != 0) {
        http.setConnectTimeout(timeout);
        http.setReadTimeout(timeout);
    }

    http.setDoOutput(true); // Triggers POST.
    http.setRequestProperty("Accept-Charset", charset);
    http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=" + charset);

    OutputStream output = null;

    try {
        output = http.getOutputStream();
        output.write(query.getBytes(charset));
    } finally {
        if (output != null) {
            try {
                output.close();
            } catch (IOException e) {
                LOGGER.debug(e, e);
            }
        }
    }

    LOGGER.info("fetching cookie: " + url);
    connection.connect();

    tmCookie = HttpCookie.parse(http.getHeaderField("Set-Cookie")).get(0);
    LOGGER.debug("cookie: " + tmCookie);

    return tmCookie;
}

From source file:de.mprengemann.hwr.timetabel.data.Parser.java

protected Void doInBackground(Void... params) {
    this.exception = null;

    if (Utils.connectionChecker(context)) {
        try {/*www . j a  v  a  2 s  . c  o  m*/
            final String matrikelnr = preferences.getString(context.getString(R.string.prefs_matrikelNrKey),
                    "");

            DefaultHttpClient httpclient = new DefaultHttpClient();
            if (preferences.getBoolean(context.getString(R.string.prefs_proxyFlagKey), false)) {
                HttpHost proxy = new HttpHost(
                        preferences.getString(context.getString(R.string.prefs_proxyKey), "localhost"),
                        Integer.parseInt(
                                preferences.getString(context.getString(R.string.prefs_proxyPortKey), "8080")));
                httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
            }

            HttpGet httpget = new HttpGet(Utils.buildURL(context, preferences));

            HttpResponse response = httpclient.execute(httpget);
            HttpEntity entity = response.getEntity();

            if (entity != null) {
                entity.consumeContent();
            }

            List<Cookie> cookies = httpclient.getCookieStore().getCookies();

            HttpPost httpost = new HttpPost("http://ipool.ba-berlin.de/main.php?action=login");

            List<NameValuePair> nvps = new ArrayList<NameValuePair>();
            nvps.add(new BasicNameValuePair("FORM_LOGIN_NAME", "student"));
            nvps.add(new BasicNameValuePair("FORM_LOGIN_PASS", matrikelnr));
            nvps.add(new BasicNameValuePair("FORM_LOGIN_PAGE", "home"));
            nvps.add(new BasicNameValuePair("FORM_LOGIN_REDIRECTION", Utils.buildURL(context, preferences)));
            nvps.add(new BasicNameValuePair("FORM_ACCEPT", "1"));
            nvps.add(new BasicNameValuePair("LOGIN", "login"));

            httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

            response = httpclient.execute(httpost);
            entity = response.getEntity();

            if (entity != null) {
                entity.consumeContent();
            }

            cookies = httpclient.getCookieStore().getCookies();

            final URL url;
            InputStream is = null;
            try {
                url = new URL(Utils.buildURL(context, preferences));
                URLConnection c = url.openConnection();

                try {
                    c.setRequestProperty("Cookie", cookies.get(0).getName() + "=" + cookies.get(0).getValue());
                    c.connect();
                    is = c.getInputStream();
                } catch (NullPointerException e) {
                    throw new ConnectionAuthentificationException(
                            context.getString(R.string.dialog_error_message_auth));
                } catch (IndexOutOfBoundsException e) {
                    throw new ConnectionAuthentificationException(
                            context.getString(R.string.dialog_error_message_auth));
                }

                if (is != null) {
                    try {
                        new IcsParser(is, new OnCalendarParsingListener() {

                            @Override
                            public void onNewItem(Component c) {
                                if (c.getName().equals(Component.VEVENT)) {
                                    PropertyList p = c.getProperties();

                                    Subjects s = new Subjects();
                                    s.setTitle(p.getProperty("SUMMARY").getValue());
                                    s.setShortTitle(s.getTitle().substring(s.getTitle().indexOf("-") + 1));
                                    s.setShow(true);

                                    SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd HHmmss");

                                    Events evt = new Events();
                                    try {
                                        evt.setStart(df
                                                .parse(p.getProperty("DTSTART").getValue().replace("T", " ")));
                                        evt.setEnd(
                                                df.parse(p.getProperty("DTEND").getValue().replace("T", " ")));
                                    } catch (ParseException e) {
                                        exception = new IPoolFormatException(
                                                context.getString(R.string.dialog_error_message_ipool));
                                        sendBugReport(e, url.toString());
                                    }

                                    evt.setRoom(p.getProperty("LOCATION").getValue());
                                    evt.setUid(p.getProperty("UID").getValue());

                                    String description = p.getProperty("DESCRIPTION").getValue();
                                    evt.setFullDescription(description);
                                    for (String desc : description.split("\\n")) {
                                        if (desc.startsWith("Dozent: ")) {
                                            evt.setLecturer(desc.replace("Dozent: ", ""));
                                            break;
                                        } else if (desc.startsWith("Art: ")) {
                                            evt.setType(desc.replace("Art: ", ""));
                                        }
                                    }
                                    try {
                                        if (listener != null) {
                                            listener.onNewItem(s, evt);
                                        }
                                    } catch (SQLiteConstraintException e) {
                                        exception = new StorageException(
                                                context.getString(R.string.dialog_error_message_storage));
                                        sendBugReport(e, url.toString(), s.toString(), evt.toString());
                                    }

                                }
                            }
                        });
                    } catch (ParserException e) {
                        exception = new UnknownTimetableException(
                                context.getString(R.string.dialog_error_message_auth));
                        sendNewTimetable(url);
                    } catch (IOException e) {
                        if (e instanceof HttpHostConnectException) {
                            exception = new ConnectionTimeoutException(
                                    context.getString(R.string.dialog_error_message_timeout));
                        } else {
                            exception = new ConnectionException(
                                    context.getString(R.string.dialog_error_message));
                        }
                    }
                } else {
                    throw new IOException();
                }
            } catch (ConnectionAuthentificationException e) {
                exception = e;
            } catch (IOException e) {
                if (e instanceof ConnectTimeoutException) {
                    exception = new ConnectionTimeoutException(
                            context.getString(R.string.dialog_error_message_timeout));
                } else if (e instanceof MalformedURLException) {
                    exception = new ConnectionException(context.getString(R.string.dialog_error_message));
                    sendBugReport(e);
                } else if (e instanceof HttpHostConnectException) {
                    exception = new ConnectionTimeoutException(
                            context.getString(R.string.dialog_error_message_timeout));
                } else {
                    exception = new ConnectionException(context.getString(R.string.dialog_error_message));
                    sendBugReport(e);
                }
            } finally {
                try {
                    if (is != null) {
                        is.close();
                    }
                } catch (IOException e) {
                    if (e instanceof ConnectTimeoutException) {
                        exception = new ConnectionTimeoutException(
                                context.getString(R.string.dialog_error_message_timeout));
                    } else {
                        exception = new ConnectionException(context.getString(R.string.dialog_error_message));
                        sendBugReport(e);
                    }
                }
            }

            httpclient.getConnectionManager().shutdown();
        } catch (Exception e) {
            if (e instanceof ConnectTimeoutException) {
                exception = new ConnectionTimeoutException(
                        context.getString(R.string.dialog_error_message_timeout));
            } else {
                exception = new ConnectionException(context.getString(R.string.dialog_error_message));
                sendBugReport(e);
            }

        }
    } else {
        exception = new ConnectionException(context.getString(R.string.dialog_error_message));
    }

    return null;
}

From source file:com.intellij.util.net.HttpConfigurable.java

/**
 * todo [all] It is NOT nessesary to call anything if you obey common IDEA proxy settings;
 * todo if you want to define your own behaviour, refer to {@link com.intellij.util.proxy.CommonProxy}
 *
 * also, this method is useful in a way that it test connection to the host [through proxy]
 *
 * @param url URL for HTTP connection//ww w.  j  av  a  2  s .com
 * @throws IOException
 */
public void prepareURL(String url) throws IOException {
    //setAuthenticator();
    CommonProxy.isInstalledAssertion();

    final URLConnection connection = openConnection(url);
    try {
        connection.setConnectTimeout(3 * 1000);
        connection.setReadTimeout(3 * 1000);
        connection.connect();
        connection.getInputStream();
    } catch (Throwable e) {
        if (e instanceof IOException) {
            throw (IOException) e;
        }
    } finally {
        if (connection instanceof HttpURLConnection) {
            ((HttpURLConnection) connection).disconnect();
        }
    }
}

From source file:net.sf.jabref.logic.net.URLDownload.java

private URLConnection openConnection() throws IOException {
    URLConnection connection = source.openConnection();
    for (Map.Entry<String, String> entry : parameters.entrySet()) {
        connection.setRequestProperty(entry.getKey(), entry.getValue());
    }/*from w  w  w  . java 2 s.c om*/
    if (!postData.isEmpty()) {
        connection.setDoOutput(true);
        try (DataOutputStream wr = new DataOutputStream(connection.getOutputStream())) {
            wr.writeBytes(postData);
        }

    }
    // this does network i/o: GET + read returned headers
    connection.connect();

    return connection;
}

From source file:org.apache.fop.apps.FOURIResolver.java

/**
 * Called by the processor through {@link FOUserAgent} when it encounters an
 * uri in an external-graphic element. (see also
 * {@link javax.xml.transform.URIResolver#resolve(String, String)} This
 * resolver will allow URLs without a scheme, i.e. it assumes 'file:' as the
 * default scheme. It also allows relative URLs with scheme, e.g.
 * file:../../abc.jpg which is not strictly RFC compliant as long as the
 * scheme is the same as the scheme of the base URL. If the base URL is null
 * a 'file:' URL referencing the current directory is used as the base URL.
 * If the method is successful it will return a Source of type
 * {@link javax.xml.transform.stream.StreamSource} with its SystemID set to
 * the resolved URL used to open the underlying InputStream.
 *
 * @param href//from  w  w w . j  av a  2 s .c o  m
 *            An href attribute, which may be relative or absolute.
 * @param base
 *            The base URI against which the first argument will be made
 *            absolute if the absolute URI is required.
 * @return A {@link javax.xml.transform.Source} object, or null if the href
 *         cannot be resolved.
 * @throws javax.xml.transform.TransformerException
 *             Never thrown by this implementation.
 * @see javax.xml.transform.URIResolver#resolve(String, String)
 */
public Source resolve(String href, String base) throws TransformerException {
    Source source = null;

    // data URLs can be quite long so evaluate early and don't try to build a File
    // (can lead to problems)
    source = commonURIResolver.resolve(href, base);

    // Custom uri resolution
    if (source == null && uriResolver != null) {
        source = uriResolver.resolve(href, base);
    }

    // Fallback to default resolution mechanism
    if (source == null) {
        URL absoluteURL = null;
        int hashPos = href.indexOf('#');
        String fileURL;
        String fragment;
        if (hashPos >= 0) {
            fileURL = href.substring(0, hashPos);
            fragment = href.substring(hashPos);
        } else {
            fileURL = href;
            fragment = null;
        }
        File file = new File(fileURL);
        if (file.canRead() && file.isFile()) {
            try {
                if (fragment != null) {
                    absoluteURL = new URL(file.toURI().toURL().toExternalForm() + fragment);
                } else {
                    absoluteURL = file.toURI().toURL();
                }
            } catch (MalformedURLException mfue) {
                handleException(mfue, "Could not convert filename '" + href + "' to URL", throwExceptions);
            }
        } else {
            // no base provided
            if (base == null) {
                // We don't have a valid file protocol based URL
                try {
                    absoluteURL = new URL(href);
                } catch (MalformedURLException mue) {
                    try {
                        // the above failed, we give it another go in case
                        // the href contains only a path then file: is
                        // assumed
                        absoluteURL = new URL("file:" + href);
                    } catch (MalformedURLException mfue) {
                        handleException(mfue, "Error with URL '" + href + "'", throwExceptions);
                    }
                }

                // try and resolve from context of base
            } else {
                URL baseURL = null;
                try {
                    baseURL = new URL(base);
                } catch (MalformedURLException mfue) {
                    handleException(mfue, "Error with base URL '" + base + "'", throwExceptions);
                }

                /*
                 * This piece of code is based on the following statement in
                 * RFC2396 section 5.2:
                 *
                 * 3) If the scheme component is defined, indicating that
                 * the reference starts with a scheme name, then the
                 * reference is interpreted as an absolute URI and we are
                 * done. Otherwise, the reference URI's scheme is inherited
                 * from the base URI's scheme component.
                 *
                 * Due to a loophole in prior specifications [RFC1630], some
                 * parsers allow the scheme name to be present in a relative
                 * URI if it is the same as the base URI scheme.
                 * Unfortunately, this can conflict with the correct parsing
                 * of non-hierarchical URI. For backwards compatibility, an
                 * implementation may work around such references by
                 * removing the scheme if it matches that of the base URI
                 * and the scheme is known to always use the <hier_part>
                 * syntax.
                 *
                 * The URL class does not implement this work around, so we
                 * do.
                 */
                assert (baseURL != null);
                String scheme = baseURL.getProtocol() + ":";
                if (href.startsWith(scheme) && "file:".equals(scheme)) {
                    href = href.substring(scheme.length());
                    int colonPos = href.indexOf(':');
                    int slashPos = href.indexOf('/');
                    if (slashPos >= 0 && colonPos >= 0 && colonPos < slashPos) {
                        href = "/" + href; // Absolute file URL doesn't
                        // have a leading slash
                    }
                }
                try {
                    absoluteURL = new URL(baseURL, href);
                } catch (MalformedURLException mfue) {
                    handleException(mfue, "Error with URL; base '" + base + "' " + "href '" + href + "'",
                            throwExceptions);
                }
            }
        }

        if (absoluteURL != null) {
            String effURL = absoluteURL.toExternalForm();
            try {
                URLConnection connection = absoluteURL.openConnection();
                connection.setAllowUserInteraction(false);
                connection.setDoInput(true);
                updateURLConnection(connection, href);
                connection.connect();
                return new StreamSource(connection.getInputStream(), effURL);
            } catch (FileNotFoundException fnfe) {
                // Note: This is on "debug" level since the caller is
                // supposed to handle this
                log.debug("File not found: " + effURL);
            } catch (java.io.IOException ioe) {
                log.error("Error with opening URL '" + effURL + "': " + ioe.getMessage());
            }
        }
    }
    return source;
}

From source file:org.chromium.net.test.util.TestWebServer.java

/**
 * Terminate the http server./*  www .  j  a  v a  2  s  .c  o m*/
 */
public void shutdown() {
    if (mSsl) {
        setSecureInstance(null);
    } else {
        setInstance(null);
    }

    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);
    }
}

From source file:com.github.thebigs.foscam.recorder.Recorder.java

@Override
public void run() {
    new File(outputDir).mkdirs();
    initTotalBytesSaved();//from  w ww . j ava  2  s .co  m

    while (!shutdown) {
        try {
            URL url = new URL(camUrl);

            // Open a Connection to the server
            URLConnection urlc = url.openConnection();
            if (urlc == null) {
                throw new IOException("Unable to make a connection to the image source");
            }
            // Turn off caches to force fresh reload of the jpg
            urlc.setUseCaches(false);
            urlc.connect(); // ignored if already connected.
            InputStream stream = urlc.getInputStream();

            // Line 1: "--ipcamera"
            String delimiter = new String(readLine(stream));
            while (!shutdown && !imageListeners.isEmpty()) {
                // Line 2: "Content-Type: image/jpeg"
                String contentType = new String(readLine(stream)).split(":")[1].trim();
                // Line 3: "Content-Length: 23304"
                int contentLength = Integer.parseInt(new String(readLine(stream)).split(":")[1].trim());
                // Line 4: <Blank Line>
                readLine(stream);

                // read image data
                byte[] imageData = new byte[contentLength];
                for (int i = 0; i < contentLength; i++) {
                    int readByte = stream.read();
                    imageData[i] = (byte) readByte;
                }

                Image image = Toolkit.getDefaultToolkit().createImage(imageData);
                // save the image to the current recording
                saveImage(image);

                // notify listeners
                synchronized (imageListeners) {
                    for (WebCamImageListener l : imageListeners) {
                        l.onImage(image);
                    }
                }

                // read stream till next delimiter
                boolean delimiterFound = false;
                ByteArrayOutputStream delimiterBuffer = new ByteArrayOutputStream();
                while (!delimiterFound) {
                    int nextByte = stream.read();
                    if (nextByte == 0x2d) {
                        int followingByte = stream.read();
                        if (followingByte == 0x2d) {
                            // read 8 bytes into the delimiter buffer
                            for (int i = 0; i < 8; i++) {
                                delimiterBuffer.write(stream.read());
                            }
                            if (new String(delimiterBuffer.toByteArray()).equals("ipcamera")) {
                                delimiterFound = true;
                                // skip CR/LF
                                stream.skip(2);
                            }
                        }
                    }
                }
            }
        } catch (MalformedURLException e) {
            System.err.println("Unable to parse URL: '" + camUrl + "'");
            System.exit(-1);
        } catch (IOException e) {
            System.err
                    .println("IO Exception: server not responding at : '" + camUrl + "'. retrying in 1 second");

            // sleep for 1 sec
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e1) {
            }
            continue;
        }

        if (imageListeners.isEmpty()) {
            // sleep for 5 secs
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
            }
        }
    }
}

From source file:org.wattdepot.client.http.api.collector.NOAAWeatherCollector.java

@Override
public void run() {
    Measurement meas = null;/*  ww  w.  j a  v  a 2 s . c  o m*/
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setNamespaceAware(true);
    try {
        DocumentBuilder builder = domFactory.newDocumentBuilder();

        // Have to make HTTP connection manually so we can set proper timeouts
        URL url = new URL(noaaWeatherUri);
        URLConnection httpConnection;
        httpConnection = url.openConnection();
        // Set both connect and read timeouts to 15 seconds. No point in long
        // timeouts since the
        // sensor will retry before too long anyway.
        httpConnection.setConnectTimeout(15 * 1000);
        httpConnection.setReadTimeout(15 * 1000);
        httpConnection.connect();

        // Record current time as close approximation to time for reading we are
        // about to make
        Date timestamp = new Date();

        Document doc = builder.parse(httpConnection.getInputStream());

        XPathFactory factory = XPathFactory.newInstance();
        XPath xPath = factory.newXPath();
        // path to the data
        String valString = "/current_observation/" + this.registerName + "/text()";
        XPathExpression exprValue = xPath.compile(valString);
        Object result = new Double(0);
        if (this.registerName.equals("weather")) {
            Object cloudCoverage = exprValue.evaluate(doc, XPathConstants.STRING);
            String cloudStr = cloudCoverage.toString();
            if (cloudStr.equalsIgnoreCase("sunny") || cloudStr.equalsIgnoreCase("clear")) {
                // 0 to 1/8 cloud coverage
                result = new Double(6.25);
            } else if (cloudStr.equalsIgnoreCase("mostly sunny") || cloudStr.equalsIgnoreCase("mostly clear")) {
                // 1/8 to 2/8 cloud coverage
                result = new Double(18.75);
            } else if (cloudStr.equalsIgnoreCase("partly sunny")
                    || cloudStr.equalsIgnoreCase("partly cloudy")) {
                // 3/8 to 5/8 cloud coverage
                result = new Double(50.0);
            } else if (cloudStr.equalsIgnoreCase("mostly cloudy")) {
                // 6/8 to 7/8 cloud coverage
                result = new Double(81.25);
            } else if (cloudStr.equalsIgnoreCase("cloudy")) {
                // 7/8 to 100% cloud coverage
                result = new Double(93.75);
            }
        } else {
            result = exprValue.evaluate(doc, XPathConstants.NUMBER);
        }

        Double value = (Double) result;
        meas = new Measurement(definition.getSensorId(), timestamp, value, measUnit);
    } catch (MalformedURLException e) {
        System.err.format("URI %s was invalid leading to malformed URL%n", noaaWeatherUri);
    } catch (XPathExpressionException e) {
        System.err.println("Bad XPath expression, this should never happen.");
    } catch (ParserConfigurationException e) {
        System.err.println("Unable to configure XML parser, this is weird.");
    } catch (SAXException e) {
        System.err.format("%s: Got bad XML from eGauge sensor %s (%s), hopefully this is temporary.%n",
                Tstamp.makeTimestamp(), sensor.getName(), e);
    } catch (IOException e) {
        System.err.format(
                "%s: Unable to retrieve data from eGauge sensor %s (%s), hopefully this is temporary.%n",
                Tstamp.makeTimestamp(), sensor.getName(), e);
    }

    if (meas != null) {
        try {
            this.client.putMeasurement(depository, meas);
        } catch (MeasurementTypeException e) {
            System.err.format("%s does not store %s measurements%n", depository.getName(),
                    meas.getMeasurementType());
        }
        if (debug) {
            System.out.println(meas);
        }
    }
}

From source file:com.yglab.openapi.youtube.caption.Video.java

public InputStreamReader readURL(String s) throws MalformedURLException, IOException {
    URL url;/* w  w w.  jav  a  2 s  .c o m*/
    InputStreamReader isr;
    String appName, appVersion;

    appName = "ScanNews";
    appVersion = "1.0.0";

    url = new URL(s);
    URLConnection urlconn = url.openConnection();
    urlconn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
    urlconn.setRequestProperty("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
    urlconn.setRequestProperty("User-Agent", "Mozilla/5.0 (compatible; " + appName + "/" + appVersion + ")");
    urlconn.connect();

    isr = new InputStreamReader(urlconn.getInputStream(), "UTF-8");

    return isr;
}