vendredi 2 décembre 2016

Reading source code from url

I had a method to read a source code pag and transform it to an string array, the thing is I had to put diferent input streams, and I ended up mixing up the whole method, how do I make this work??

public static String[]  getUrlSource(String site) throws IOException {
    List<String> myList = new ArrayList<String>();
    URL url = new URL(site);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // Cast shouldn't fail
    HttpURLConnection.setFollowRedirects(true);
    conn.setRequestProperty("Accept-Encoding", "gzip, deflate");
    String encoding = conn.getContentEncoding();
    InputStream inStr = null;


    if (encoding != null && encoding.equalsIgnoreCase("gzip")) {
        inStr = new GZIPInputStream(conn.getInputStream());

    } else {
        inStr = conn.getInputStream();
    }

    BufferedReader in = new BufferedReader(inStr);
    String inputLine;
    while ((inputLine = in.readLine()) != null)
        myList.add(inputLine);
    in.close();

    String[] arr = myList.toArray(new String[myList.size()]);
    return arr;

    }
}




Aucun commentaire:

Enregistrer un commentaire