Java URL Download downloadWebpage(String url)

Here you can find the source of downloadWebpage(String url)

Description

download Webpage

License

Open Source License

Declaration

public static String downloadWebpage(String url) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;

public class Main {
    public static String downloadWebpage(String url) {
        try {/*w  ww .  ja va2 s .  com*/
            return downloadWebpage(new URL(url));
        } catch (MalformedURLException e) {
            //Not a valid URL
            return null;
        }
    }

    public static String downloadWebpage(URL url) {

        InputStream is = null;
        BufferedReader br;

        try {
            //         Pattern p = Pattern.compile("text/html;\\s+charset=([^\\s]+)\\s*");
            //         Matcher m = p.matcher(con.getContentType());
            //
            //         String charset = m.matches()?m.group(1):"UTF-8";
            //
            //         Reader r = new InputStreamReader(con.getInputStream(), charset);

            is = url.openStream();
            br = new BufferedReader(new InputStreamReader(is));

            int ch;
            StringBuilder buf = new StringBuilder();

            while ((ch = br.read()) != -1)
                buf.append((char) ch);

            return buf.toString();
        } catch (UnsupportedEncodingException e) {
            return null;
        } catch (IOException e) {
            return null;
        } finally {
            try {
                if (is != null)
                    is.close();
            } catch (IOException e) {
            }
        }
    }
}

Related

  1. downloadURL(URL url, File file)
  2. downloadURL(URL url, String localPath)
  3. downloadUrlToFile(String surl, File file, String method)
  4. downloadUrlToFile(URL url, File file)
  5. downloadUrlToFile(URL url, File result)
  6. downloadZip(URL file, File dump)
  7. downlod(String url, File dest)
  8. downNetImg(String filePath, String remotePath, String htmlUrl, String fileName)
  9. downPicture(String filePath, String imageUrl, String fileName)