欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

java代码发送body参数。

时间:2023-07-01

jar

com.alibaba fastjson 1.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

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。