Java URL Download get(String url)

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

Description

get

License

Open Source License

Declaration

public static String get(String url) throws IOException 

Method Source Code

//package com.java2s;
/**/*w  w  w  .j a  va2s.  c  om*/
 * Copyright (c) 2010-2016 by the respective copyright holders.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 */

import java.io.BufferedReader;

import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class Main {
    private static final String USER_AGENT = "Java";

    public static String get(String url) throws IOException {
        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        con.setRequestMethod("GET");
        con.setRequestProperty("User-Agent", USER_AGENT);

        int responseCode = con.getResponseCode();
        if (responseCode != 200) {
            throw new IOException("Response Code: " + responseCode);
        }
        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        try {
            StringBuilder response = new StringBuilder();
            String line;
            while ((line = in.readLine()) != null) {
                response.append(line);
            }
            return response.toString();
        } finally {
            in.close();
        }
    }
}

Related

  1. fetchURL(String urlStr, List errors, Map requestProperties)
  2. fetchUrl(URL url)
  3. fetchURL(URL url)
  4. fetchUrl(URL url)
  5. fetchURLContents(String url)
  6. get(String url)
  7. get(String url)
  8. get(String url)
  9. get(String url, int timeout, Map header)