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


听说学习能够让青春永驻。
原文地址:https://www.cnblogs.com/chenyf/p/8512556.html