远程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 {
       //调用失败
    }
相关推荐
步行cgn5 小时前
Spring c 命名空间注入详解
java·后端·spring
明月_清风5 小时前
Maven 到底是什么?一篇文章搞懂 Java 项目构建与依赖管理
java·后端·maven
明月_清风5 小时前
AI 越来越强,程序员真正的价值到底是什么?
人工智能·后端
aramae5 小时前
MySQL复合查询(8)
java·c语言·开发语言·后端·算法
Rain的Java大神之路6 小时前
如何快速上传10G文件
java·spring boot·redis·后端·mysql·spring cloud·面试
Wang's Blog7 小时前
Java 接入Redis: 通用命令与键管理
java·服务器·redis
Wang's Blog7 小时前
Java 接入Redis: 列表集合与有序集合操作命令
java·服务器·redis
Ivanqhz7 小时前
SVD++算法
java·服务器·网络·深度学习·神经网络
码事漫谈7 小时前
别再跟AI说“请”了,它根本不 care——但有个东西它超在意
后端
qq_2518364578 小时前
springboot vue3 开发实现 拼豆管理系统
java·开发语言·ai编程