• Apache 4.x HttpClient


    public static Map callRequest(String requestUrl, Method method, Map<String, String> data) throws IOException {
    
        CloseableHttpResponse response;
    
        try {
            List<NameValuePair> nvps = new ArrayList<NameValuePair>();
            for(String key: data.keySet()) {
                nvps.add(new BasicNameValuePair(key, data.get(key)));
            }
            switch (method){
                case GET:{
                    HttpGet httpGet = new HttpGet(requestUrl+"&"+ EntityUtils.toString(new UrlEncodedFormEntity(nvps, Consts.UTF_8)));
                    response = httpclient.execute(httpGet);
                    return parseResponse(response);
                }
                case DELETE:{
                    response = httpclient.execute(new HttpDelete(requestUrl));
                    return parseResponse(response);
                }
                case PUT:{
                    HttpPut httpPut = new HttpPut(requestUrl);
                    httpPut.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8));
                    response = httpclient.execute(httpPut);
                    return parseResponse(response);
                }
                case POST:{
                    String post = Http4Client.post(requestUrl, data);
                    return doParse(post);
                }
                default:{
                    throw new APIException("Unsupport request METHOD");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    //        finally {
    //            httpclient.close();
    //        }
    
        return null;
    }
  • 相关阅读:
    MFC弹出模拟对话框
    ansible中的playbook详解
    django后台管理--添加自定义action
    django后台管理-ModelAdmin对象
    django表单的api
    puppet的配置
    git常用命令
    HAProxy的日志配置以及ACL规则实现负载均衡
    HAproxy的安装与配置讲解
    实时监测网络流量
  • 原文地址:https://www.cnblogs.com/slankka/p/9158491.html
Copyright © 2020-2023  润新知