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;
    }


    听说学习能够让青春永驻。
  • 相关阅读:
    jsp报源码
    c#简单写售票系统
    linux常用命令大全[转]
    【转载】大型网站渗透思之信息收集
    Ajax初窥
    屏蔽win10中文输入法
    win10禁止更新的方法
    win10进入到安全模式的三种方法
    7代CPU安装win7的方法
    python的输出问题
  • 原文地址:https://www.cnblogs.com/chenyf/p/8512556.html
Copyright © 2011-2022 走看看