public static String sendHttpRequest(String httpURL, Map< String, String> params) throws Exception {
URL url=new URL(httpURL);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("POST");
urlConnection.setDoOutput(true);
if (params!=null && params.size()>0){
StringBuilder strBuf=new StringBuilder();
for (Map.Entry<String, String> entry : params.entrySet()) {
strBuf.append("&").append(entry.getKey()).append("=").append(entry.getValue());
}
//去除第一个&
urlConnection.getOutputStream().write(strBuf.substring(1).toString().getBytes("UTF-8"));
}
urlConnection.connect();
String res = StreamUtils.copyToString(urlConnection.getInputStream(), Charset.forName("UTF-8"));
return res;
}
调用api方法
于 2020-12-15 14:26:34 首次发布