SOAPMessage类型数据存入.xml文件

本文介绍了一种方法,用于将从WebService接收到的SOAPMessage响应保存为XML文件。此过程包括检查SOAP响应中是否包含错误,并通过SOAPPart获取响应内容。
将web service传回的SOAPMessage信息存储成.xml文件
SOAPMessage reply = null;
String url="https://" + username + ":" + password + "@" + host + ":" + port + "/axl/";
reply = con.call(requestMessage,url); //con为SOAPConnection,requestMessage为请求的SOAPMessage信息

if(reply!=null){
//Check if reply includes soap fault
                SOAPPart replySP = reply.getSOAPPart();
                SOAPEnvelope replySE = replySP.getEnvelope();
                SOAPBody replySB = replySE.getBody();
             
                if (replySB.hasFault()) {
                    System.out.println("ERROR: " + replySB.getFault().getFaultString());
                }
                else {
                    System.out.println("Positive response received.");
                }
FileWrite fw = new FileWriter(outputFile,true); //outputFile为要写入的.xml文件,如result.xml
BufferedWriter bw = new BufferedWriter(fw);
Source source = reply.getSOAPPart().getContent();
Transformer transformer = TransformerFactory.newInstance().newTransformer();
ByteArrayOutputStream myOutStr = new ByteArrayOutputStream();
StreamResult res = new StreamResult();
res.setOutputStream(myOutStr);
transformer.transform(source,res);
String temp = myOutStr.toString().trim();

bw.write(temp);
bw.newLine();
bw.flush();
bw.close();
public void Tests() { try { string URL = "http://192.168.65.4:5051/JavaInterfaces/UniServices.asmx"; string jsonString = ""; var param = new { datatype = "01" }; jsonString = JsonConvert.SerializeObject(param); string soapRequest = $@" <?xml version=""1.0"" encoding=""utf-8""?> <soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/""> <soap:Body> <UniRequest xmlns=""http://tempuri.org/""> <rb> <account>user</account> <optype>0</optype> <param>{jsonString}</param> <password>123456</password> <sericeName>GET_BASEDATA_INFO</sericeName> </rb> </UniRequest> </soap:Body> </soap:Envelope>"; HttpClient client = new HttpClient(); client.Timeout = TimeSpan.FromSeconds(10); HttpContent content = new StringContent(soapRequest, Encoding.UTF8, "text/xml"); // 发送同步的 POST 请求并等待响应 HttpResponseMessage response = client.PostAsync(URL, content).Result; response.EnsureSuccessStatusCode(); string result = response.Content.ReadAsStringAsync().Result; // 解析响应 XDocument doc = XDocument.Parse(result); XNamespace soap = "http://schemas.xmlsoap.org/soap/envelope/"; XNamespace tempuri = "http://tempuri.org/"; string? resultCode = doc.Descendants(tempuri + "result").FirstOrDefault()?.Value; string? msgCode = doc.Descendants(tempuri + "msgCode").FirstOrDefault()?.Value; resultCode_ = resultCode; //datas_ = datas; msgCode_ = msgCode; } catch (Exception ex) { err2 = ex.Message; } }这是我的代码,POSTMAN上的请求头有只有参数名Content-Type String * 参数值 text/xml; charset=utf-8
03-13
`javax.xml.soap.SOAPException: InputStream does not represent a valid SOAP 1.1 Message` 异常表明输入流中的数据不符合SOAP 1.1消息的格式规范。解决该问题可从以下几个方面着手: #### 检查SOAP消息格式 确保输入的SOAP消息格式正确,SOAP 1.1消息需包含正确的命名空间和结构。以下是一个正确的SOAP 1.1消息示例: ```xml <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Header> <!-- 可选的头部信息 --> </soap:Header> <soap:Body> <!-- 消息主体内容 --> <m:GetStockPrice xmlns:m="http://www.example.org/stock"> <m:StockName>IBM</m:StockName> </m:GetStockPrice> </soap:Body> </soap:Envelope> ``` #### 检查编码问题 确保输入流使用正确的字符编码,常见的编码为UTF-8。以下是创建输入流时指定编码的示例: ```java import javax.xml.soap.*; import java.io.ByteArrayInputStream; import java.nio.charset.StandardCharsets; public class SoapExample { public static void main(String[] args) { String soapMessageStr = "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" + "<soap:Body>" + "<m:GetStockPrice xmlns:m=\"http://www.example.org/stock\">" + "<m:StockName>IBM</m:StockName>" + "</m:GetStockPrice>" + "</soap:Body>" + "</soap:Envelope>"; try { MessageFactory messageFactory = MessageFactory.newInstance(); ByteArrayInputStream inputStream = new ByteArrayInputStream(soapMessageStr.getBytes(StandardCharsets.UTF_8)); SOAPMessage soapMessage = messageFactory.createMessage(null, inputStream); } catch (SOAPException e) { e.printStackTrace(); } } } ``` #### 检查输入流是否包含额外数据 确保输入流中仅包含SOAP消息,没有额外的空白字符或其他数据。可以在创建输入流前对字符串进行清理: ```java String cleanSoapMessage = soapMessageStr.trim(); ByteArrayInputStream inputStream = new ByteArrayInputStream(cleanSoapMessage.getBytes(StandardCharsets.UTF_8)); ``` #### 检查命名空间 SOAP消息中的命名空间必须正确,否则会导致解析失败。确保所有命名空间的声明和使用一致。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值