Java URL Download get(String url)

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

Description

Run a (blocking) HTTP get request.

License

Open Source License

Parameter

Parameter Description
url url to send request to (including all arguments etc).

Return

Response, empty if an exception is thrown, won't be null.

Declaration

public static String get(String url) 

Method Source Code


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

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 = "Mozilla/4.76";

    /**/*w w  w .j ava 2s . c  om*/
     * Run a (blocking) HTTP get request.
     *
     * @param url url to send request to (including all arguments etc).
     * @return Response, empty if an exception is thrown, won't be null.
     */
    public static String get(String url) {
        String result = "";
        try {
            HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
            connection.setInstanceFollowRedirects(true);
            connection.setRequestMethod("GET");
            connection.setRequestProperty("User-Agent", USER_AGENT);
            connection.setConnectTimeout(3000);
            connection.setReadTimeout(10000);
            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            StringBuilder builder = new StringBuilder();
            String line;
            while ((line = in.readLine()) != null) {
                builder.append(line);
            }
            in.close();
            result = builder.toString();
        } catch (IOException ignored) {
        }
        return result;
    }
}

Related

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