SOAPMessage类型数据存入.xml文件

本文介绍了一种方法,用于将从WebService接收到的SOAPMessage响应保存为XML文件。此过程包括检查SOAP响应中是否包含错误,并通过SOAPPart获取响应内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

将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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值