http请求传json串或者是key=value模式

本文介绍了一个使用ByteTCC实现的分布式事务测试案例。通过模拟HTTP请求并发测试其机制及性能表现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近做一个bytetcc分布式事务的试验看看在高并发下它的机制,和性能如何。所以要模拟http请求,直接上代码

public class HttpSendTest {


    static CyclicBarrier cyclicBarrier = new CyclicBarrier(10);//让十个线程同时请求


    public static void main(String[] args) throws InterruptedException, BrokenBarrierException,
                                          IOException, TimeoutException {
        test();
    }


    public static void test() throws InterruptedException, BrokenBarrierException, IOException,
                             TimeoutException {
        for (int i = 0; i < 50; i++) {
            final Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    while (true) {
                        try {
                            Thread.currentThread().sleep(1000);
                        } catch (InterruptedException e1) {
                            // logger.error("", e);
                        }
                        try {

//这是key=value的写法 post
                            httpRequest(
                                "http://192.168.2.49:7080/transfer?Id=1001&amount=1&acctId=2001",
                                "POST", "");
                        } catch (IOException e) {
                            //logger.error("", e);
                        } catch (InterruptedException e) {
                            //logger.error("", e);
                        } catch (BrokenBarrierException e) {
                            // logger.error("", e);
                        } catch (TimeoutException e) {
                            // logger.error("", e);
                        }
                    }


                }
            });
            thread.start();
            System.out.println(thread.getName());
        }
    }


    public static String httpRequest(String requestUrl, String requestMethod, String data)
                                                                                          throws IOException,
                                                                                          InterruptedException,
                                                                                          BrokenBarrierException,
                                                                                          TimeoutException {


        System.out.println("httpRequest==" + Thread.currentThread().getName());
        int number = cyclicBarrier.await();
        System.out.println("number=" + number);
        URL url = new URL(requestUrl);
        StringBuffer buffer = new StringBuffer();
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
        // 设置通用的请求属性
        httpURLConnection.setRequestProperty("Accept", "*/*");
        httpURLConnection.setRequestProperty("Accept-Charset", "UTF-8");
        httpURLConnection.setRequestProperty("connection", "keep-alive");
        // httpURLConnection.setRequestProperty("Content-Length", data.getBytes("UTF-8").length + "");
        // 发送POST请求必须设置如下两行
        httpURLConnection.setDoOutput(true);
        httpURLConnection.setDoInput(true);
        httpURLConnection.setUseCaches(false);
        // 设置请求方式(GET/POST)
        httpURLConnection.setRequestMethod(requestMethod);
        httpURLConnection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");


        OutputStream outputStream = httpURLConnection.getOutputStream();
        // 注意编码格式,防止中文乱码
        outputStream.write(data.getBytes("UTF-8"));
        outputStream.close();
        int code = httpURLConnection.getResponseCode();
        if (code == 200) {
            // 将返回的输入流转换成字符串
            InputStream inputStream = httpURLConnection.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String str = null;
            while ((str = bufferedReader.readLine()) != null) {
                buffer.append(str);
            }
            bufferedReader.close();
            inputStreamReader.close();
            // 释放资源
            inputStream.close();
            inputStream = null;
            httpURLConnection.disconnect();
            // JSONObject jsonObject = JSONObject.parseObject(buffer.toString()); //这里可以把服务器返回的数据转成 jsonObject  然后方便取数据
            System.out.println(buffer.toString());
        }
        return buffer.toString();
    }
}

   key=value模式直接在请求的url后面类似get请求的方式

服务端编写:

  @ResponseBody
    @RequestMapping(value = "/transfer", method = RequestMethod.POST)
    @Transactional
    public String transfer(@RequestParam String Id, @RequestParam String acctId,
                           @RequestParam double amount);


如果要传json传到服务端   用map封装:

Map<String, Object> map =new HashMap<String, Object>();

map.put("data", "xxx");

map.put("data1", "xxx1");

Stringdata= JSON.toJSONString(map);

 httpRequest( requestUrl, requestMethod, data);


服务端编写:

@RequestMapping(value = "/SyncUserInfo", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
@ResponseBody
public String getDetail(@RequestBody JSONObject jsonObject, HttpServletRequest request,
HttpServletResponse response)
//当然你自己定义有model也可以不用JSONObject 接收;


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值