Java运用apache的axis通过soap协议调用C#或C++发布的WebService

话不多说直接上代码:

C++的WebService说明书格式样例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://tempuri.org/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" targetnamespace="http://tempuri.org/">
   <wsdl:types>
    <s:schema elementformdefault="qualified" targetnamespace="http://tempuri.org/">
     <s:element name="GetList">
      <s:complextype>
       <s:sequence>
        <s:element minoccurs="0" maxoccurs="1" name="zt" type="s:string" />
       </s:sequence>
      </s:complextype>
     </s:element>   
     <s:element name="GetListResponse">
      <s:complextype>
       <s:sequence>
        <s:element minoccurs="0" maxoccurs="1" name="GetListResult" type="s:string" />
       </s:sequence>
      </s:complextype>
     </s:element>   
     <s:element name="MySoapHeader" type="tns:MySoapHeader" />
     <s:complextype name="MySoapHeader">
      <s:sequence>
       <s:element minoccurs="0" maxoccurs="1" name="UserName" type="s:string" />
       <s:element minoccurs="0" maxoccurs="1" name="PassWord" type="s:string" />
      </s:sequence>
      <s:anyattribute />
     </s:complextype>
     <s:element name="GetDataWzjh">
      <s:complextype>
       <s:sequence>
        <s:element minoccurs="0" maxoccurs="1" name="jhbh" type="s:string" />
       </s:sequence>
      </s:complextype>
     </s:element>   
     <s:element name="GetDataWzjhResponse">
      <s:complextype>
       <s:sequence>
        <s:element minoccurs="0" maxoccurs="1" name="GetDataWzjhResult" type="s:string" />
       </s:sequence>
      </s:complextype>
     </s:element>   
     <s:element name="GetDataZbgg">
      <s:complextype>
       <s:sequence>
        <s:element minoccurs="0" maxoccurs="1" name="zbggbh" type="s:string" />
       </s:sequence>
      </s:complextype>
     </s:element>   
     <s:element name="GetDataZbggResponse">
      <s:complextype>
       <s:sequence>
        <s:element minoccurs="0" maxoccurs="1" name="GetDataZbggResult" type="s:string" />
       </s:sequence>
      </s:complextype>
     </s:element>   
     <s:element name="SaveZbjg">
      <s:complextype>
       <s:sequence>
        <s:element minoccurs="0" maxoccurs="1" name="jhbh" type="s:string" />
       </s:sequence>
      </s:complextype>
     </s:element>
     <s:element minoccurs="0" maxoccurs="1" name="data" type="s:string" />   
     <s:element name="SaveZbjgResponse">
      <s:complextype>
       <s:sequence>
        <s:element minoccurs="0" maxoccurs="1" name="SaveZbjgResult" type="s:string" />
       </s:sequence>
      </s:complextype>
     </s:element>   
    </s:schema>
   </wsdl:types>
   <wsdl:message name="GetListSoapIn">
    <wsdl:part name="parameters" element="tns:GetList" />
   </wsdl:message>
   <wsdl:message name="GetListSoapOut">
    <wsdl:part name="parameters" element="tns:GetListResponse" />
   </wsdl:message>
   <wsdl:message name="GetListMySoapHeader">
    <wsdl:part name="MySoapHeader" element="tns:MySoapHeader" />
   </wsdl:message>
   <wsdl:message name="GetDataWzjhSoapIn">
    <wsdl:part name="parameters" element="tns:GetDataWzjh" />
   </wsdl:message>
   <wsdl:message name="GetDataWzjhSoapOut">
    <wsdl:part name="parameters" element="tns:GetDataWzjhResponse" />
   </wsdl:message>
   <wsdl:message name="GetDataWzjhMySoapHeader">
    <wsdl:part name="MySoapHeader" element="tns:MySoapHeader" />
   </wsdl:message>
   <wsdl:message name="GetDataZbggSoapIn">
    <wsdl:part name="parameters" element="tns:GetDataZbgg" />
   </wsdl:message>
   <wsdl:message name="GetDataZbggSoapOut">
    <wsdl:part name="parameters" element="tns:GetDataZbggResponse" />
   </wsdl:message>
   <wsdl:message name="GetDataZbggMySoapHeader">
    <wsdl:part name="MySoapHeader" element="tns:MySoapHeader" />
   </wsdl:message>
   <wsdl:message name="SaveZbjgSoapIn">
    <wsdl:part name="parameters" element="tns:SaveZbjg" />
   </wsdl:message>
   <wsdl:message name="SaveZbjgSoapOut">
    <wsdl:part name="parameters" element="tns:SaveZbjgResponse" />
   </wsdl:message>
   <wsdl:message name="SaveZbjgMySoapHeader">
    <wsdl:part name="MySoapHeader" element="tns:MySoapHeader" />
   </wsdl:message>
   <wsdl:porttype name="WebService_ShareSoap">
    <wsdl:operation name="GetList">
     <wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
      取共享计划数据列表
     </wsdl:documentation>
     <wsdl:input message="tns:GetListSoapIn" />
     <wsdl:output message="tns:GetListSoapOut" />
    </wsdl:operation>
    <wsdl:operation name="GetDataWzjh">
     <wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
      根据计划编号取计划数据
     </wsdl:documentation>
     <wsdl:input message="tns:GetDataWzjhSoapIn" />
     <wsdl:output message="tns:GetDataWzjhSoapOut" />
    </wsdl:operation>
    <wsdl:operation name="GetDataZbgg">
     <wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
      根据招标公告编号取公告及供应商报名数据
     </wsdl:documentation>
     <wsdl:input message="tns:GetDataZbggSoapIn" />
     <wsdl:output message="tns:GetDataZbggSoapOut" />
    </wsdl:operation>
    <wsdl:operation name="SaveZbjg">
     <wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
      回写招标结果
     </wsdl:documentation>
     <wsdl:input message="tns:SaveZbjgSoapIn" />
     <wsdl:output message="tns:SaveZbjgSoapOut" />
    </wsdl:operation>
   </wsdl:porttype>
   <wsdl:binding name="WebService_ShareSoap" type="tns:WebService_ShareSoap">
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http" />
    <wsdl:operation name="GetList">
     <soap:operation soapaction="http://tempuri.org/GetList" style="document" />
     <wsdl:input>
      <soap:body use="literal" />
      <soap:header message="tns:GetListMySoapHeader" part="MySoapHeader" use="literal" />
     </wsdl:input>
     <wsdl:output>
      <soap:body use="literal" />
     </wsdl:output>
    </wsdl:operation>
    <wsdl:operation name="GetDataWzjh">
     <soap:operation soapaction="http://tempuri.org/GetDataWzjh" style="document" />
     <wsdl:input>
      <soap:body use="literal" />
      <soap:header message="tns:GetDataWzjhMySoapHeader" part="MySoapHeader" use="literal" />
     </wsdl:input>
     <wsdl:output>
      <soap:body use="literal" />
     </wsdl:output>
    </wsdl:operation>
    <wsdl:operation name="GetDataZbgg">
     <soap:operation soapaction="http://tempuri.org/GetDataZbgg" style="document" />
     <wsdl:input>
      <soap:body use="literal" />
      <soap:header message="tns:GetDataZbggMySoapHeader" part="MySoapHeader" use="literal" />
     </wsdl:input>
     <wsdl:output>
      <soap:body use="literal" />
     </wsdl:output>
    </wsdl:operation>
    <wsdl:operation name="SaveZbjg">
     <soap:operation soapaction="http://tempuri.org/SaveZbjg" style="document" />
     <wsdl:input>
      <soap:body use="literal" />
      <soap:header message="tns:SaveZbjgMySoapHeader" part="MySoapHeader" use="literal" />
     </wsdl:input>
     <wsdl:output>
      <soap:body use="literal" />
     </wsdl:output>
    </wsdl:operation>
   </wsdl:binding>
   <wsdl:binding name="WebService_ShareSoap12" type="tns:WebService_ShareSoap">
    <soap12:binding transport="http://schemas.xmlsoap.org/soap/http" />
    <wsdl:operation name="GetList">
     <soap12:operation soapaction="http://tempuri.org/GetList" style="document" />
     <wsdl:input>
      <soap12:body use="literal" />
      <soap12:header message="tns:GetListMySoapHeader" part="MySoapHeader" use="literal" />
     </wsdl:input>
     <wsdl:output>
      <soap12:body use="literal" />
     </wsdl:output>
    </wsdl:operation>
    <wsdl:operation name="GetDataWzjh">
     <soap12:operation soapaction="http://tempuri.org/GetDataWzjh" style="document" />
     <wsdl:input>
      <soap12:body use="literal" />
      <soap12:header message="tns:GetDataWzjhMySoapHeader" part="MySoapHeader" use="literal" />
     </wsdl:input>
     <wsdl:output>
      <soap12:body use="literal" />
     </wsdl:output>
    </wsdl:operation>
    <wsdl:operation name="GetDataZbgg">
     <soap12:operation soapaction="http://tempuri.org/GetDataZbgg" style="document" />
     <wsdl:input>
      <soap12:body use="literal" />
      <soap12:header message="tns:GetDataZbggMySoapHeader" part="MySoapHeader" use="literal" />
     </wsdl:input>
     <wsdl:output>
      <soap12:body use="literal" />
     </wsdl:output>
    </wsdl:operation>
    <wsdl:operation name="SaveZbjg">
     <soap12:operation soapaction="http://tempuri.org/SaveZbjg" style="document" />
     <wsdl:input>
      <soap12:body use="literal" />
      <soap12:header message="tns:SaveZbjgMySoapHeader" part="MySoapHeader" use="literal" />
     </wsdl:input>
     <wsdl:output>
      <soap12:body use="literal" />
     </wsdl:output>
    </wsdl:operation>
   </wsdl:binding>
   <wsdl:service name="WebService_Share">
    <wsdl:port name="WebService_ShareSoap" binding="tns:WebService_ShareSoap">
     <soap:address location="http://10.194.7.214/WZCG/INTERFACE/WebService_Share.asmx" />
    </wsdl:port>
    <wsdl:port name="WebService_ShareSoap12" binding="tns:WebService_ShareSoap12">
     <soap12:address location="http://10.194.7.214/WZCG/INTERFACE/WebService_Share.asmx" />
    </wsdl:port>
   </wsdl:service>
