get From URL - Java Network

Java examples for Network:URL Download

Description

get From URL

Demo Code


//package com.java2s;
import java.io.*;
import java.net.*;

public class Main {
    public static String getFrom(String URL) throws Exception {
        StringBuffer res = new StringBuffer("");
        try {/*from w ww .  j  a va  2s  .c  om*/
            URL url = new URL(URL);
            URLConnection postUrlConnection = url.openConnection();
            postUrlConnection.setDoOutput(true);
            postUrlConnection.setUseCaches(false);
            postUrlConnection.setRequestProperty("Accept",
                    "application/rdf+xml");

            InputStream postInputStream = postUrlConnection
                    .getInputStream();
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    postInputStream));

            char[] buff = new char[1024 * 100];
            int n;
            int i = 0;
            do {
                n = in.read(buff);
                if (n != -1) {
                    res.append(new String(buff, 0, n));
                }
                i += n;
            } while (n != -1);
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res.toString();
    }
}

Related Tutorials