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:MyServlet.java

public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

    ServletOutputStream out = res.getOutputStream();
    res.setContentType("text/plain");

    String file = req.getPathInfo();
    if (file == null) {
        out.println("Extra path info was null; should be a resource to view");
        return;//  w w w.j a va 2 s .  co m
    }

    URL url = getServletContext().getResource(file);
    if (url == null) {
        out.println("Resource " + file + " not found");
        return;
    }

    URLConnection con = null;
    try {
        con = url.openConnection();
        con.connect();
    } catch (IOException e) {
        out.println("Resource " + file + " could not be read: " + e.getMessage());
        return;
    }
}

From source file:org.openmrs.scheduler.tasks.CheckInternetConnectivityTask.java

/**
 * @see org.openmrs.scheduler.tasks.AbstractTask#execute()
 *//* w w w  . jav  a  2s.co  m*/
public void execute() {

    // TODO url should be provided as a property to taskconfig
    String url = "http://www.google.com:80/index.html";
    try {
        URLConnection connection = new URL(url).openConnection();
        connection.connect();
    } catch (IOException ioe) {
        try {
            if (!Context.isAuthenticated()) {
                authenticate();
            }
            String text = "At " + new Date()
                    + " there was an error reported connecting to the internet address " + url + ": " + ioe;
            // TODO role should be provided as a property to taskconfig
            Role role = Context.getUserService().getRole("System Developer");
            Collection<User> users = Context.getUserService().getUsersByRole(role);
            Context.getAlertService().saveAlert(new Alert(text, users));
        } catch (Exception e) {
            // Uh oh, just log it.
            log.error(e);
        }
    }
}

From source file:com.nominanuda.web.mvc.URLStreamer.java

public HttpResponse handle(HttpRequest request) throws IOException {
    URL url = getURL(request);//from  w  w  w .  j  a va  2  s  .com
    URLConnection conn = url.openConnection();
    conn.connect();
    int len = conn.getContentLength();
    InputStream is = conn.getInputStream();
    String ce = conn.getContentEncoding();
    String ct = determineContentType(url, conn);
    if (len < 0) {
        byte[] content = ioHelper.readAndClose(is);
        is = new ByteArrayInputStream(content);
        len = content.length;
    }
    StatusLine statusline = httpCoreHelper.statusLine(SC_OK);
    HttpResponse resp = new BasicHttpResponse(statusline);
    resp.setEntity(new InputStreamEntity(is, len));
    httpCoreHelper.setContentType(resp, ct);
    httpCoreHelper.setContentLength(resp, len);//TODO not needed ??
    if (ce != null) {
        httpCoreHelper.setContentEncoding(resp, ce);
    }
    return resp;
}

From source file:org.nhnnext.android.androidnaming.ImageDownload.java

public void copy_img(String url, String save_name) {

    File img_cache_path;//  w w  w .j av a  2 s  . c o m

    img_cache_path = new File(HomeView.FILES_DIR + save_name);
    if (!img_cache_path.exists()) {

        // ************************************
        try {
            URL Url = new URL(url);
            URLConnection conn = Url.openConnection();
            conn.connect();
            InputStream is = conn.getInputStream();

            BufferedInputStream bis = new BufferedInputStream(is);
            ByteArrayBuffer baf = new ByteArrayBuffer(50);

            int current = 0;

            while ((current = bis.read()) != -1) {
                baf.append((byte) current);
            }

            FileOutputStream fos = context.openFileOutput(save_name, 0);
            fos.write(baf.toByteArray());
            fos.close();
        } catch (IOException e) {

        }

    }

}

From source file:org.nhnnext.android.day3.Img_Downloader.java

