查看調用方法
調用地址:http://localhost:8080/WP_FOTON_FSOA/APP_FACTOR_SERVICES/Proxy_Services/TA_SAP/FACTOR_SYC_238_SendEarnestMoneyInfo_PS?wsdl
根據接口的wsdl文檔可以看到調用的接口方法(FACTOR_SYC_238_SendEarnestMoneyInfoService)以及反饋參數(getFACTOR_SYC_238_SendEarnestMoneyInfoResponse)里的SIGN,MESSAGE。

查看調用格式
可以用解析webservice的框架解析也可以使用外部工具,如SoapUI解析,如下(SoapUI示例);通過SoapUI即可查看調用接口的SOAP格式

調用接口
調用接口可通過wsimport編譯,將接口的wsdl編譯成本地實體類,然后調用實體類的方法即可,下面演示使用Post直接請求的方式。
一、post請求方法
1 package com.lwl.util; 2 3 import sun.misc.BASE64Encoder; 4 5 import java.io.*; 6 import java.net.HttpURLConnection; 7 import java.net.URL; 8 import java.net.URLConnection; 9 import java.nio.charset.StandardCharsets; 10 11 /** 12 * post請求 13 * 14 * @author liuwenlong 15 * @create 2021-12-04 12:25:00 16 */ 17 @SuppressWarnings("all") 18 public class SendPost { 19 20 /** 21 * Post請求一個地址 22 * 23 * @param URL 請求地址 24 * @param requestBody 請求的body 25 * @param Authorization 賬號密碼 26 * @return 返回調用結果 27 */ 28 public String doPost(String URL, String requestBody,String Authorization) { 29 OutputStreamWriter out = null; 30 BufferedReader in = null; 31 StringBuilder result = new StringBuilder(); 32 HttpURLConnection conn = null; 33 34 try { 35 java.net.URL url = new URL(URL); 36 conn = (HttpURLConnection) url.openConnection(); 37 BASE64Encoder base = new BASE64Encoder(); 38 String encodedPassword = base.encode(Authorization.getBytes("UTF-8")); 39 //System.out.println("加密后的密碼:" + encodedPassword); 40 //將加密的賬號密碼放到請求頭里,這里注意Basic后面要加空格 41 conn.setRequestProperty("Authorization", "Basic " + encodedPassword); 42 conn.setRequestMethod("POST"); 43 //發送POST請求必須設置為true 44 conn.setDoOutput(true); 45 conn.setDoInput(true); 46 //設置連接超時時間和讀取超時時間 47 conn.setConnectTimeout(3000); 48 conn.setReadTimeout(3000); 49 conn.setRequestProperty("Content-Type","text/xml;charset=UTF-8"); 50 //conn.setRequestProperty("Accept", "application/json"); 51 //獲取輸出流,寫入請求的json或者xml報文 52 out = new OutputStreamWriter(conn.getOutputStream(),"utf-8"); 53 //System.out.println(requestBody); 54 55 out.write(requestBody); //獲取請求的body, 56 out.flush(); 57 out.close(); 58 //取得輸入流,並使用Reader讀取 59 if (200 == conn.getResponseCode()) { 60 in = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8)); 61 String line; 62 while ((line = in.readLine()) != null) { 63 result.append(line); 64 //System.out.println(line); 65 } 66 } else { 67 System.out.println("ResponseCode is an error code:" + conn.getResponseCode()); 68 } 69 } catch (Exception e) { 70 e.printStackTrace(); 71 } finally { 72 try { 73 if (out != null) { 74 out.close(); 75 } 76 if (in != null) { 77 in.close(); 78 } 79 } catch (IOException ioe) { 80 ioe.printStackTrace(); 81 } 82 } 83 return result.toString(); 84 } 85 86 }
二、組裝報文,調用方法
1 package com.restful.client.config.FFS; 2 3 import com.lwl.util.SendPost; 4 import org.dom4j.Document; 5 import org.dom4j.DocumentException; 6 import org.dom4j.DocumentHelper; 7 import org.dom4j.Element; 8 9 /** 10 * @author liuwenlong 11 * @create 2021-12-04 12:25:31 12 */ 13 @SuppressWarnings("all") 14 public class SYC_238 { 15 public static void main(String[] args) { 16 //請求地址 17 String test_URL_SYC_238 = "http://localhost:8080/WP_FOTON_FSOA/APP_FACTOR_SERVICES/Proxy_Services/TA_SAP/FACTOR_SYC_238_SendEarnestMoneyInfo_PS?wsdl"; 18 19 SendPost sendPost = new SendPost(); 20 String username = "admin"; //賬號 21 String password = "admin111"; //密碼 22 String Authorization = username + ":" + password; 23 24 String sendMessage = "請求報文放這里"; 25 26 String requestBody = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:fac=\"http://www.***.com.cn/FACTOR_SYC_238_SendEarnestMoneyInfo\">\n" + 27 " <soapenv:Header/>\n" + 28 " <soapenv:Body>\n" + 29 " <fac:FACTOR_SYC_238_SendEarnestMoneyInfoService>\n" + 30 " <fac:DATA><![CDATA[" + sendMessage + "]]></fac:DATA>\n" + 31 " </fac:FACTOR_SYC_238_SendEarnestMoneyInfoService>\n" + 32 " </soapenv:Body>\n" + 33 "</soapenv:Envelope>"; 34 System.out.println("requestBody\n" + requestBody); 35 String responseBody = sendPost.doPost(test_URL_SYC_238, requestBody, Authorization); 36 System.out.println("responseBody:\n" + responseBody); 37 38 } 39 }
三、獲取反饋,獲取反饋可以直接根據wsdl里getFACTOR_SYC_238_SendEarnestMoneyInfoResponse方法,並取出WSDL文檔里描述的SIGN,MESSAGE;
1 Document doc = null; 2 try { 3 doc = DocumentHelper.parseText(responseBody.trim()); 4 } catch ( 5 DocumentException e) { 6 e.printStackTrace(); 7 } 8 Element root = doc.getRootElement();// 指向根節點 9 String message = root.element("Body").element("getFACTOR_SYC_238_SendEarnestMoneyInfoResponse").elementTextTrim("MESSAGE").trim(); 10 String sign = root.element("Body").element("getFACTOR_SYC_238_SendEarnestMoneyInfoResponse").elementTextTrim("SIGN").trim(); 11 12 System.out.println("\n反饋報文:" + message); 13 System.out.println("標識:" + sign); 14 String result = ""; 15 if ("0".equals(sign)) { 16 result = "調用成功!"; 17 } else { 18 result = "調用失敗!"; 19 } 20 System.out.println(result);

