远程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 {
       //调用失败
    }
相关推荐
国思RDIF框架1 天前
RDIFramework.NET CS 敏捷开发框架 V6.3 版本重磅发布!.NET8+Framework双引擎,性能升级全维度进化
后端·.net
心在飞扬1 天前
ReRank重排序提升RAG系统效果
前端·后端
喝茶与编码1 天前
Python异步并发控制:asyncio.gather 与 Semaphore 协同设计解析
后端·python
不早睡不改名1 天前
网络编程基础:从BIO到NIO再到AIO(一)
后端
开源之眼1 天前
《github star 加星 Taimili.com 艾米莉 》为什么Java里面,Service 层不直接返回 Result 对象?
java·后端·github
心在飞扬1 天前
RAPTOR 递归文档树优化策略
前端·后端
zone77391 天前
003:RAG 入门-LangChain 读取图片数据
后端·python·面试
心在飞扬1 天前
LangChain Parent Document Retriever (父文档检索器)
后端
zone77391 天前
002:RAG 入门-LangChain 读取文本
后端·算法·面试
用户8356290780511 天前
在 PowerPoint 中用 Python 添加和定制形状的完整教程
后端·python