java http post json

pom.xml



        <!-- Apache HttpClient 依赖项 -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.13</version> <!-- 请根据需要选择合适的版本 -->
        </dependency>

 

HttpCLientUtils.java
package cn.felord.debezium.utils;

import cn.felord.debezium.entity.ItemHttpLed;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
@Slf4j
public class HttpCLientUtils {
    public static void main(String[] args) throws Exception {
        String url = "http://10.30.13.249:5003";
//        String url = "http://localhost:8280//api/tbUpload/getUploadFeedRealInfo";
//        String json = "{\"key\": \"value\"}";
//        String json = "{\"cmd\":4,\"duration\":0,\"items\":[{\"x\":0,\"y\":0,\"w\":64,\"h\":16,\"effect\":0,\"spd\":8,\"st\":0,\"zt_id\":1,\"zt_ys\":0,\"text\":\"12345678\"},{\"x\":0,\"y\":16,\"w\":64,\"h\":16,\"effect\":1,\"spd\":16,\"st\":0,\"zt_id\":1,\"zt_ys\":1,\"text\":\"测试0123456789\"}]}";
        JSONObject jsonObject = new JSONObject();
//        {"cmd":4,"duration":0,"items":[
//        {"x":0,"y":0,"w":64,"h":16,"effect":0,"spd":8,"st":0,"zt_id":1,"zt_ys":0,"text":"12345678"},
//        {"x":0,"y":16,"w":64,"h":16,"effect":1,"spd":16,"st":0,"zt_id":1,"zt_ys":1,"text":"测试0123456789"}]}
//        jsonObject.put("furnaceNum", "7");
        jsonObject.put("cmd",4);
        jsonObject.put("duration",0);
        JSONArray jsonArray = new JSONArray();
        ItemHttpLed.addItem(jsonArray,0,0,64,16,26,99,256,1,0,"12345678");
        ItemHttpLed.addItem(jsonArray,0,16,64,16,26,99,256,1,1,"测试0123456789");
        jsonObject.put("items",jsonArray);
        HttpPost httpPost = new HttpPost(url);
        httpPost.setURI(new URI(url));
        httpPost.setHeader("Content-Type", "application/json");
        log.info(jsonObject.toJSONString());
        log.info(jsonObject.toString());
//        StringEntity entity = new StringEntity(json, "UTF-8");
        StringEntity entity = new StringEntity(jsonObject.toJSONString(), "UTF-8");
        httpPost.setEntity(entity);
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = httpClient.execute(httpPost);
        try {
            HttpEntity responseEntity = response.getEntity();
            if (responseEntity != null) {
                InputStream inputStream = responseEntity.getContent();
                System.out.println(convertInputStreamToJson(inputStream));
            }
        } finally {
            response.close();
            httpClient.close();
        }
    }

    public static JSONObject convertInputStreamToJson(InputStream inputStream) {
        StringBuilder stringBuilder = new StringBuilder();
        String line;
        try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"))) {
            while ((line = bufferedReader.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        String jsonString = stringBuilder.toString();
        JSONObject jsonObject = JSONObject.parseObject(jsonString);
        return jsonObject;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

somdip

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值