java訪問webserice介面xml報文
阿新 • • 發佈:2021-10-17
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 * @create2021-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 }