Java HTTP Request request(String httpUrl, Map httpArgMap)

Here you can find the source of request(String httpUrl, Map httpArgMap)

Description

request

License

Apache License

Declaration

public static String request(String httpUrl, Map<String, String> httpArgMap) 

Method Source Code


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

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
import java.util.Set;

public class Main {
    public static String request(String httpUrl, Map<String, String> httpArgMap) {
        return request(httpUrl, httpArgMap, null);
    }//w  ww .  j a  v a 2 s  . co  m

    public static String request(String httpUrl, Map<String, String> httpArgMap, String apikey) {
        BufferedReader reader = null;
        String result = null;
        StringBuffer sbf = new StringBuffer();
        httpUrl = httpUrl + "?";
        Set<String> keySet = httpArgMap.keySet();
        for (String string : keySet) {
            httpUrl = httpUrl + string + "=" + httpArgMap.get(string) + "&";
        }
        httpUrl = httpUrl.replaceAll(" ", "");
        try {
            URL url = new URL(httpUrl.substring(0, httpUrl.length() - 1));
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            if (apikey != null) {
                connection.setRequestProperty("apikey", apikey);
            }
            connection.connect();
            InputStream is = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            String strRead = null;
            while ((strRead = reader.readLine()) != null) {
                sbf.append(strRead);
                sbf.append("\r\n");
            }
            reader.close();
            result = sbf.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}

Related

  1. getRequest(String urlString)
  2. getRequest(URL url, int timeout)
  3. getRequestContent(String urlText)
  4. makeUrlRequest(String surl, String data, String method)
  5. request(boolean quiet, String method, URL url, Map body)
  6. request(String url, int timeout, String method, Map header)
  7. request(String url, Map cookies, Map parameters)
  8. requestData(String url)
  9. requestDataFromUrl(URL url, byte[] tosend, String userAgent)