其他分享
首页 > 其他分享> > 完整的webservice接口调用过程

完整的webservice接口调用过程

作者:互联网

查看调用方法

调用地址: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);

 

标签:SYC,调用,webservice,接口,String,238,import,conn,out
来源: https://www.cnblogs.com/lwl80/p/15641574.html