public void copy_img(String url, String save_name) {

    File img_cache_path;/* www.  ja v  a2 s .  c  om*/

    img_cache_path = new File(MainActivity.FILES_DIR + save_name);
    if (!img_cache_path.exists()) {

        // ************************************
        try {
            URL Url = new URL(url);
            URLConnection conn = Url.openConnection();
            conn.connect();
            InputStream is = conn.getInputStream();

            BufferedInputStream bis = new BufferedInputStream(is);
            ByteArrayBuffer baf = new ByteArrayBuffer(50);

            int current = 0;

            while ((current = bis.read()) != -1) {
                baf.append((byte) current);
            }

            FileOutputStream fos = context.openFileOutput(save_name, 0);
            fos.write(baf.toByteArray());
            fos.close();
        } catch (IOException e) {

        }

    }

}

From source file:org.nhnnext.android.day5_simple.ImageDownloader.java

public void copy_img(String url, String save_name) {

    File img_cache_path;//w  ww .  j  a  v a2s  .c  om

    img_cache_path = new File(NextgramController.FILES_DIR + save_name);
    if (!img_cache_path.exists()) {

        // ************************************
        try {
            URL Url = new URL(url);
            URLConnection conn = Url.openConnection();
            conn.connect();
            InputStream is = conn.getInputStream();

            BufferedInputStream bis = new BufferedInputStream(is);
            ByteArrayBuffer baf = new ByteArrayBuffer(50);

            int current = 0;

            while ((current = bis.read()) != -1) {
                baf.append((byte) current);
            }

            FileOutputStream fos = context.openFileOutput(save_name, 0);
            fos.write(baf.toByteArray());
            fos.close();
        } catch (IOException e) {

        }

    }

}

From source file:org.nhnnext.android.basic.ImageDownload.java

public void copy_img(String url, String save_name) {

    File img_cache_path;//  w w  w .  j  a v a2 s  .com

    img_cache_path = new File(pref.getString(context.getString(R.string.files_directory), "") + save_name);
    if (!img_cache_path.exists()) {

        // ************************************
        try {
            URL Url = new URL(url);
            URLConnection conn = Url.openConnection();
            conn.connect();
            InputStream is = conn.getInputStream();

            BufferedInputStream bis = new BufferedInputStream(is);
            ByteArrayBuffer baf = new ByteArrayBuffer(50);

            int current = 0;

            while ((current = bis.read()) != -1) {
                baf.append((byte) current);
            }

            FileOutputStream fos = context.openFileOutput(save_name, 0);
            fos.write(baf.toByteArray());
            fos.close();
        } catch (IOException e) {

        }

    }

}

From source file:ste.xtest.net.BugFreeXTestHandler.java

@Test
public void registration() throws Exception {
    final String TEST_URL = "xtest://steak";

    URL url = new URL(TEST_URL);
    URLConnection conn = url.openConnection();
    conn.connect();

    ////from w w w  .j  a v a2s  .c o  m
    // The default implenmentation retuns as body of the response the
    // requested URL
    //
    assertEquals(TEST_URL, new String(IOUtils.toByteArray(conn)));
}

From source file:ste.xtest.net.BugFreeXTestHandler.java

public void statusCode(final int code) throws Exception {
    URL url = new URL("xtest://test.nowhere.com");
    XTestHandler.statusCode = code;/*from w w  w  .  ja v a2s .  c o  m*/

    final String TEST_MSG = String.format("server returned HTTP response code [%d] for URL [%s]", code, url);

    URLConnection conn = url.openConnection();
    conn.connect();

    try {
        IOUtils.toString(conn.getInputStream());
        fail("IOException not thrown");
    } catch (IOException x) {
        assertEquals(TEST_MSG, x.getMessage());
    }

}

From source file:org.mycore.common.content.MCRURLContent.java

@Override
public String getETag() throws IOException {
    URLConnection openConnection = url.openConnection();
    openConnection.connect();
    String eTag = openConnection.getHeaderField("ETag");
    if (eTag != null) {
        return eTag;
    }/*from  w ww.j  a v  a2  s  .  c o  m*/
    long lastModified = openConnection.getLastModified();
    long length = openConnection.getContentLengthLong();
    eTag = getSimpleWeakETag(url.toString(), length, lastModified);
    return eTag == null ? null : eTag.substring(2);
}