zoukankan      html  css  js  c++  java
  • Map中的key值按字典排序,客户端发送http请求

    /**
    * 获得参数格式化字符串
    * 参数名按字典排序,小写在后面
    */
    private String getFormatParams(Map<String,String> params){
    List<Map.Entry<String, String>> infoIds = new ArrayList<Map.Entry<String, String>>(params.entrySet());
    Collections.sort(infoIds, new Comparator<Map.Entry<String, String>>() {
    public int compare(Map.Entry<String, String> arg0, Map.Entry<String, String> arg1) {
    return (arg0.getKey()).compareTo(arg1.getKey());
    }
    });
    String ret = "";

    for (Map.Entry<String, String> entry : infoIds) {
    ret += entry.getKey();
    ret += "=";
    ret += entry.getValue();
    ret += "&";
    }
    ret = ret.substring(0, ret.length() - 1);
    return ret;
    }

    /**
    * 发送http请求,返回响应数据
    * @param requestUrl
    * @return
    */
    private String sendHttpRequest(String requestUrl){
    CloseableHttpClient httpCilent = HttpClients.createDefault();
    HttpGet httpGet = new HttpGet(requestUrl);
    String result = "";
    try {
    HttpResponse httpResponse = httpCilent.execute(httpGet);
    result = EntityUtils.toString(httpResponse.getEntity());//获得返回的结果
    } catch (IOException e) {
    e.printStackTrace();
    }finally {
    try {
    httpCilent.close();//释放资源
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    return result;
    }


    听说学习能够让青春永驻。
  • 相关阅读:
    提出的问题
    2020软件工程作业02
    2020软件工程作业01
    我当社畜这些年-1
    Electron-vue实现后台多进程(二)
    Electron-vue实现后台多进程(一)
    pytorch自定义loss函数的几种方法
    XGBoost原理
    transformers的bert预训练模型的返回值简要描述
    sklearn下的ROC与AUC原理详解
  • 原文地址:https://www.cnblogs.com/chenyf/p/8512556.html
Copyright © 2011-2022 走看看