Protobuf协议接口请求工具

最近在工作遇到了Protobuf协议接口,现成的测试工具jmeter、postman等,无法直接测试,想到开发工具快速集成测试

以下是工具思路

1、使用原始protos文件生成java 类文件

2、构建基础请求

BaiduRtaProtos.RtaApiRequest request = requestBuilder.build();

注意:返回类型 GeneratedMessageV3

3、httpclient 封装请求方法

public static HttpResponse postBytes(String url, GeneratedMessageV3 message) throws IOException {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpPost post = new HttpPost(url);
    ByteArrayInputStream inputStream = new ByteArrayInputStream(message.toByteArray());
    InputStreamEntity inputStreamEntity = new InputStreamEntity(inputStream);
    post.setEntity(inputStreamEntity);
    post.addHeader("Content-Type", "application/octet-stream");
    return httpclient.execute(post);
}

 

4、基础请求结合策略模式,开发可转发多渠道的Protobuf转发接口






posted @ 2025-01-07 20:04  樊文鹏  阅读(225)  评论(0)    收藏  举报