远程post请求调用

java 复制代码
 /**
     * 远程post请求
     *
     * @param urlStr
     * @param param
     * @return
     */
    public static String doPost(String urlStr, String param,String clientId,String  authorization) {
        HttpURLConnection connection = null;
        InputStream is = null;
        InputStreamReader rsd = null;
        BufferedReader br = null;
        OutputStream os = null;
        OutputStreamWriter osw = null;
        BufferedWriter bw = null;
        StringBuffer sb = new StringBuffer();

        try {
            URL url = new URL(urlStr);
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setUseCaches(false);    //不缓存
            connection.setRequestProperty("connection", "Keep-Alive");    //设置保活连接
            connection.setRequestProperty("charset", "UTF-8");            //提交的数据编码
            connection.setRequestProperty("Content-type", "application/json");    //提交的数据格式
            connection.setRequestProperty("accept", "application/json");        //接收的数据格式
            connection.setRequestProperty("clientId", clientId);        //登录认证
            connection.setRequestProperty("authorization", authorization);        //登录认证
            connection.setConnectTimeout(90000);    //30秒连接超时
            connection.setReadTimeout(90000);        //30秒读取超时
            connection.connect();
            if (param != null && !"".equals(param)) {
                os = connection.getOutputStream();
                osw = new OutputStreamWriter(os);
                bw = new BufferedWriter(osw);
                bw.write(param);
                bw.flush();
            }
            int status = connection.getResponseCode();
            if (status == 200) {
                is = connection.getInputStream();
                rsd = new InputStreamReader(is, "UTF-8");
                br = new BufferedReader(rsd);
                String s;
                while ((s = br.readLine()) != null) {
                    sb.append(s);
                }
            } else {
                return "{\"ResuleState\":\"-1\",\"Msg\":\"连接异常\"}";
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (ProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null) {
                    br.close();
                }
                if (rsd != null) {
                    rsd.close();
                }
                if (is != null) {
                    is.close();
                }
                if (bw != null) {
                    bw.close();
                }
                if (osw != null) {
                    osw.close();
                }
                if (os != null) {
                    os.close();
                }
                if (connection != null) {
                    connection.disconnect();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }

调用示例:

java 复制代码
    Map<String, Object> personInfoMap = new HashMap<>();
    personInfoMap.put("xxx", ""); 
    String params = JSONObject.toJSONString(personInfoMap);
	//调用接口发送数据
    String jsonStr = BeanUtiles.doPost(urlStr, params, clientId, authorization);
	//处理返回信息
    JSONObject jsonObject = JSONObject.parseObject(jsonStr);
    Integer code = jsonObject.getInteger("code");
    String msg = jsonObject.getString("msg");
    if (code == 200) {
       //调用成功
       String returnData= jsonObject.getString("data");
    } else {
       //调用失败
    }
相关推荐
摇滚侠10 分钟前
SpringMVC 入门到实战 文件上传 75-77
java·后端·spring·maven·intellij-idea
GIS数据转换器18 分钟前
城市排水生命线安全运行监测平台深度解析
java·运维·人工智能·python·安全·数据挖掘·无人机
华如锦1 小时前
面了很多 Java转AI Agent方向,一些面试题总结
java·开发语言·人工智能·python·ai
睡不醒男孩0308231 小时前
CLup 6.x 版本中针对StarRocks 存算一体集群的完整操作手册
java·服务器·网络·clup
程序员黑豆2 小时前
Java中怎么实现字符串拼接呢【AI全栈开发】
java
fox_lht2 小时前
15.3.改进我们之前的输入、输出项目
开发语言·后端·学习·rust
大鸡腿同学2 小时前
用 AI 肝了一个星期的智能客服助手,看看怎么个事
后端
IT_陈寒2 小时前
Python的os.path.join居然能这么坑?
前端·人工智能·后端
java1234_小锋2 小时前
LangChain4j 开发Java Agent智能体- 多模态支持
java·开发语言·langchain4j