• 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">
    " +
     25                 "   <soapenv:Header/>
    " +
     26                 "   <soapenv:Body>
    " +
     27                 "      <les:LES_LWL_001_AvailableInventoryInformationService>
    " +
     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>
    ]]></les:DATA>
    " +
     29                 "      </les:LES_LWL_001_AvailableInventoryInformationService>
    " +
     30                 "   </soapenv:Body>
    " +
     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 += "
    " + line;
     92             }
     93         } catch (Exception e) {
     94             System.out.println("发送POST请求出现异常" + e);
     95             e.printStackTrace();
     96         }
     97         return result;
     98     }
     99 
    100 }
    原创文章,转载请说明出处,谢谢合作
  • 相关阅读:
    插播一条 WMI修复教程
    DirectX12 3D 游戏开发与实战第八章内容(上)
    陆地与波浪演示程序(第七章内容)
    DirectX12 3D 游戏开发与实战第七章内容(下)
    C++匿名函数的使用
    绘制多种几何体演示程序(第七章内容)
    DirectX12 3D 游戏开发与实战第七章内容(上)
    DirectX12 3D 游戏开发与实战第六章内容
    DirectX12 3D 游戏开发与实战第五章内容
    无法解析的外部符号
  • 原文地址:https://www.cnblogs.com/lwl80/p/15416982.html
Copyright © 2020-2023  润新知