In the process of docking with the hardware, we hope to simulate some binary data communicating with the hardware through IDEA, such as:
byte[] input = new byte[]{
// 类型编号
0x00,
// 中继器编号
(byte) 0xff, (byte) 0xff,
// 监视器类别0
0x12,
// 监视器编号
(byte) 0xab, (byte) 0xff, (byte) 0xcb
};
IDEA's own HTTP Request can quickly satisfy our various data requests, but in its official document , there is no direct example of sending binary data streams.
If you want to send some binary data stream, you can follow the steps below:
Build a binary file
We can create a new document with any name and extension. For example, we can name it 0x01.data
. The information stored in this document will be the binary data stream that we send http requests.
Install plugin
If you want to use the 0x01.data
file to meet our test requirements, you need a binary editor to edit it. BinEd plug-in appeared friendly in IDEA.
After finding the plug-in management and searching for the bind keyword in the market options, install:
After the plug-in is installed successfully, IDEA needs to be restarted.
Edit data file
Double-click the file 0x01.data
we created earlier, and choose any way to open it, for example, we choose to open it as text, then right-click in the file content area on the right, and select the last option Open As Binary
:
You can edit the binary file directly here.
Create request
Finally, you can use HTTP Request to send binary data based on the 0x01.data
We create a new Http Request at the same level of the 0x01.data
POST http://localhost:8081/yourApi
Content-Type: application/octet-stream
< ./0x01.data
At this time, please request to run it, and then you can successfully send the binary data stream to the background.
other
When the data is small, we can also tap it manually, but if the amount of data is relatively large, then we definitely look forward to using some JAVA syntax to construct (for example, we hope to generate 1M and regular binary data stream). At this time, some code is needed to assist, the sample code is as follows:
package club.yunzhi.switchgear.httpRequest;
import club.yunzhi.switchgear.input.MonitorInput0x01Test;
import org.apache.commons.lang3.ArrayUtils;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class Monitor0x01 {
@Test
void saveToDisk() {
// 使用代码来构建测试的二进制数组
byte[] category = new byte[]{
(byte) 0x00,
(byte) 0x00, (byte) 0x01,
(byte) 0x01
};
byte[] bytes = ArrayUtils.addAll(category, MonitorInput0x01Test.input);
// 将二进制写入到0x01.data中
File outputFile = new File("./0x01.data");
try {
FileOutputStream fileOutputStream = new FileOutputStream(outputFile);
fileOutputStream.write(bytes);
fileOutputStream.flush();
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
After the code runs, copy the generated data file to the location you want.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。