一、交互流程图:
总结点:
问题:一开始设置http请求中content-type 设置为默认文本格式,导致使用http传输body信息的时候必须进行base64加密才可以传输,这样会导致增加传输1/3的数据量,
解决:使用流进行写入与读取,客户端请求中使用inputStream,服务端使用outputStream,http数据格式设置application/octet-stream;
二、示例代码:
服务端:
private static void sendProtobufObject() throws Exception { org.chench.test.protobuf.OriginalData.Tdata.Rdata.Builder rdata = org.chench.test.protobuf.OriginalData.Tdata.Rdata.newBuilder(); rdata.setV(101); rdata.setC("got doub"); rdata.setR("test"); rdata.setRS("rolen1"); org.chench.test.protobuf.OriginalData.Tdata.Builder tdata = org.chench.test.protobuf.OriginalData.Tdata.newBuilder(); tdata.setDatatype("aias"); tdata.setIdVer("An1"); tdata.setTrans("y"); tdata.setRk("uZkmGAC="); tdata.addData(rdata); org.chench.test.protobuf.OriginalData.Tdata staff = tdata.build(); byte[] buff = staff.toByteArray(); //String source = new String(buff, "utf-8"); byte[] payload = Encrypt(buff, "1234567890123456"); //封装最外层数据 org.chench.test.protobuf.messages.Amsg.Builder msg = org.chench.test.protobuf.messages.Amsg.newBuilder(); msg.setContentValue("zYaIF1toXcw="); msg.setContentId("1HEABwINCAcOAQ=="); msg.setVersionType("1001"); msg.setPayload(ByteString.copyFrom(payload)); URL url = new URL("http://127.0.0.1:9090/api/v1/suss_msg"); HttpURLConnection connection = (HttpURLConnection)url.openConnection(); connection.setConnectTimeout(15000); connection.setReadTimeout(60000); connection.setRequestMethod("POST"); connection.setDoInput(true); connection.setDoOutput(true); connection.setUseCaches(false); connection.setInstanceFollowRedirects(true); connection.setRequestProperty("Content-Type", "application/octet-stream"); //connection.connect(); // 通过连接对象获取一个输出流 OutputStream os = connection.getOutputStream(); // 通过输出流对象将参数写出去/传输出去,它是通过字节数组写出的 os.write(msg.build().toByteArray()); ///msg.build().writeTo(connection.getOutputStream()); os.flush(); os.close(); int responseCode = connection.getResponseCode();// 调用此方法就不必再使用conn.connect()方法 if (responseCode == 200) { }else { } connection.disconnect();// 关闭连接
客户端:
public Object processRequest(HttpServletRequest request) throws Exception { //new BASE64Decoder().decodeBuffer(requestBody) InputStream inputStream = request.getInputStream(); messages.Amsg msg = messages.Amsg.parseFrom(inputStream); System.out.println("----------header-------------"); System.out.println(msg.getContentValue()); System.out.println(msg.getContentId()); System.out.println(msg.getVersionType()); System.out.println("----------payload-------------"); byte[] data = Decrypt(msg.getPayload().toByteArray(), "1234567890123456"); OriginalData.Tdata tdata = OriginalData.Tdata.parseFrom(data); System.out.println("----------tdate:"); System.out.println(tdata.getAid()); System.out.println(tdata.getDatatype()); System.out.println(tdata.getIdVer()); System.out.println(tdata.getTrans()); System.out.println(tdata.getRk()); System.out.println("----------rdate:"); OriginalData.Tdata.Rdata rdata = tdata.getData(0); System.out.println(rdata.getA()); System.out.println(rdata.getP()); System.out.println(rdata.getM()); System.out.println(rdata.getGJ()); return null; }
Schema:
syntax = "proto3";package msg;option java_package = "org.chench.test.protobuf";option java_outer_classname = "messages";message Amsg { string content_value = 1; string content_id = 2; //appid string version_type = 3; //协议类型 bytes payload = 4; //加密数据}
syntax = "proto3";package originaldata;option java_package = "org.chench.test.protobuf";option java_outer_classname = "OriginalData";message Tdata { string aid = 1; string datatype = 2; string hk = 3; string id_ver = 4; message Rdata{ int32 V = 1; int32 oo = 2; //为区分大小写,原小写字母变为双小写 string pp = 3;} repeated Rdata data = 5; //rdata对含数据请参考安全热更请求数据}