java 发送getpost带参请求返回json数据

public class HttpRequest {
    public static JSONObject SendPost(String url, Map map) {
        CloseableHttpClient client = HttpClients.createDefault();
        HttpPost post = new HttpPost(url);
        // 设置参数
        List formparams = new ArrayList();
        for (Map.Entry entry : map.entrySet()) {
            formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
        }
        UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(formparams, Consts.UTF_8);
        post.setEntity(formEntity);
        try {
            HttpResponse httpResponse = client.execute(post);
            HttpEntity entity = httpResponse.getEntity();
            String data = entity.toString();
            System.out.println("获取的数据" + data);
            InputStream instreams = entity.getContent();
            String str = ConvertStreamToString(instreams);
            System.out.println("Response:" + "\n" + str);
            JSONObject jsonObject = JSONObject.fromObject(JSON.parseObject(str));
        /*    JSONArray family = jsonObject.getJSONArray("monitorList");
            for (int i=0;i map) {
        CloseableHttpClient client = HttpClients.createDefault();
        String urlget = url;
        String token="";
       Integer count=1;
       if (map!=null){
        for (Map.Entry entry : map.entrySet()) {
            if (entry.getKey().equals("Ttoken")){
                token=entry.getValue();
                continue;
            }
            if (count==1){
                urlget=urlget+("?" + entry.getKey() + "=" + entry.getValue());
            }
            else {
                urlget=urlget+("&&" + entry.getKey() + "=" + entry.getValue());
            }
            System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
            count=count+1;
        }
       }
        org.apache.http.client.methods.HttpGet get = new org.apache.http.client.methods.HttpGet(urlget);

        get.setHeader("Accept", "application/json;charset=utf-8");
        if (token!=null&&!token.equals("")){
            get.setHeader("Ttoken",token);
        }
        try {
            HttpResponse httpResponse = client.execute(get);
            HttpEntity entity = httpResponse.getEntity();
            String data = entity.toString();
            System.out.println("获取的数据" + data);
            InputStream instreams = entity.getContent();
            String str = ConvertStreamToString(instreams);
            System.out.println("Response:" + "\n" + str);
            JSONObject jsonObject = JSONObject.fromObject(JSON.parseObject(str));
            return jsonObject;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    // Convert stream to string
    public static String ConvertStreamToString(InputStream is) {

        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();

        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
        } catch (IOException e) {
            System.out.println("Error=" + e.toString());
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                System.out.println("Error=" + e.toString());
            }
        }
        return sb.toString();

    }
}

你可能感兴趣的:(后端发送post带参请求,java,后端发送post带参请求,java)