HttpURLConnection post请求 传参数

本文详细介绍了如何利用Java的HttpURLConnection类进行POST请求,包括设置请求方法、添加请求头和参数,以及发送和接收数据的过程。
摘要由CSDN通过智能技术生成
    public static JSONObject post(String urlString, ParamInfo paramInfo) throws Exception {
        URL url = new URL(urlString);
        JSONObject jsonObject = new JSONObject();
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoInput(true); // 设置该连接可输入
        conn.setDoOutput(true); // 设置该连接可输出
        conn.setRequestMethod("POST"); // 设置请求方式
        conn.setRequestProperty("accept","application/json");
        conn.setRequestProperty("Connection", "Keep-Alive");
        conn.setRequestProperty("Cache-Control", "no-cache");
        conn.setRequestProperty("User-Agent", "Mozilla/5.0");
        String paramJsonStr = JSON.toJSONString(paramInfo);
//        注释掉的方法没成功
//        conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
//        System.out.println(paramJsonStr);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值