Java URL Download get(String url, Map params)

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

Description

get

License

Apache License

Declaration

public static String get(String url, Map<String, String[]> params) throws Exception 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.net.HttpURLConnection;
import java.net.URL;

import java.util.Map;

public class Main {
    private static final String USER_AGENT = "Mozilla/5.0";

    public static String get(String url) throws Exception {
        return get(url, "");
    }/*from   w  w w.  j  a v a  2 s. com*/

    public static String get(String url, Map<String, String[]> params) throws Exception {
        StringBuilder sb = new StringBuilder();
        if (!params.isEmpty()) {
            sb.append("?");
        }
        int idx = 0;
        for (String column : params.keySet()) {
            if (idx > 0) {
                sb.append("&");
            }
            ;
            sb.append(column);
            sb.append("=");
            sb.append(params.get(column)[0]);
            idx++;
        }
        return get(url, sb.toString());
    }

    public static String get(String url, String urlParameters) throws Exception {

        String lUrl = url + urlParameters;
        //      System.out.println(lUrl);

        URL obj = new URL(lUrl);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();

        // optional default is GET
        con.setRequestMethod("GET");

        //add request header
        con.setRequestProperty("User-Agent", USER_AGENT);

        //      int responseCode = con.getResponseCode();

        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

        return response.toString();
    }
}

Related

  1. get(String url)
  2. get(String url)
  3. get(String url)
  4. get(String url, int timeout, Map header)
  5. get(String url, Map headers)
  6. get(String url, String encoding)
  7. get(String urlStr)
  8. get(String urlString)
  9. get(URL host, String endpoint, String customer, String name, String version, OutputStream out)