jar
com.alibaba fastjson1.2.21
public static String post(String requestUrl, JSonObject parm) throws Exception{
//响应的内容
StringBuffer stringBuffer = new StringBuffer();
URL url = new URL(requestUrl);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");//请求post方式
connection.setDoInput(true);
connection.setDoOutput(true);
//header内的的参数在这里设置 setRequestProperty("健, "值");
connection.setRequestProperty("Content-Type", "application/json");
//connection.setRequestProperty("", "");
connection.connect();
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(connection.getOutputStream(),"UTF-8");
outputStreamWriter.write(parm.toString());
outputStreamWriter.flush();
InputStream inputStream = connection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
String strRead = null;
while ((strRead = bufferedReader.readLine()) != null) {
stringBuffer.append(strRead);
stringBuffer.append("rn");
}
bufferedReader.close();
connection.disconnect();
String results = stringBuffer.toString();
return results;
}
参考: https://blog.csdn.net/weixin_42620563/article/details/114235588



