diff --git a/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsTCPSampler.java b/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsTCPSampler.java index 2bf5686044..3c36ea9127 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsTCPSampler.java +++ b/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsTCPSampler.java @@ -106,7 +106,7 @@ public class MsTCPSampler extends MsTestElement { break; case "xml": if (CollectionUtils.isNotEmpty(this.xmlDataStruct)) { - request = TcpTreeTableDataParser.treeTableData2Xml(this.xmlDataStruct); + request = TcpTreeTableDataParser.treeTableData2Xml(this.xmlDataStruct, this.connectEncoding); } break; case "raw": diff --git a/api-test/backend/src/main/java/io/metersphere/api/parse/scenario/TcpTreeTableDataParser.java b/api-test/backend/src/main/java/io/metersphere/api/parse/scenario/TcpTreeTableDataParser.java index 40ed5ff376..6f0da1961b 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/parse/scenario/TcpTreeTableDataParser.java +++ b/api-test/backend/src/main/java/io/metersphere/api/parse/scenario/TcpTreeTableDataParser.java @@ -34,7 +34,7 @@ public class TcpTreeTableDataParser { public static final String DATA_TYPE_STRING = PropertyConstant.STRING; public static final String DATA_TYPE_OBJECT = PropertyConstant.OBJECT; - public static String treeTableData2Xml(List treeDataList) { + public static String treeTableData2Xml(List treeDataList, String encoding) { String xmlString = StringUtils.EMPTY; try { if (treeDataList == null || treeDataList.isEmpty()) { @@ -56,7 +56,12 @@ public class TcpTreeTableDataParser { // 设置XML文档格式 OutputFormat outputFormat = OutputFormat.createPrettyPrint(); // 设置XML编码方式,即是用指定的编码方式保存XML文档到字符串(String),这里也可以指定为GBK或是ISO8859-1 - outputFormat.setEncoding(StandardCharsets.UTF_8.name()); + if (StringUtils.equals(encoding, StandardCharsets.UTF_8.name())) { + outputFormat.setEncoding(StandardCharsets.UTF_8.name()); + } else { + //目前只有UTF-8和GBK两种编码 + outputFormat.setEncoding(encoding); + } //outputFormat.setSuppressDeclaration(true); //是否生产xml头 outputFormat.setIndent(true); //设置是否缩进 outputFormat.setNewlines(true); //设置是否换行 diff --git a/api-test/backend/src/main/java/io/metersphere/service/definition/TcpApiParamService.java b/api-test/backend/src/main/java/io/metersphere/service/definition/TcpApiParamService.java index 815b388008..9941ff9d2f 100644 --- a/api-test/backend/src/main/java/io/metersphere/service/definition/TcpApiParamService.java +++ b/api-test/backend/src/main/java/io/metersphere/service/definition/TcpApiParamService.java @@ -11,7 +11,6 @@ import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import jakarta.annotation.Resource; import java.util.List; /** @@ -59,7 +58,7 @@ public class TcpApiParamService { tcpSampler.setRequest(tcpSampler.getRawDataStruct()); break; case "xml": - String xmlDataStruct = this.genValueFromEsbDataStructByParam(tcpSampler.getXmlDataStruct()); + String xmlDataStruct = this.genValueFromEsbDataStructByParam(tcpSampler.getXmlDataStruct(), tcpSampler.getConnectEncoding()); tcpSampler.setRequest(xmlDataStruct); break; case "json": @@ -75,8 +74,8 @@ public class TcpApiParamService { } //通过报文模版中的变量参数,解析报文数据结构,生成对应的xml数据 - private String genValueFromEsbDataStructByParam(List dataStructRequestList) { - String returnValue = TcpTreeTableDataParser.treeTableData2Xml(dataStructRequestList); + private String genValueFromEsbDataStructByParam(List dataStructRequestList, String connectEncoding) { + String returnValue = TcpTreeTableDataParser.treeTableData2Xml(dataStructRequestList, connectEncoding); return returnValue; }