Java tutorial
//package com.java2s; /** * Copyright (C) 2010 Peter Karich <jetwick_@_pannous_._info> * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.URL; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; public class Main { public static Document getUrlAsDocument(String urlAsString, int timeout) throws Exception { URL url = new URL(urlAsString); //using proxy may increase latency HttpURLConnection hConn = (HttpURLConnection) url.openConnection(); hConn.setReadTimeout(timeout); hConn.setConnectTimeout(timeout); // hConn.setRequestProperty("Accept-Encoding", "gzip, deflate"); InputStream is = hConn.getInputStream(); // if ("gzip".equals(hConn.getContentEncoding())) // is = new GZIPInputStream(is); return newDocumentBuilder().parse(is); } public static String getInputStream(InputStream is) throws IOException { if (is == null) throw new IllegalArgumentException("stream mustn't be null!"); BufferedReader bufReader = createBuffReader(is); StringBuilder sb = new StringBuilder(); String line; while ((line = bufReader.readLine()) != null) { sb.append(line); sb.append('\n'); } bufReader.close(); return sb.toString(); } public static DocumentBuilder newDocumentBuilder() throws ParserConfigurationException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); // ist das langsammer: factory.setValidating(true); factory.setValidating(false); factory.setNamespaceAware(false); DocumentBuilder builder = factory.newDocumentBuilder(); return builder; } public static BufferedReader createBuffReader(File file) throws FileNotFoundException, UnsupportedEncodingException { return new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8")); } public static BufferedReader createBuffReader(InputStream is) throws FileNotFoundException, UnsupportedEncodingException { return new BufferedReader(new InputStreamReader(is, "UTF-8")); } }