</wsdl:definitions>

GsopUtil:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
 
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.soap.SOAPException;
 
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.apache.axis.message.SOAPHeaderElement;
import org.apache.commons.collections.MapUtils;
 
import com.sckj.base.exception.ServiceException;
 
 
public class GsopUtil {
    /**
     * Java运用apache的axis通过soap协议调用C#发布的WebService
     * @author 47Gamer
     * @date 2019年10月28日
     * @param endpointURL
     *      WebService说明书地址
     * @param method
     *      说明书里发布的方法
     * @param serviceName
     *      说明书service服务名称
     * @param targetNamespace
     *      说明书的targetNamespace
     * @param
     *      说明书里的part名称
     * @param soapHeaderMap
     *      协议头的键值对参数
     * @param objs
     *      传入的参数值
     * @param params
     *      传入的说明书params参数
     */
    public static String getDataFromGsop(String endpointURL,String method,String serviceName,String targetNamespace,String part,Map<String
            ,Object> soapHeaderMap,Object[] objs,String[] params) {
        String result = "";
        try {
            // 服务端的url,需要根据情况更改
            Service service = new Service();
            Call call = (Call) service.createCall();
            call.setTargetEndpointAddress(new URL(endpointURL));
            call.setUseSOAPAction(true);
            call.setEncodingStyle("UTF-8");
            call.setReturnType(XMLType.XSD_STRING);// 返回的数据类型
            // 由于需要认证,故需要设置调用的用户名和密码
            SOAPHeaderElement soapHeaderElement = new SOAPHeaderElement(new QName(targetNamespace, part));
            soapHeaderElement.setNamespaceURI(targetNamespace);
            try {
                if(MapUtils.isNotEmpty(soapHeaderMap)){
                    for (Entry<String, Object> entry : soapHeaderMap.entrySet()) {
                        soapHeaderElement.addChildElement((String)entry.getKey()).setValue((String) entry.getValue());
                    }
                }
            } catch (SOAPException e) {
                e.printStackTrace();
            }
            // 参数的类型
            for(int i=0;i<params.length;i++){
                call.addParameter(new QName(targetNamespace,params[i]), XMLType.XSD_STRING, ParameterMode.IN);
            }
            call.addHeader(soapHeaderElement);
            call.setSOAPActionURI(targetNamespace + method);
            call.setOperationName(new QName(targetNamespace, method));// 设置操作的名称。
            result = (String) call.invoke(objs);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (javax.xml.rpc.ServiceException e) {
            e.printStackTrace();
        } catch (RemoteException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException("调用webservice错误!");
        }
        return result;
    }
     
    public static void main(String[] args) {
        String endpointURL = "http://10.192.179.119/WZCG/INTERFACE/WebService_Share.asmx?wsdl";
        String method = "GetDataZbgg";
        String serviceName = "WebService_Share";
        String targetNamespace = "http://tempuri.org/";
        String part = "MySoapHeader";
        Map<String, Object> soapHeaderMap = new HashMap<String, Object>(16);
        soapHeaderMap.put("UserName", "root");
        soapHeaderMap.put("PassWord", "123456");
        String[] params = new String[]{ "zbggbh" };
        Object[] objs = new Object[]{ "CTWC[2016]0772" };
        String zbggData = GsopUtil.getDataFromGsop(endpointURL, method, serviceName, targetNamespace, part, soapHeaderMap, objs, params);
        System.out.println(zbggData);
    }
}

学到了吗?

posted @   47号Gamer丶  阅读(221)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具
点击右上角即可分享
微信分享提示