java访问webserice接口xml报文
1 package com.webservice.client.config.LWL_001; 2 3 import org.dom4j.DocumentException; 4 import org.dom4j.DocumentHelper; 5 import sun.misc.BASE64Encoder; 6 import java.io.*; 7 import java.net.URL; 8 import java.net.URLConnection; 9 import org.dom4j.Document; 10 import org.dom4j.Element; 11 12 13 /** 14 * @author 刘文龙 15 * @create 2021-10-15 17:25:47 16 */ 17 @SuppressWarnings("all") 18 public class LWL_001_AvailableInventoryInformation { 19 20 public static void main(String[] args) throws IOException, DocumentException { 21 String URL_FOTON_763 = "http://localhost:8080/WP_FOTON/test/LES_LWL_001_AvailableInventoryInformation_PS?wsdl"; 22 String acount = "账号:密码"; 23 24 String requestBody = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:les=\"http://www.foton.com.cn/LES_LWL_001_AvailableInventoryInformation\">\n" + 25 " <soapenv:Header/>\n" + 26 " <soapenv:Body>\n" + 27 " <les:LES_LWL_001_AvailableInventoryInformationService>\n" + 28 " <les:DATA><![CDATA[<DATA><HEAD><BIZTRANSACTIONID>SAP_FOTONCS_001_2021041410031100</BIZTRANSACTIONID><COUNT>1</COUNT><CONSUMER>SAP</CONSUMER><SRVLEVEL>1</SRVLEVEL><ACCOUNT>SAP</ACCOUNT><PASSWORD>SAP1509030</PASSWORD></HEAD><LIST><ITEM><message>test</message></ITEM></LIST></DATA>\n]]></les:DATA>\n" + 29 " </les:LES_LWL_001_AvailableInventoryInformationService>\n" + 30 " </soapenv:Body>\n" + 31 "</soapenv:Envelope>"; 32 33 String result = sendPost(URL_FOTON_763, requestBody, acount); 34 System.out.println("请求报文:" + requestBody); 35 System.out.println("请求地址:" + URL_FOTON_763); 36 System.out.println("反馈:" + result); 37 38 Document doc = null; 39 try { 40 doc = DocumentHelper.parseText(result.trim()); 41 } catch (DocumentException e) { 42 e.printStackTrace(); 43 } 44 45 Element root = doc.getRootElement();// 指向根节点 46 String message = root.element("Body").element("getLES_LWL_001_AvailableInventoryInformationResponse").elementTextTrim("MESSAGE").trim(); 47 String sign = root.element("Body").element("getLES_LWL_001_AvailableInventoryInformationResponse").elementTextTrim("SIGN").trim(); 48 49 System.out.println("message:" + message); 50 System.out.println("sign:" + sign); 51 52 53 } 54 55 56 /** 57 * 向指定URL发送POST方式的请求 58 * 59 * @param url 发送请求的URL 60 * @param param 请求参数 61 * @return URL 代表远程资源的响应 62 */ 63 public static String sendPost(String url, String requestBody, String acount) { 64 String result = ""; 65 try { 66 URL realUrl = new URL(url); 67 //打开和URL之间的连接 68 URLConnection conn = realUrl.openConnection(); 69 //设置通用的请求属性 70 conn.setRequestProperty("accept", "*/*"); 71 conn.setRequestProperty("connection", "Keep-Alive"); 72 conn.setRequestProperty("Content-Type", "text/xml;charset=UTF-8"); 73 conn.setRequestProperty("Authorization", "Basic " + new BASE64Encoder().encode(acount.getBytes())); 74 conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); 75 //conn.setRequestProperty("SOAPAction", "LES_LWL_001_AvailableInventoryInformationService"); 76 77 //发送POST请求必须设置如下两行 78 conn.setDoOutput(true); 79 conn.setDoInput(true); 80 81 //获取URLConnection对象对应的输出流 82 PrintWriter out = new PrintWriter(conn.getOutputStream()); 83 //发送请求参数 84 out.print(requestBody); 85 //flush输出流的缓冲 86 out.flush(); 87 // 定义 BufferedReader输入流来读取URL的响应 88 BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8")); 89 String line; 90 while ((line = in.readLine()) != null) { 91 result += "\n" + line; 92 } 93 } catch (Exception e) { 94 System.out.println("发送POST请求出现异常" + e); 95 e.printStackTrace(); 96 } 97 return result; 98 } 99 100 }
原创文章,转载请说明出处,谢谢合作
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律