Example usage for java.net URL openConnection

List of usage examples for java.net URL openConnection

Introduction

In this page you can find the example usage for java.net URL openConnection.

Prototype

public URLConnection openConnection() throws java.io.IOException 

Source Link

Document

Returns a java.net.URLConnection URLConnection instance that represents a connection to the remote object referred to by the URL .

Usage

From source file:Main.java

private static InputStream getInputStreamFromURL(String urlStr) {
    HttpURLConnection urlConn = null;
    InputStream inputStream = null;

    try {//from   w w  w .ja v a2  s .  co  m
        URL url = new URL(urlStr);
        urlConn = (HttpURLConnection) url.openConnection();
        inputStream = urlConn.getInputStream();
    } catch (IOException e) {
        return null;
    }
    return inputStream;
}

From source file:Main.java

public static InputStream getRequest(String path) throws Exception {
    URL url = new URL(path);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("GET");
    conn.setConnectTimeout(5000);// w  ww . j a v a 2s.c o  m
    if (conn.getResponseCode() == 200) {
        return conn.getInputStream();
    }
    return null;
}

From source file:Main.java

private static HttpURLConnection getConnection(URL url) throws IOException {
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("GET");
    conn.setDoInput(true);//w  w w.  j  a  v  a2s  . c  o  m
    conn.setDoOutput(true);
    conn.setRequestProperty("Accept", "text/xml");
    return conn;
}

From source file:Main.java

static void getHTTPXml(URL url) throws Exception {
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("POST");
    conn.setRequestProperty("ACCEPT", "application/xml");
    InputStream xml = conn.getInputStream();

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    org.w3c.dom.Document document = builder.parse(xml);

    System.out.println(document);
    String doctype = conn.getContentType();
    System.out.println(doctype);//from www  . j a va  2 s  .co  m

    XPathFactory pathFactory = XPathFactory.newInstance();
    XPath path = pathFactory.newXPath();
    XPathExpression expression;
    expression = path.compile("/result/checkid");
    NodeList nodeList = (NodeList) expression.evaluate(document, XPathConstants.NODESET);
    String checkids[] = getNodeValue(nodeList);
    for (String checkid : checkids) {
        System.out.print(checkid + ", ");
    }
    conn.disconnect();
}

From source file:Main.java

/**
 * Makes an HTTP request using GET method to the specified URL.
 *
 * @param requestURL/* w w w.  jav a  2s.c o  m*/
 *            the URL of the remote server
 * @return An HttpURLConnection object
 * @throws IOException
 *             thrown if any I/O error occurred
 */
public static HttpURLConnection sendGetRequest(String requestURL) throws IOException {
    URL url = new URL(requestURL);
    httpConn = (HttpURLConnection) url.openConnection();
    httpConn.setUseCaches(false);

    httpConn.setDoInput(true); // true if we want to read server's response
    httpConn.setDoOutput(false); // false indicates this is a GET request

    return httpConn;
}

From source file:com.digitallizard.bbcnewsreader.resource.web.ImageDownloader.java

public static byte[] getImage(URL url) throws Exception {
    URLConnection connection = url.openConnection();

    InputStream stream = connection.getInputStream();
    BufferedInputStream inputbuffer = new BufferedInputStream(stream, 8000);

    ByteArrayBuffer arraybuffer = new ByteArrayBuffer(50);
    int current = 0;
    while ((current = inputbuffer.read()) != -1) {
        arraybuffer.append((byte) current);
    }/*from  www . j  a v a 2  s  . c  om*/

    byte[] image = arraybuffer.toByteArray();

    return image;
}

From source file:Main.java

/** 
 * Get image from newwork /* w  w w.j  a v  a2  s . c om*/
 * @param path The path of image 
 * @return InputStream 
 * @throws Exception 
 */
public static InputStream getImageStream(String path) throws Exception {
    URL url = new URL(path);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setConnectTimeout(5 * 1000);
    conn.setRequestMethod("GET");
    if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
        return conn.getInputStream();
    }
    return null;
}

From source file:Main.java

private static InputStream getHttpConnection(String urlString) throws IOException {
    InputStream stream = null;//from   w  w  w. j  a v  a2  s  .  co m
    URL url = new URL(urlString);
    URLConnection connection = url.openConnection();

    try {
        HttpURLConnection httpConnection = (HttpURLConnection) connection;
        httpConnection.setRequestMethod("GET");
        httpConnection.connect();

        if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
            stream = httpConnection.getInputStream();
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return stream;
}

From source file:Main.java

public static Bitmap getBitmap(String path) {
    try {//from  ww  w  .ja va2 s  . c o m
        URL url = new URL(path);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        InputStream is = conn.getInputStream();
        return BitmapFactory.decodeStream(is);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

/**
 * Makes HttpURLConnection and returns InputStream
 *///from  w  ww . j  a va  2s  .  c  om
private static InputStream getHttpConnection(String urlString) throws IOException {
    InputStream stream = null;
    URL url = new URL(urlString);
    URLConnection connection = url.openConnection();

    try {
        HttpURLConnection httpConnection = (HttpURLConnection) connection;
        httpConnection.setRequestMethod("GET");
        httpConnection.connect();

        if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
            stream = httpConnection.getInputStream();
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return stream;
}