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 }

 

posted @ 2021-10-17 15:01  勤快的懒羊羊  阅读(435)  评论(0编辑  收藏  举报