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

话不多说直接上代码:

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

<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:

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 @ 2020-08-21 11:07  47号Gamer丶  阅读(236)  评论(0)    收藏  举报